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

Checking incorrect usage of the keyword 'nonlocal' results in fatal errors. #8735

Closed
xiaxinmeng opened this issue Jun 6, 2023 · 5 comments · Fixed by #8737
Closed

Checking incorrect usage of the keyword 'nonlocal' results in fatal errors. #8735

xiaxinmeng opened this issue Jun 6, 2023 · 5 comments · Fixed by #8737
Labels
Crash 💥 A bug that makes pylint crash Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@xiaxinmeng
Copy link

xiaxinmeng commented Jun 6, 2023

If a 'nonlocal' variable are assigned with a variable outside a function, Pylint fails to handle it. See the following example.

example.py:

nonlocal X
X = whatever

The expected output:

There is no a fatal error.

The actual output:

>> pylint example.py
************* Module pylint_example
Desktop/pylint_example.py:1:0: C0114: Missing module docstring (missing-module-docstring)
Desktop/pylint_example.py:3:0: E0117: nonlocal name X found without binding (nonlocal-without-binding)
Exception on node <Assign l.4 at 0x7fdb12c04a90> in file '/home/xxm/Desktop/pylint_example.py'
Traceback (most recent call last):
  File "/home/xxm/.local/lib/python3.11/site-packages/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "/home/xxm/.local/lib/python3.11/site-packages/pylint/checkers/variables.py", line 2043, in visit_assign
    self._check_self_cls_assign(node)
  File "/home/xxm/.local/lib/python3.11/site-packages/pylint/checkers/variables.py", line 2920, in _check_self_cls_assign
    scope = node.scope().parent.scope()
            ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'scope'
Desktop/empirical_type_checker/bug_report/pylint_example.py:1:0: F0002: /home/xxm/Desktop/pylint_example.py: Fatal error while checking '/home/xxm/Desktop/pylint_example.py'. Please open an issue in our bug tracker so we address this. There is a pre-filled template that you can use in '/home/xxm/.cache/pylint/pylint-crash-2023-06-06-15-56-31.txt'. (astroid-error)

------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00)

Test Environment:

Ubuntu 18.04,
CPython 3.11.3
Pylint: 3.0.0b1
astroid 3.0.0a3

@Pierre-Sassoulas Pierre-Sassoulas added Crash 💥 A bug that makes pylint crash Needs PR This issue is accepted, sufficiently specified and now needs an implementation labels Jun 6, 2023
@Pierre-Sassoulas
Copy link
Member

Thank you for detecting the bug and opening the issue!

@jacobtylerwalls jacobtylerwalls added this to the 2.17.5 milestone Jun 6, 2023
mbyrnepr2 added a commit to mbyrnepr2/pylint that referenced this issue Jun 6, 2023
Add a new checker ``nonlocal-defined-at-module-level`` when a ``nonlocal`` is defined at module-level.

Closes pylint-dev#8735
@xiaxinmeng
Copy link
Author

Thanks, I hope these reported bugs are helpful for the development and maintenance of pylint.

@nickdrozd
Copy link
Collaborator

This is a bug that would have been revealed with better type checking. I doubt it is the only place where the code blows past the possibility of a null scope.

@jacobtylerwalls
Copy link
Member

@nickdrozd while on the subject of type checking, do you have a view on whether we should move forward with pylint-dev/astroid#2167 or pylint-dev/astroid#2171? We're thinking of taking one of the two approaches to avoid assigning to methods, which is cluttering a lot of the mypy output now. If it would help for me to write up an astroid issue to summarize the state of the discussion, I'll gladly do it.

@nickdrozd
Copy link
Collaborator

The inference module is pretty gnarly, and it's also responsible for a lot of downstream gnarliness, so I'm in favor of obliterating it along the lines of pylint-dev/astroid#2171.

mbyrnepr2 added a commit that referenced this issue Jun 6, 2023
* Fix a crash when a ``nonlocal`` is defined at module-level.

Closes #8735
github-actions bot pushed a commit that referenced this issue Jun 6, 2023
* Fix a crash when a ``nonlocal`` is defined at module-level.

Closes #8735

(cherry picked from commit 33d3f22)
Pierre-Sassoulas pushed a commit that referenced this issue Jun 6, 2023
* Fix a crash when a ``nonlocal`` is defined at module-level.

Closes #8735

(cherry picked from commit 33d3f22)

Co-authored-by: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Crash 💥 A bug that makes pylint crash Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants