[pyflakes] Allow forward references in class bases in stub files (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.
This commit is contained in:
Alex Waygood 2024-04-07 01:15:58 +01:00 committed by GitHub
parent 86588695e3
commit 2a51dcfdf7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 88 additions and 1 deletions

View file

@ -1608,6 +1608,20 @@ impl<'a> SemanticModel<'a> {
.intersects(SemanticModelFlags::DUNDER_ALL_DEFINITION)
}
/// Return `true` if the model is visiting an item in a class's bases tuple
/// (e.g. `Foo` in `class Bar(Foo): ...`)
pub const fn in_class_base(&self) -> bool {
self.flags.intersects(SemanticModelFlags::CLASS_BASE)
}
/// Return `true` if the model is visiting an item in a class's bases tuple
/// that was initially deferred while traversing the AST.
/// (This only happens in stub files.)
pub const fn in_deferred_class_base(&self) -> bool {
self.flags
.intersects(SemanticModelFlags::DEFERRED_CLASS_BASE)
}
/// Return an iterator over all bindings shadowed by the given [`BindingId`], within the
/// containing scope, and across scopes.
pub fn shadowed_bindings(
@ -2021,6 +2035,20 @@ bitflags! {
/// ```
const F_STRING_REPLACEMENT_FIELD = 1 << 23;
/// The model is visiting the bases tuple of a class.
///
/// For example, the model could be visiting `Foo` or `Bar` in:
///
/// ```python
/// class Baz(Foo, Bar):
/// pass
/// ```
const CLASS_BASE = 1 << 24;
/// The model is visiting a class base that was initially deferred
/// while traversing the AST. (This only happens in stub files.)
const DEFERRED_CLASS_BASE = 1 << 25;
/// The context is in any type annotation.
const ANNOTATION = Self::TYPING_ONLY_ANNOTATION.bits() | Self::RUNTIME_EVALUATED_ANNOTATION.bits() | Self::RUNTIME_REQUIRED_ANNOTATION.bits();