From d30649c66c89883e814b68e51d6f7c5bb46c6d4f Mon Sep 17 00:00:00 2001 From: Glyphack Date: Mon, 30 Dec 2024 12:41:47 +0100 Subject: [PATCH] Infer Type::Unknown for a variable with value `...` --- .../resources/mdtest/stubs/ellipsis.md | 4 +++- crates/red_knot_python_semantic/src/types/infer.rs | 11 ++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md b/crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md index 645569427dc3b9..dee9849257ad1a 100644 --- a/crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md +++ b/crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md @@ -20,10 +20,12 @@ stub file only. ```py path=test.pyi y: bytes = ... reveal_type(y) # revealed: bytes +x = ... +reveal_type(x) # revealed: Unknown class Foo: y: int = ... - + reveal_type(Foo.y) # revealed: int ``` diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index 8cb6c0039db4c4..63f1fe231c4acf 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -435,6 +435,11 @@ impl<'db> TypeInferenceBuilder<'db> { matches!(self.region, InferenceRegion::Deferred(_)) || self.deferred_state.is_deferred() } + /// Are we currently inferring types in a stub file? + fn is_stub(&self) -> bool { + return self.file().is_stub(self.db().upcast()); + } + /// Get the already-inferred type of an expression node. /// /// ## Panics @@ -1174,7 +1179,7 @@ impl<'db> TypeInferenceBuilder<'db> { let inferred_ty = if let Some(default_ty) = default_ty { if default_ty.is_assignable_to(self.db(), declared_ty) { UnionType::from_elements(self.db(), [declared_ty, default_ty]) - } else if self.file().is_stub(self.db().upcast()) + } else if self.is_stub() && default .as_ref() .is_some_and(|d| d.is_ellipsis_literal_expr()) @@ -1903,8 +1908,8 @@ impl<'db> TypeInferenceBuilder<'db> { unpacked.get(name_ast_id).unwrap_or(Type::Unknown) } TargetKind::Name => { - if self.file().is_stub(self.db().upcast()) && value.is_ellipsis_literal_expr() { - Type::Any + if self.is_stub() && value.is_ellipsis_literal_expr() { + Type::Unknown } else { value_ty }