Skip to content

Fix #5569: Fix a crash in unused-private-member when type(self) used in bound methods #5662

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
merged 2 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ Release date: TBA
Closes #4798
Closes #5081

* Fix a crash in ``unused-private-member`` checker when analyzing code using
``type(self)`` in bound methods.

Closes #5569

* Pyreverse - add output in mermaidjs format

* ``used-before-assignment`` now considers that assignments in a try block
Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ Other Changes

Closes #4716

* Fix a crash in ``unused-private-member`` checker when analyzing code using
``type(self)`` in bound methods.

Closes #5569

* Fix crash in ``unnecessary-dict-index-lookup`` checker if the output of
``items()`` is assigned to a 1-tuple.

Expand Down
21 changes: 16 additions & 5 deletions pylint/checkers/classes/class_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,9 @@ def _check_unused_private_attributes(self, node: nodes.ClassDef) -> None:
if attribute.attrname != assign_attr.attrname:
continue

if self._is_type_self_call(attribute.expr):
continue

if (
assign_attr.expr.name
in {
Expand Down Expand Up @@ -2034,11 +2037,19 @@ def _is_mandatory_method_param(self, node):

Name is `self` for method, `cls` for classmethod and `mcs` for metaclass.
"""
return (
self._first_attrs
and isinstance(node, nodes.Name)
and node.name == self._first_attrs[-1]
)
if self._first_attrs:
first_attr = self._first_attrs[-1]
else:
# It's possible the function was already unregistered.
closest_func = utils.get_node_first_ancestor_of_type(
node, nodes.FunctionDef
)
if closest_func is None:
return False
if not closest_func.args.args:
return False
first_attr = closest_func.args.args[0].name
return isinstance(node, nodes.Name) and node.name == first_attr


def _ancestors_to_call(klass_node, method="__init__"):
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/u/unused/unused_private_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,13 @@ class Foo:

def method(self):
print(self.__class__.__ham)


class TypeSelfCallInMethod:
"""Regression test for issue 5569"""
@classmethod
def b(cls) -> None:
cls.__a = '' # [unused-private-member]

def a(self):
return type(self).__a
1 change: 1 addition & 0 deletions tests/functional/u/unused/unused_private_member.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ unused-private-member:243:12:243:50:FalsePositive4681.__init__:Unused private me
unused-private-member:274:4:275:26:FalsePositive4849.__unused_private_method:Unused private member `FalsePositive4849.__unused_private_method()`:UNDEFINED
unused-private-member:291:4:294:44:Pony.__init_defaults:Unused private member `Pony.__init_defaults(self)`:UNDEFINED
unused-private-member:296:4:298:20:Pony.__get_fur_color:Unused private member `Pony.__get_fur_color(self)`:UNDEFINED
unused-private-member:318:8:318:15:TypeSelfCallInMethod.b:Unused private member `TypeSelfCallInMethod.__a`:UNDEFINED