Skip to content

Fix a crash when nonlocal is defined at module level #8737

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 4 commits into from
Jun 6, 2023

Conversation

mbyrnepr2
Copy link
Member

@mbyrnepr2 mbyrnepr2 commented Jun 6, 2023

Type of Changes

Type
βœ“ πŸ› Bug fix
✨ New feature
πŸ”¨ Refactoring
πŸ“œ Docs

Description

Defining nonlocal at the module level is a SyntaxError.

Closes #8735

mbyrnepr2 added 2 commits June 6, 2023 14:53
Add a new checker ``nonlocal-defined-at-module-level`` when a ``nonlocal`` is defined at module-level.

Closes pylint-dev#8735
@jacobtylerwalls
Copy link
Member

jacobtylerwalls commented Jun 6, 2023

Thanks for picking this up! When I apply the crash fix, but without applying the new checker, I get this from linting the bug report:

a.py:1:0: E0117: nonlocal name X found without binding (nonlocal-without-binding)

Is that enough, without needing a new checker?

@jacobtylerwalls jacobtylerwalls added the Crash πŸ’₯ A bug that makes pylint crash label Jun 6, 2023
@jacobtylerwalls jacobtylerwalls added this to the 2.17.5 milestone Jun 6, 2023
@github-actions

This comment has been minimized.

@mbyrnepr2
Copy link
Member Author

Yeah we could also do that @jacobtylerwalls. My rationale is that the new checker correspond to a different Python output when running the code. But I understand if we want to keep things lean and just stick to the fix without the new checker.

Copy link
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked, and when doing this:

class A:
     nonlocal X

python still gives a SyntaxError, like so:

SyntaxError: no binding for nonlocal 'X' found

So I think we can just let the original message do the work.

@mbyrnepr2
Copy link
Member Author

mbyrnepr2 commented Jun 6, 2023

The rationale for the new checker was to distinguish the module-level definition from the function/class definition. Python gives different messages for these 2 distinct situations and I tried to reflect that here.

That said, we don’t necessarily need to mirror the Python messages if Pylint output is accurate enough.

@jacobtylerwalls
Copy link
Member

Ah, I didn't realize you were targeting the different wording in the two SyntaxErrors. (But yes, I think that's too subtle to justify adding a new check.)

@mbyrnepr2 mbyrnepr2 changed the title Fix crash and add new checker when nonlocal defined at module level Fix crash when nonlocal is defined at module level Jun 6, 2023
@mbyrnepr2 mbyrnepr2 changed the title Fix crash when nonlocal is defined at module level Fix a crash when nonlocal is defined at module level Jun 6, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jun 6, 2023

πŸ€– According to the primer, this change has no effect on the checked open source code. πŸ€–πŸŽ‰

This comment was generated for commit 82c31e5

@codecov
Copy link

codecov bot commented Jun 6, 2023

Codecov Report

Merging #8737 (82c31e5) into main (59194eb) will not change coverage.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #8737   +/-   ##
=======================================
  Coverage   95.82%   95.82%           
=======================================
  Files         173      173           
  Lines       18381    18381           
=======================================
  Hits        17613    17613           
  Misses        768      768           
Impacted Files Coverage Ξ”
pylint/checkers/variables.py 97.24% <100.00%> (ΓΈ)

@mbyrnepr2 mbyrnepr2 requested a review from jacobtylerwalls June 6, 2023 18:39
@mbyrnepr2 mbyrnepr2 merged commit 33d3f22 into pylint-dev:main Jun 6, 2023
@mbyrnepr2 mbyrnepr2 deleted the 8735_nonlocal_at_module_level branch June 6, 2023 18:57
github-actions bot pushed a commit that referenced this pull request 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 pull request 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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported Crash πŸ’₯ A bug that makes pylint crash
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Checking incorrect usage of the keyword 'nonlocal' results in fatal errors.
3 participants