From 1ccb31f12dd8a702acbec92960e6e47396de2221 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 16 Aug 2024 15:08:27 +0100 Subject: [PATCH] [red-knot] Use `Unknown` rather than `Unbound` for unresolved imports --- crates/red_knot_python_semantic/src/types.rs | 6 +++++- crates/red_knot_python_semantic/src/types/infer.rs | 10 +++++----- crates/red_knot_workspace/src/lint.rs | 4 ++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index e6f739df3a0096..b59d7a7f2513d3 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -113,7 +113,7 @@ pub enum Type<'db> { Any, /// the empty set of values Never, - /// unknown type (no annotation) + /// unknown type (either no annotation, or some kind of type error) /// equivalent to Any, or possibly to object in strict mode Unknown, /// name does not exist or is not bound to any value (this represents an error, but with some @@ -145,6 +145,10 @@ impl<'db> Type<'db> { matches!(self, Type::Unbound) } + pub const fn is_unknown(&self) -> bool { + matches!(self, Type::Unknown) + } + pub const fn is_never(&self) -> bool { matches!(self, Type::Never) } diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index 6f494f9c6bf963..7d476ea028e29e 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -949,7 +949,7 @@ impl<'db> TypeInferenceBuilder<'db> { fn module_ty_from_name(&self, module_name: Option) -> Type<'db> { module_name .and_then(|module_name| resolve_module(self.db, module_name)) - .map_or(Type::Unbound, |module| Type::Module(module.file())) + .map_or(Type::Unknown, |module| Type::Module(module.file())) } fn infer_decorator(&mut self, decorator: &ast::Decorator) -> Type<'db> { @@ -1783,7 +1783,7 @@ mod tests { ("src/package/bar.py", "from .foo import X"), ])?; - assert_public_ty(&db, "src/package/bar.py", "X", "Unbound"); + assert_public_ty(&db, "src/package/bar.py", "X", "Unknown"); Ok(()) } @@ -1821,7 +1821,7 @@ mod tests { fn follow_nonexistent_relative_import_bare_to_package() -> anyhow::Result<()> { let mut db = setup_db(); db.write_files([("src/package/bar.py", "from . import X")])?; - assert_public_ty(&db, "src/package/bar.py", "X", "Unbound"); + assert_public_ty(&db, "src/package/bar.py", "X", "Unknown"); Ok(()) } @@ -1851,7 +1851,7 @@ mod tests { ("src/package/bar.py", "from . import foo"), ])?; - assert_public_ty(&db, "src/package/bar.py", "foo", "Unbound"); + assert_public_ty(&db, "src/package/bar.py", "foo", "Unknown"); Ok(()) } @@ -1874,7 +1874,7 @@ mod tests { fn follow_nonexistent_relative_import_from_dunder_init() -> anyhow::Result<()> { let mut db = setup_db(); db.write_files([("src/package/__init__.py", "from .foo import X")])?; - assert_public_ty(&db, "src/package/__init__.py", "X", "Unbound"); + assert_public_ty(&db, "src/package/__init__.py", "X", "Unknown"); Ok(()) } diff --git a/crates/red_knot_workspace/src/lint.rs b/crates/red_knot_workspace/src/lint.rs index ba8b3e5b19be10..8aea5722e39081 100644 --- a/crates/red_knot_workspace/src/lint.rs +++ b/crates/red_knot_workspace/src/lint.rs @@ -129,7 +129,7 @@ fn lint_unresolved_imports(context: &SemanticLintContext, import: AnyImportRef) for alias in &import.names { let ty = alias.ty(&context.semantic); - if ty.is_unbound() { + if ty.is_unknown() { context.push_diagnostic(format_diagnostic( context, &format!("Unresolved import '{}'", &alias.name), @@ -142,7 +142,7 @@ fn lint_unresolved_imports(context: &SemanticLintContext, import: AnyImportRef) for alias in &import.names { let ty = alias.ty(&context.semantic); - if ty.is_unbound() { + if ty.is_unknown() { context.push_diagnostic(format_diagnostic( context, &format!("Unresolved import '{}'", &alias.name),