Skip to content

Commit

Permalink
red_knot: first attempt at string annotation
Browse files Browse the repository at this point in the history
This separates the inference of string annotations in the deferred
region. But, this creates complications in annotations that are only
partially stringified e.g., `tuple[int, "Foo"]` where "Foo" is a forward
reference.

This commit exists so as to create a checkpoint in case some of the
ideas explored here are useful.
  • Loading branch information
dhruvmanila committed Nov 13, 2024
1 parent 5c548dc commit 6217f48
Show file tree
Hide file tree
Showing 5 changed files with 350 additions and 44 deletions.
1 change: 1 addition & 0 deletions crates/red_knot_python_semantic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ license = { workspace = true }
ruff_db = { workspace = true }
ruff_index = { workspace = true }
ruff_python_ast = { workspace = true, features = ["salsa"] }
ruff_python_parser = { workspace = true }
ruff_python_stdlib = { workspace = true }
ruff_source_file = { workspace = true }
ruff_text_size = { workspace = true }
Expand Down
30 changes: 27 additions & 3 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod display;
mod infer;
mod mro;
mod narrow;
mod string_annotation;
mod unpacker;

#[salsa::tracked(return_ref)]
Expand All @@ -56,7 +57,7 @@ pub fn check_types(db: &dyn Db, file: File) -> TypeCheckDiagnostics {

/// Infer the public type of a symbol (its type as seen from outside its scope).
fn symbol_by_id<'db>(db: &'db dyn Db, scope: ScopeId<'db>, symbol: ScopedSymbolId) -> Symbol<'db> {
let _span = tracing::trace_span!("symbol_ty_by_id", ?symbol).entered();
let _span = tracing::trace_span!("symbol_by_id", ?symbol).entered();

let use_def = use_def_map(db, scope);

Expand Down Expand Up @@ -180,13 +181,21 @@ pub(crate) fn global_symbol<'db>(db: &'db dyn Db, file: File, name: &str) -> Sym
/// Infer the type of a binding.
pub(crate) fn binding_ty<'db>(db: &'db dyn Db, definition: Definition<'db>) -> Type<'db> {
let inference = infer_definition_types(db, definition);
inference.binding_ty(definition)
if inference.is_eagerly_deferred(definition) {
infer_deferred_types(db, definition).binding_ty(definition)
} else {
inference.binding_ty(definition)
}
}

/// Infer the type of a declaration.
fn declaration_ty<'db>(db: &'db dyn Db, definition: Definition<'db>) -> Type<'db> {
let inference = infer_definition_types(db, definition);
inference.declaration_ty(definition)
if inference.is_eagerly_deferred(definition) {
infer_deferred_types(db, definition).declaration_ty(definition)
} else {
inference.declaration_ty(definition)
}
}

/// Infer the type of a (possibly deferred) sub-expression of a [`Definition`].
Expand Down Expand Up @@ -2220,6 +2229,21 @@ impl<'db> IterationOutcome<'db> {
}
}

#[derive(Debug)]
enum MaybeDeferred<'db> {
Type(Type<'db>),
Deferred,
}

impl<'db> MaybeDeferred<'db> {
fn expect_type(self) -> Type<'db> {
match self {
MaybeDeferred::Type(ty) => ty,
MaybeDeferred::Deferred => panic!("expected a type, but got a deferred annotation"),
}
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Truthiness {
/// For an object `x`, `bool(x)` will always return `True`
Expand Down
Loading

0 comments on commit 6217f48

Please sign in to comment.