ruff/crates
Brent Westbrook bc7274d148
Add a ScopeKind for the __class__ cell (#20048)
Summary
--

This PR aims to resolve (or help to resolve) #18442 and #19357 by
encoding the CPython semantics around the `__class__` cell in our
semantic model. Namely,

> `__class__` is an implicit closure reference created by the compiler
if any methods in a class body refer to either `__class__` or super.

from the Python
[docs](https://docs.python.org/3/reference/datamodel.html#creating-the-class-object).

As noted in the variant docs by @AlexWaygood, we don't fully model this
behavior, opting always to create the `__class__` cell binding in a new
`ScopeKind::DunderClassCell` around each method definition, without
checking if any method in the class body actually refers to `__class__`
or `super`.

As such, this PR fixes #18442 but not #19357.

Test Plan
--

Existing tests, plus the tests from #19783, which now pass without any
rule-specific code.

Note that we opted not to alter the behavior of F841 here because
flagging `__class__` in these cases still seems helpful. See the
discussion in
https://github.com/astral-sh/ruff/pull/20048#discussion_r2296252395 and
in the test comments for more information.

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Mikko Leppänen <mleppan23@gmail.com>
2025-08-26 09:49:08 -04:00
..
ruff Bump 0.12.10 (#20025) 2025-08-21 13:09:31 -05:00
ruff_annotate_snippets Move full diagnostic rendering to ruff_db (#19415) 2025-08-08 12:56:23 -04:00
ruff_benchmark Feature/build riscv64 bin (#19819) 2025-08-14 16:11:14 +02:00
ruff_cache Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
ruff_db Improve diff rendering for notebooks (#20036) 2025-08-25 09:20:42 -04:00
ruff_dev [ty] Remove duplicate global lint registry (#20053) 2025-08-22 19:43:12 -04:00
ruff_diagnostics Fix rust feature activation (#20012) 2025-08-21 09:26:06 +02:00
ruff_formatter Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_graph [ty] Remove KnownModule::is_enum (#19681) 2025-08-01 10:31:12 +02:00
ruff_index Update Rust toolchain to 1.88 and MSRV to 1.86 (#19011) 2025-06-28 20:24:00 +02:00
ruff_linter Add a ScopeKind for the __class__ cell (#20048) 2025-08-26 09:49:08 -04:00
ruff_macros Don't cache files with diagnostics (#19869) 2025-08-12 15:28:44 -04:00
ruff_memory_usage [ty] Track heap usage of salsa structs (#19790) 2025-08-12 13:28:44 +02:00
ruff_notebook Improve diff rendering for notebooks (#20036) 2025-08-25 09:20:42 -04:00
ruff_options_metadata Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_python_ast [ty] Shrink size of AstNodeRef (#20028) 2025-08-22 17:03:22 -04:00
ruff_python_ast_integration_tests Disallow implicit concatenation of t-strings and other string types (#19485) 2025-07-27 12:41:03 +00:00
ruff_python_codegen Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_python_formatter [ty] Shrink size of AstNodeRef (#20028) 2025-08-22 17:03:22 -04:00
ruff_python_index Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
ruff_python_literal Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
ruff_python_parser [ty] Shrink size of AstNodeRef (#20028) 2025-08-22 17:03:22 -04:00
ruff_python_semantic Add a ScopeKind for the __class__ cell (#20048) 2025-08-26 09:49:08 -04:00
ruff_python_stdlib Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
ruff_python_trivia Treat ty: comments as pragma comments (#18532) 2025-06-07 16:02:43 +02:00
ruff_python_trivia_integration_tests Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
ruff_server Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_source_file Move diff rendering to ruff_db (#20006) 2025-08-21 09:47:00 -04:00
ruff_text_size [ty] Fix a few more diagnostic differences from Ruff (#19806) 2025-08-08 11:31:19 -04:00
ruff_wasm Bump 0.12.10 (#20025) 2025-08-21 13:09:31 -05:00
ruff_workspace Fix rust feature activation (#20012) 2025-08-21 09:26:06 +02:00
ty [ty] Add support for PEP 800 (#20084) 2025-08-25 19:39:05 +01:00
ty_combine [ty] Disallow std::env and io methods in most ty crates (#20046) 2025-08-22 11:13:47 -07:00
ty_ide [ty] Refactor inlay hints structure to use separate parts (#20052) 2025-08-26 10:21:31 +05:30
ty_project [ty] Remove duplicate global lint registry (#20053) 2025-08-22 19:43:12 -04:00
ty_python_semantic [ty] Add support for PEP 750 t-strings (#20085) 2025-08-25 18:49:49 +00:00
ty_server [ty] Refactor inlay hints structure to use separate parts (#20052) 2025-08-26 10:21:31 +05:30
ty_static [ty] Disallow std::env and io methods in most ty crates (#20046) 2025-08-22 11:13:47 -07:00
ty_test [ty] Disallow std::env and io methods in most ty crates (#20046) 2025-08-22 11:13:47 -07:00
ty_vendored [ty] Sync vendored typeshed stubs (#20083) 2025-08-25 17:01:51 +00:00
ty_wasm [ty] Refactor inlay hints structure to use separate parts (#20052) 2025-08-26 10:21:31 +05:30