Skip to content

Commit

Permalink
[red-knot] De-duplicate symbol table query (astral-sh#16515)
Browse files Browse the repository at this point in the history
## 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
astral-sh#16468.
  • Loading branch information
dhruvmanila authored Mar 5, 2025
1 parent bb44926 commit d94a78a
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions crates/red_knot_python_semantic/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d94a78a

Please sign in to comment.