Skip to content
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

new unused-private-member causes a crash #4755

Closed
stanislavlevin opened this issue Jul 27, 2021 · 4 comments · Fixed by #4783
Closed

new unused-private-member causes a crash #4755

stanislavlevin opened this issue Jul 27, 2021 · 4 comments · Fixed by #4783
Assignees
Labels
Bug 🪲 Crash 💥 A bug that makes pylint crash
Milestone

Comments

@stanislavlevin
Copy link
Contributor

Bug description

Given a file test.py:

# Please disable message unrelated to the bug
# pylint: disable=missing-docstring,too-few-public-methods,protected-access
class Context:
    def __init__(self):
        self.__messages = None

class Command:
    def __init__(self):
        self.context = Context()

    def method(self):
        self.context.__messages = []
        for message in self.context.__messages:
            print(message)

(Configuration)

No response

Command used

Result of pylint test.py:

************* Module test
test.py:4:8: W0238: Unused private member `Context.__messages` (unused-private-member)
Exception on node <ClassDef.Command l.6 at 0x7f4ee5bd05e0> in file '/usr/src/RPM/BUILD/test.py'
Traceback (most recent call last):
  File "/usr/bin/pylint.py3", line 33, in <module>
    sys.exit(load_entry_point('pylint==2.8.2', 'console_scripts', 'pylint')())
  File "/usr/src/.local/lib/python3/site-packages/pylint/__init__.py", line 24, in run_pylint
    PylintRun(sys.argv[1:])
  File "/usr/src/.local/lib/python3/site-packages/pylint/lint/run.py", line 384, in __init__
    linter.check(args)
  File "/usr/src/.local/lib/python3/site-packages/pylint/lint/pylinter.py", line 951, in check
    self._check_files(
  File "/usr/src/.local/lib/python3/site-packages/pylint/lint/pylinter.py", line 985, in _check_files
    self._check_file(get_ast, check_astroid_module, name, filepath, modname)
  File "/usr/src/.local/lib/python3/site-packages/pylint/lint/pylinter.py", line 1011, in _check_file
    check_astroid_module(ast_node)
  File "/usr/src/.local/lib/python3/site-packages/pylint/lint/pylinter.py", line 1145, in check_astroid_module
    retval = self._check_astroid_module(
  File "/usr/src/.local/lib/python3/site-packages/pylint/lint/pylinter.py", line 1190, in _check_astroid_module
    walker.walk(ast_node)
  File "/usr/src/.local/lib/python3/site-packages/pylint/utils/ast_walker.py", line 77, in walk
    self.walk(child)
  File "/usr/src/.local/lib/python3/site-packages/pylint/utils/ast_walker.py", line 79, in walk
    callback(astroid)
  File "/usr/src/.local/lib/python3/site-packages/pylint/checkers/classes.py", line 893, in leave_classdef
    self._check_unused_private_members(node)
  File "/usr/src/.local/lib/python3/site-packages/pylint/checkers/classes.py", line 953, in _check_unused_private_members
    and attribute.expr.name == "self"
AttributeError: 'Attribute' object has no attribute 'name'

(Expected behavior)

pylint shouldn't crash.

(Version affected)

pylint 2.9.0+ (including today's main).

(OS / Environment)

ALTLinux

(Additional dependencies)

No response

@stanislavlevin stanislavlevin added Bug 🪲 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jul 27, 2021
@stanislavlevin
Copy link
Contributor Author

traceback for today's main:

************* Module test
test.py:4:8: W0238: Unused private member `Context.__messages` (unused-private-member)
Exception on node <ClassDef.Command l.6 at 0x7feb211e4940> in file '/usr/src/RPM/BUILD/test.py'
Traceback (most recent call last):
  File "/usr/bin/pylint.py3", line 33, in <module>
    sys.exit(load_entry_point('pylint==2.8.2', 'console_scripts', 'pylint')())
  File "/usr/src/.local/lib/python3/site-packages/pylint/__init__.py", line 24, in run_pylint
    PylintRun(sys.argv[1:])
  File "/usr/src/.local/lib/python3/site-packages/pylint/lint/run.py", line 384, in __init__
    linter.check(args)
  File "/usr/src/.local/lib/python3/site-packages/pylint/lint/pylinter.py", line 975, in check
    self._check_files(
  File "/usr/src/.local/lib/python3/site-packages/pylint/lint/pylinter.py", line 1009, in _check_files
    self._check_file(get_ast, check_astroid_module, name, filepath, modname)
  File "/usr/src/.local/lib/python3/site-packages/pylint/lint/pylinter.py", line 1035, in _check_file
    check_astroid_module(ast_node)
  File "/usr/src/.local/lib/python3/site-packages/pylint/lint/pylinter.py", line 1172, in check_astroid_module
    retval = self._check_astroid_module(
  File "/usr/src/.local/lib/python3/site-packages/pylint/lint/pylinter.py", line 1217, in _check_astroid_module
    walker.walk(ast_node)
  File "/usr/src/.local/lib/python3/site-packages/pylint/utils/ast_walker.py", line 77, in walk
    self.walk(child)
  File "/usr/src/.local/lib/python3/site-packages/pylint/utils/ast_walker.py", line 79, in walk
    callback(astroid)
  File "/usr/src/.local/lib/python3/site-packages/pylint/checkers/classes.py", line 904, in leave_classdef
    self._check_unused_private_attributes(node)
  File "/usr/src/.local/lib/python3/site-packages/pylint/checkers/classes.py", line 983, in _check_unused_private_attributes
    assign_attr.expr.name == "cls"
AttributeError: 'Attribute' object has no attribute 'name'

@stanislavlevin
Copy link
Contributor Author

IMO, non-current node's private attributes should be skipped.

@Pierre-Sassoulas Pierre-Sassoulas added Crash 💥 A bug that makes pylint crash and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jul 27, 2021
@yushao2 yushao2 self-assigned this Aug 1, 2021
@yushao2
Copy link
Collaborator

yushao2 commented Aug 1, 2021

hi, looked into it and came up with a PR :) please take a look

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.10.0 milestone Aug 1, 2021
@stanislavlevin
Copy link
Contributor Author

The fix is verified. Thank you! (fixed + no regressions were found)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Crash 💥 A bug that makes pylint crash
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants