Skip to content

Commit 609e184

Browse files
authored
Stop defining __cached__ for Python 3.15 (#28476)
Summary -- This moves `__cached__` from the main list of `MAGIC_GLOBALS` to a version-specific list because it will be [removed](https://docs.python.org/3/reference/datamodel.html#module.__cached__) in 3.15. Test Plan -- A new mdtest for `undefined-name` (`F821`), which is just one of the rules affected but the easiest to test.
1 parent 859ff2f commit 609e184

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# `undefined-name` (`F821`)
2+
3+
## Module cache path in Python 3.14
4+
5+
```toml
6+
target-version = "py314"
7+
lint.select = ["F821"]
8+
```
9+
10+
The import system defines `__cached__` in module scope before Python 3.15.
11+
12+
```py
13+
print(__cached__)
14+
```
15+
16+
## Module cache path in Python 3.15
17+
18+
```toml
19+
target-version = "py315"
20+
lint.select = ["F821"]
21+
```
22+
23+
The import system no longer defines `__cached__` in Python 3.15, so `F821` reports uses without a
24+
definition.
25+
26+
```py
27+
print(__cached__) # error: [undefined-name] "Undefined name `__cached__`"
28+
```

‎crates/ruff_python_stdlib/src/builtins.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ const MAGIC_GLOBALS: &[&str] = &[
1919
"WindowsError",
2020
"__annotations__",
2121
"__builtins__",
22-
"__cached__",
2322
"__warningregistry__",
2423
"__file__",
2524
];
2625

26+
/// Magic globals that were removed in Python 3.15.
27+
const PRE_PY315_MAGIC_GLOBALS: &[&str] = &["__cached__"];
28+
2729
/// Magic globals that are only available starting in specific Python versions.
2830
///
2931
/// `__annotate__` was introduced in Python 3.14.
@@ -242,8 +244,15 @@ pub fn python_magic_globals(minor_version: u8) -> impl Iterator<Item = &'static
242244
None
243245
};
244246

247+
let pre_py315_magic_globals = if minor_version < 15 {
248+
Some(PRE_PY315_MAGIC_GLOBALS)
249+
} else {
250+
None
251+
};
252+
245253
py314_magic_globals
246254
.into_iter()
255+
.chain(pre_py315_magic_globals)
247256
.flatten()
248257
.chain(MAGIC_GLOBALS)
249258
.copied()

0 commit comments

Comments
 (0)