[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

@ -20,9 +20,15 @@ pub const MAGIC_GLOBALS: &[&str] = &[
"__annotations__",
"__builtins__",
"__cached__",
"__warningregistry__",
"__file__",
];
/// Magic globals that are only available starting in specific Python versions.
///
/// `__annotate__` was introduced in Python 3.14.
static PY314_PLUS_MAGIC_GLOBALS: &[&str] = &["__annotate__"];
static ALWAYS_AVAILABLE_BUILTINS: &[&str] = &[
"ArithmeticError",
"AssertionError",
@ -216,6 +222,21 @@ pub fn python_builtins(minor_version: u8, is_notebook: bool) -> impl Iterator<It
.copied()
}
/// Return the list of magic globals for the given Python minor version.
pub fn python_magic_globals(minor_version: u8) -> impl Iterator<Item = &'static str> {
let py314_magic_globals = if minor_version >= 14 {
Some(PY314_PLUS_MAGIC_GLOBALS)
} else {
None
};
py314_magic_globals
.into_iter()
.flatten()
.chain(MAGIC_GLOBALS)
.copied()
}
/// Returns `true` if the given name is that of a Python builtin.
///
/// Intended to be kept in sync with [`python_builtins`].