mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +00:00
[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.
This commit is contained in:
parent
bb44926ca5
commit
d94a78a134
1 changed files with 5 additions and 3 deletions
|
@ -458,12 +458,14 @@ fn symbol_by_id<'db>(
|
||||||
// a diagnostic if we see it being modified externally. In type inference, we
|
// 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
|
// 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`].
|
// do not have to call [`widen_type_for_undeclared_public_symbol`].
|
||||||
|
//
|
||||||
// `TYPE_CHECKING` is a special variable that should only be assigned `False`
|
// `TYPE_CHECKING` is a special variable that should only be assigned `False`
|
||||||
// at runtime, but is always considered `True` in type checking.
|
// at runtime, but is always considered `True` in type checking.
|
||||||
// See mdtest/known_constants.md#user-defined-type_checking for details.
|
// See mdtest/known_constants.md#user-defined-type_checking for details.
|
||||||
let is_considered_non_modifiable = symbol_table(db, scope).symbol(symbol_id).name()
|
let is_considered_non_modifiable = matches!(
|
||||||
== "__slots__"
|
symbol_table(db, scope).symbol(symbol_id).name().as_str(),
|
||||||
|| symbol_table(db, scope).symbol(symbol_id).name() == "TYPE_CHECKING";
|
"__slots__" | "TYPE_CHECKING"
|
||||||
|
);
|
||||||
|
|
||||||
widen_type_for_undeclared_public_symbol(db, inferred, is_considered_non_modifiable)
|
widen_type_for_undeclared_public_symbol(db, inferred, is_considered_non_modifiable)
|
||||||
.into()
|
.into()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue