-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[ty] lint on the global
keyword if there's no explicit definition in the…
#19344
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
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! This looks pretty good to me. I haven't gone through the mypy_primer hits, but it doesn't look prohibitively large to me.
The main thing I think we need to do before landing this is to improve the diagnostic to make it clearer what the problem is, and why we object to this coding pattern
|
Lint rule | Added | Removed | Changed |
---|---|---|---|
unresolved-reference |
39 | 0 | 0 |
Total | 39 | 0 | 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! (Haven't reviewed the mypy_primer hits other than the quantity -- I'm trusting you to check that they're all as expected!)
Most of the new lints in the ecosystem are as expected, class CounterTests(unittest.TestCase):
def setUp(self):
global Counter
from Crypto.Util import Counter So that's doing a global import, but not in the global scope. If the user is sure they want to do this sort of thing, is there an alternative way for them to do it that will type-check? (Maybe something better than |
7ae687d
to
d6d4914
Compare
Ok, folks who want to do |
…al` lints This is a follow-up to #19344 that improves the error formatting slightly. For example with this program: ```py def f(): global foo, bar ``` Before we printed: ``` 1 | def f(): 2 | global foo, bar | ^^^^^^^^^^^^^^^ `foo` has no declarations or bindings in the global scope ... 1 | def f(): 2 | global foo, bar | ^^^^^^^^^^^^^^^ `bar` has no declarations or bindings in the global scope ``` Now we print: ``` 1 | def f(): 2 | global foo, bar | ^^^ `foo` has no declarations or bindings in the global scope ... 1 | def f(): 2 | global foo, bar | ^^^ `bar` has no declarations or bindings in the global scope ```
…`global` lints This is a follow-up to #19344 that improves the error formatting slightly. For example with this program: ```py def f(): global foo, bar ``` Before we printed: ``` 1 | def f(): 2 | global foo, bar | ^^^^^^^^^^^^^^^ `foo` has no declarations or bindings in the global scope ... 1 | def f(): 2 | global foo, bar | ^^^^^^^^^^^^^^^ `bar` has no declarations or bindings in the global scope ``` Now we print: ``` 1 | def f(): 2 | global foo, bar | ^^^ `foo` has no declarations or bindings in the global scope ... 1 | def f(): 2 | global foo, bar | ^^^ `bar` has no declarations or bindings in the global scope ```
…`global` lints (#19379) This is a follow-up to #19344 that improves the error formatting slightly. For example with this program: ```py def f(): global foo, bar ``` Before we printed: ``` 1 | def f(): 2 | global foo, bar | ^^^^^^^^^^^^^^^ `foo` has no declarations or bindings in the global scope ... 1 | def f(): 2 | global foo, bar | ^^^^^^^^^^^^^^^ `bar` has no declarations or bindings in the global scope ``` Now we print: ``` 1 | def f(): 2 | global foo, bar | ^^^ `foo` has no declarations or bindings in the global scope ... 1 | def f(): 2 | global foo, bar | ^^^ `bar` has no declarations or bindings in the global scope ``` --------- Co-authored-by: Micha Reiser <[email protected]>
We currently lint on examples like this (even though it's allowed at runtime, as long as you call
f
beforeg
):However, linting only on the
print
is kind of confusing. This PR adds a lint on theglobal
statement itself (edit: updated after review feedback):Because this is legal Python, I expect this is going to have a lot of new hits in the
ecosystem-analyzer
(and I had to fix some of our mdtests). I don't know what would count as an "excessive" number of new failures, though, and I'll need advice from reviewers :)