-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
F821 false positive for .pyi #3011
Comments
Surely this is correct though? At the time of declaration of |
No, it is allowed in stub files, this is taken from |
Ok. Fair enough, I know declarations in stub files can be out of order. I guess I'm wondering why the declarations cannot just be reordered? |
You are right even if it is allowed does not mean it should be done. |
I vote fixing it. |
Can you look into how |
This is done here. ❯ flake8 stubs/setuptools/pkg_resources/__init__.pyi --verbose
flake8.checker MainProcess 91 INFO Making checkers
flake8.bugbear MainProcess 107 INFO Optional warning B950 not present in selected warnings: None. Not firing it at all.
flake8.pyi MainProcess 119 INFO Replacing FlakesChecker with PyiAwareFlakesChecker while checking 'stubs/setuptools/pkg_resources/__init__.pyi'
flake8.main.application MainProcess 194 INFO Finished running
flake8.main.application MainProcess 194 INFO Reporting errors
flake8.main.application MainProcess 195 INFO Found a total of 209 violations and reported 0 |
Woah that's wild! |
Yeah, we should probably just change the behavior of |
https://beta.ruff.rs/docs/ https://github.com/charliermarsh/ruff Massively simplify configurations and speedup linting thanks to Ruff. Adds more autofixes too. Using `pathlib` instead of `os.path` is the modern way to go. However fixing this could introduce a bug if not careful. So I'm leaving it for later. Existing related Ruff requests (nothing here is a blocker, just future improvements): - astral-sh/ruff#1256 - astral-sh/ruff#3011 - astral-sh/ruff#3072 - astral-sh/ruff#3910 - astral-sh/ruff#2419 - astral-sh/ruff#3115 - astral-sh/ruff#1904
https://beta.ruff.rs/docs/ https://github.com/charliermarsh/ruff Massively simplify configurations and speedup linting thanks to Ruff. Adds more autofixes too. Using `pathlib` instead of `os.path` is the modern way to go. However fixing this could introduce a bug if not careful. So I'm leaving it for later. Existing related Ruff requests (nothing here is a blocker, just future improvements): - astral-sh/ruff#1256 - astral-sh/ruff#3011 - astral-sh/ruff#3072 - astral-sh/ruff#3910 - astral-sh/ruff#2419 - astral-sh/ruff#3115 - astral-sh/ruff#1904
https://beta.ruff.rs/docs/ https://github.com/charliermarsh/ruff Massively simplify configurations and speedup linting thanks to Ruff. Adds more autofixes too. Using `pathlib` instead of `os.path` is the modern way to go. However fixing this could introduce a bug if not careful. So I'm leaving it for later. Existing related Ruff requests (nothing here is a blocker, just future improvements): - astral-sh/ruff#1256 - astral-sh/ruff#3011 - astral-sh/ruff#3072 - astral-sh/ruff#3910 - astral-sh/ruff#2419 - astral-sh/ruff#3115 - astral-sh/ruff#1904
Hey! Are there any plans for this issue, now that flake8-pyi has been fully integrated? Thanks and have a great day! |
In #10341, I fixed the following F821 false positive on x: int
y = x # F821 previously incorrectly emitted here in a `.pyi` file But Ruff still emits an error on the original snippet in this issue if it's linting a class Distribution(IMetadataProvider): ... # F821 still emitted here currently due to the forward reference
class IMetadataProvider: ... However, with the latest version of flake8-monkeypatched-by-flake8-pyi, flake8 also emits an F821 error on that snippet. This is due to changes we made in flake8-pyi in PyCQA/flake8-pyi#364. The motivation for the flake8-pyi changes were:
I'm not sure that ruff should necessarily make the same decisions that flake8-pyi made, however:
Curious for @charliermarsh's opinion here! |
Thanks for the clear write-up. I think my preference would be...
|
I'd definitely prefer it if we emitted something on these constructs in a stub file -- type checkers don't flag them (since they're legal in So that implies (2). I'll start off by making a PR that stops ruff from emitting F821 on this kind of construct, and then look at implementing a new rule for this. Should the new rule go in the RUF ruleset? |
Sounds good. Yeah, unless we want to add it to |
…F821`) (#10779) ## Summary Fixes #3011. Type checkers currently allow forward references in all contexts in stub files, and stubs frequently make use of this capability (although it doesn't actually seem to be specc'd anywhere --neither in PEP 484, nor https://typing.readthedocs.io/en/latest/source/stubs.html#id6, nor the CPython typing docs). Implementing it so that Ruff allows forward references in _all contexts_ in stub files seems non-trivial, however (or at least, I couldn't figure out how to do it easily), so this PR does not do that. Perhaps it _should_; if we think this apporach isn't principled enough, I'm happy to close it and postpone changing anything here. However, this does reduce the number of F821 errors Ruff emits on typeshed down from 76 to 2, which would mean that we could enable the rule at typeshed. The remaining 2 F821 errors can be trivially fixed at typeshed by moving definitions around; forward references in class bases were really the only remaining places where there was a real _use case_ for forward references in stub files that Ruff wasn't yet allowing. ## Test plan `cargo test`. I also ran this PR branch on typeshed to check to see if there were any new false positives caused by the changes here; there were none.
…F821`) (astral-sh#10779) ## Summary Fixes astral-sh#3011. Type checkers currently allow forward references in all contexts in stub files, and stubs frequently make use of this capability (although it doesn't actually seem to be specc'd anywhere --neither in PEP 484, nor https://typing.readthedocs.io/en/latest/source/stubs.html#id6, nor the CPython typing docs). Implementing it so that Ruff allows forward references in _all contexts_ in stub files seems non-trivial, however (or at least, I couldn't figure out how to do it easily), so this PR does not do that. Perhaps it _should_; if we think this apporach isn't principled enough, I'm happy to close it and postpone changing anything here. However, this does reduce the number of F821 errors Ruff emits on typeshed down from 76 to 2, which would mean that we could enable the rule at typeshed. The remaining 2 F821 errors can be trivially fixed at typeshed by moving definitions around; forward references in class bases were really the only remaining places where there was a real _use case_ for forward references in stub files that Ruff wasn't yet allowing. ## Test plan `cargo test`. I also ran this PR branch on typeshed to check to see if there were any new false positives caused by the changes here; there were none.
ruff
detects theF821
whileflake8
withflake8-pyi
does not (flake8-pyi
must be installed otherwise it will also detect an error).Link to the playground
The text was updated successfully, but these errors were encountered: