Skip to content

Fix #1555: Fix false negatives for no-member from self-referencing assignments #5544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ Release date: TBA
..
Put new features here and also in 'doc/whatsnew/2.14.rst'

* Fix false negative for ``no-member`` when attempting to assign an instance
attribute to itself without any prior assignment.

Closes #1555

..
Insert your changelog randomly, it will reduce merge conflicts
Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/2.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ Extensions

Other Changes
=============

* Fix false negative for ``no-member`` when attempting to assign an instance
attribute to itself without any prior assignment.

Closes #1555
28 changes: 19 additions & 9 deletions pylint/checkers/typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,15 +1007,7 @@ def visit_attribute(self, node: nodes.Attribute) -> None:
return

try:
if not [
n
for n in owner.getattr(node.attrname)
if not isinstance(n.statement(future=True), nodes.AugAssign)
]:
missingattr.add((owner, name))
continue
except astroid.exceptions.StatementMissing:
continue
attr_nodes = owner.getattr(node.attrname)
except AttributeError:
continue
except astroid.DuplicateBasesError:
Expand All @@ -1039,6 +1031,24 @@ def visit_attribute(self, node: nodes.Attribute) -> None:
continue
missingattr.add((owner, name))
continue
else:
for attr_node in attr_nodes:
attr_parent = attr_node.parent
# Skip augmented assignments
try:
if isinstance(
attr_node.statement(future=True), nodes.AugAssign
):
continue
except astroid.exceptions.StatementMissing:
break
# Skip self-referencing assignments
if attr_parent is node.parent:
continue
break
else:
missingattr.add((owner, name))
continue
# stop on the first found
break
else:
Expand Down
37 changes: 37 additions & 0 deletions tests/functional/n/no/no_member_assign_same_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Tests for no-member for self-referencing instance attributes
See https://github.com/PyCQA/pylint/issues/1555
"""
# pylint: disable=too-few-public-methods


class ClassWithMember:
"""Member defined in superclass."""
def __init__(self):
self.member = True


class AssignMemberInSameLine:
"""This class attempts to assign and access a member in the same line."""
def __init__(self):
self.member = self.member # [no-member]


class AssignMemberInSameLineAfterTypeAnnotation:
"""This might emit a message like `maybe-no-member` in the future."""
def __init__(self):
self.member: bool
self.member = self.member


class AssignMemberFromSuper1(ClassWithMember):
"""This assignment is valid due to inheritance."""
def __init__(self):
self.member = self.member
super().__init__()


class AssignMemberFromSuper2(ClassWithMember):
"""This assignment is valid due to inheritance."""
def __init__(self):
super().__init__()
self.member = self.member
1 change: 1 addition & 0 deletions tests/functional/n/no/no_member_assign_same_line.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no-member:16:22:16:33:AssignMemberInSameLine.__init__:Instance of 'AssignMemberInSameLine' has no 'member' member:INFERENCE