Skip to content

Commit

Permalink
Infer Type::Unknown for a variable with value ...
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Dec 30, 2024
1 parent 2fd6c86 commit d30649c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
11 changes: 8 additions & 3 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit d30649c

Please sign in to comment.