[pyflakes] Fix false positives for __annotate__ (Py3.14+) and __warningregistry__ (F821) (#20154)

## Summary

Fixes #19970

---------

Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
This commit is contained in:
Dan Parizher 2025-09-23 08:16:00 -04:00 committed by GitHub
parent 742f8a4ee6
commit 346842f003
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 153 additions and 28 deletions

View file

@ -0,0 +1,31 @@
# `__annotate__` as an implicit global is version-gated (Py3.14+)
## Absent before 3.14
`__annotate__` is never present in the global namespace on Python \<3.14.
```toml
[environment]
python-version = "3.13"
```
```py
# error: [unresolved-reference]
reveal_type(__annotate__) # revealed: Unknown
```
## Present in 3.14+
The `__annotate__` global may be present in Python 3.14, but only if at least one global symbol in
the module is annotated (e.g. `x: int` or `x: int = 42`). Currently we model `__annotate__` as
always being possibly unbound on Python 3.14+.
```toml
[environment]
python-version = "3.14"
```
```py
# error: [possibly-unresolved-reference]
reveal_type(__annotate__) # revealed: (format: int, /) -> dict[str, Any]
```

View file

@ -16,6 +16,8 @@ reveal_type(__doc__) # revealed: str | None
reveal_type(__spec__) # revealed: ModuleSpec | None
reveal_type(__path__) # revealed: MutableSequence[str]
reveal_type(__builtins__) # revealed: Any
# error: [possibly-unresolved-reference] "Name `__warningregistry__` used when possibly not defined"
reveal_type(__warningregistry__) # revealed: dict[Any, int]
import sys
@ -75,6 +77,8 @@ reveal_type(module.__file__) # revealed: Unknown | None
reveal_type(module.__path__) # revealed: list[str]
reveal_type(module.__doc__) # revealed: Unknown
reveal_type(module.__spec__) # revealed: Unknown | ModuleSpec | None
# error: [unresolved-attribute]
reveal_type(module.__warningregistry__) # revealed: Unknown
def nested_scope():
global __loader__