From d94a78a134c438c063d144e11f2ae8c1657dfa99 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Wed, 5 Mar 2025 13:06:21 +0530 Subject: [PATCH] [red-knot] De-duplicate symbol table query (#16515) ## Summary This PR does a small refactor to avoid double `symbol_table(...).symbol(...)` call to check for `__slots__` and `TYPE_CHECKING`. It merges them into a single call. I noticed this while looking at https://github.com/astral-sh/ruff/pull/16468. --- crates/red_knot_python_semantic/src/symbol.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/red_knot_python_semantic/src/symbol.rs b/crates/red_knot_python_semantic/src/symbol.rs index 95669db13dc22..a6c2671bb22fa 100644 --- a/crates/red_knot_python_semantic/src/symbol.rs +++ b/crates/red_knot_python_semantic/src/symbol.rs @@ -458,12 +458,14 @@ fn symbol_by_id<'db>( // a diagnostic if we see it being modified externally. In type inference, we // can assign a "narrow" type to it even if it is not *declared*. This means, we // do not have to call [`widen_type_for_undeclared_public_symbol`]. + // // `TYPE_CHECKING` is a special variable that should only be assigned `False` // at runtime, but is always considered `True` in type checking. // See mdtest/known_constants.md#user-defined-type_checking for details. - let is_considered_non_modifiable = symbol_table(db, scope).symbol(symbol_id).name() - == "__slots__" - || symbol_table(db, scope).symbol(symbol_id).name() == "TYPE_CHECKING"; + let is_considered_non_modifiable = matches!( + symbol_table(db, scope).symbol(symbol_id).name().as_str(), + "__slots__" | "TYPE_CHECKING" + ); widen_type_for_undeclared_public_symbol(db, inferred, is_considered_non_modifiable) .into()