-
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
[pyflakes
] Allow forward references in class bases in stub files (F821
)
#10779
Conversation
|
Well, locally it seems to get rid of 74 F821 hits on typeshed :( Does the ecosystem check respect projects' |
To be more specific about why deferring all expressions in stub files seems trickier: here's a patch that does that: https://github.com/astral-sh/ruff/compare/main...AlexWaygood:ruff:f821-forward-refs-pyi?expand=1. And here's the impact it has on typeshed:
Looking more closely at those new errors this morning, however, I think most fall into two buckets:
So, perhaps if those two issues were resolved, then the patch that defers resolution of all expressions might be a more principled basis for a fix for this issue? |
It is also worth noting that, while this PR doesn't get us to the generalised support for forward references that type checkers have for stubs, the approach here is actually very similar to the monkey-patching flake8-pyi does. Flake8-pyi monkey-patches flake8 to do three things:
|
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.
This seems reasonable to me.
gah, why did I go make merge conflicts for myself? |
Although this doesn't directly match the behaviour of type checkers with regards to stub files, I think I will merge this for now:
|
…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.
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.