Skip to content

Commit

Permalink
Experiment: cache retrieval of NoneType symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Nov 4, 2024
1 parent c7d4bdb commit 503aa46
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 5 additions & 0 deletions crates/red_knot_python_semantic/src/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ pub(crate) fn typeshed_symbol<'db>(db: &'db dyn Db, symbol: &str) -> Symbol<'db>
core_module_symbol(db, CoreStdlibModule::Typeshed, symbol)
}

#[salsa::tracked]
pub(crate) fn typeshed_symbol_none_type<'db>(db: &'db dyn Db) -> Symbol<'db> {
typeshed_symbol(db, "NoneType")
}

/// Lookup the type of `symbol` in the `typing_extensions` module namespace.
///
/// Returns `Symbol::Unbound` if the `typing_extensions` module isn't available for some reason.
Expand Down
4 changes: 2 additions & 2 deletions crates/red_knot_python_semantic/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
Db,
};

#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Boundness {
Bound,
MayBeUnbound,
Expand All @@ -26,7 +26,7 @@ pub(crate) enum Boundness {
/// maybe_unbound: Symbol::Type(Type::IntLiteral(2), Boundness::MayBeUnbound),
/// non_existent: Symbol::Unbound,
/// ```
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Symbol<'db> {
Type(Type<'db>, Boundness),
Unbound,
Expand Down
6 changes: 4 additions & 2 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use crate::semantic_index::{
global_scope, semantic_index, symbol_table, use_def_map, BindingWithConstraints,
BindingWithConstraintsIterator, DeclarationsIterator,
};
use crate::stdlib::{builtins_symbol, types_symbol, typeshed_symbol, typing_extensions_symbol};
use crate::stdlib::{
builtins_symbol, types_symbol, typeshed_symbol_none_type, typing_extensions_symbol,
};
use crate::symbol::{Boundness, Symbol};
use crate::types::diagnostic::TypeCheckDiagnosticsBuilder;
use crate::types::narrow::narrowing_constraint;
Expand Down Expand Up @@ -1340,7 +1342,7 @@ impl<'db> KnownClass {
types_symbol(db, self.as_str()).unwrap_or_unknown()
}

Self::NoneType => typeshed_symbol(db, self.as_str()).unwrap_or_unknown(),
Self::NoneType => typeshed_symbol_none_type(db).unwrap_or_unknown(),
}
}

Expand Down

0 comments on commit 503aa46

Please sign in to comment.