Skip to content

Commit

Permalink
Fixed false positive for methods of namedtuples instances
Browse files Browse the repository at this point in the history
  • Loading branch information
yushao2 committed May 21, 2021
1 parent 55d3cb7 commit d2a59ec
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ modules are added.

Closes #3619

* Fix raising false-positive ``no-member`` on instances' methods where class can be inferred
to be derived from Named Tuples

Closes #4377


What's New in Pylint 2.8.2?
===========================
Expand Down
3 changes: 3 additions & 0 deletions doc/whatsnew/2.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ Other Changes
of overridden functions. It aims to separate the functionality of ``arguments-differ``.

* Fix incompatibility with Python 3.6.0 caused by ``typing.Counter`` and ``typing.NoReturn`` usage

* Fix raising false-positive ``no-member`` on instances' methods where class can be inferred
to be derived from Named Tuples
18 changes: 18 additions & 0 deletions pylint/checkers/typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,24 @@ def _emit_no_member(node, owner, owner_name, ignored_mixins=True, ignored_none=T
# See https://github.com/PyCQA/pylint/issues/4123
return False

if (
isinstance(node.parent, astroid.Call)
and isinstance(owner, astroid.Instance)
and isinstance(owner.parent, astroid.Module)
and isinstance(getattr(owner, "_proxied", None), astroid.ClassDef)
and any(
(
isinstance(b, astroid.Name)
and b.name == "tuple"
and utils.is_builtin_object(utils.safe_infer(b))
)
for b in owner._proxied.bases
)
):
# Avoid false positive on function calls on instances of named tuples
# See https://github.com/PyCQA/pylint/issues/4377
return False

return True


Expand Down
14 changes: 14 additions & 0 deletions tests/functional/m/member/member_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,17 @@ class Animal(Enum):
print(keyy)
for vall in Animal.__members__.values():
print(vall)

# To test false positive no-member after _replace() described in issue #4377
# pylint: disable=invalid-name
from urllib import parse
parsed_url = parse.urlparse("http://www.this-is-weird.com")
sorted_query = parse.urlencode(
sorted(parse.parse_qsl(parsed_url.query),
key=lambda param: param[0])
)
new_parsed_url = parse.ParseResult._replace(parsed_url, query=sorted_query)
new_url = new_parsed_url.geturl() # This should not trigger a warning

new_parsed_url = parsed_url._replace(query=sorted_query)
new_url = new_parsed_url.geturl()

0 comments on commit d2a59ec

Please sign in to comment.