Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ x = int
class C:
var: ClassVar[x]

reveal_type(C.var) # revealed: Unknown | str
reveal_type(C.var) # revealed: str

x = str
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ x = [1, 2, 3]
reveal_type(x) # revealed: list

# TODO reveal int
reveal_type(x[0]) # revealed: Unknown
reveal_type(x[0]) # revealed: @Todo(Support for `typing.TypeVar` instances in type expressions)

# TODO reveal list
reveal_type(x[0:1]) # revealed: @Todo(specialized non-generic class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ python-version = "3.10"
import types
from knot_extensions import static_assert, is_singleton

# TODO: types.NotImplementedType is a TypeAlias of builtins._NotImplementedType
# Once TypeAlias support is added, it should satisfy `is_singleton`
reveal_type(types.NotImplementedType) # revealed: Unknown | Literal[_NotImplementedType]
static_assert(not is_singleton(types.NotImplementedType))
static_assert(is_singleton(types.NotImplementedType))
```

### Callables
Expand Down
8 changes: 8 additions & 0 deletions crates/red_knot_python_semantic/src/semantic_index/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ impl<'db> ScopeId<'db> {
self.node(db).scope_kind().is_function_like()
}

pub(crate) fn is_module_scope(self, db: &'db dyn Db) -> bool {
self.node(db).scope_kind().is_module()
}

pub(crate) fn is_type_parameter(self, db: &'db dyn Db) -> bool {
self.node(db).scope_kind().is_type_parameter()
}
Expand Down Expand Up @@ -263,6 +267,10 @@ impl ScopeKind {
matches!(self, ScopeKind::Class)
}

pub(crate) fn is_module(self) -> bool {
matches!(self, ScopeKind::Module)
}

pub(crate) fn is_type_parameter(self) -> bool {
matches!(self, ScopeKind::Annotation | ScopeKind::TypeAlias)
}
Expand Down
14 changes: 12 additions & 2 deletions crates/red_knot_python_semantic/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,18 @@ fn symbol_by_id<'db>(
"__slots__" | "TYPE_CHECKING"
);

widen_type_for_undeclared_public_symbol(db, inferred, is_considered_non_modifiable)
.into()
if scope.is_module_scope(db) && scope.file(db).is_stub(db.upcast()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why only at the module level? Typeshed does things like this as well, and I think there's an argument to be made that the public type for DeprecatedNameForThing should also not be unioned with Unknown here:

class Foo:
    THING: Final = 42
    DeprecatedNameForThing = THING

it feels consistent with the idea behind this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't have this restriction initially, but then added it because Carl suggested this as a rule here: #17032 (comment). What was your reasoning @carljm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to do the following: I'm going to merge this as-is, and then re-open another PR with that restriction lifted. This should allow us to see the impact in a better way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

Copy link
Contributor

@carljm carljm Apr 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think the same rationale applies; I don't think I carefully thought through the class-scope case as even a thing that would happen in stub files.

// We generally trust module-level undeclared symbols in stubs and do not union
// with `Unknown`. If we don't do this, simple aliases like `IOError = OSError` in
// stubs would result in `IOError` being a union of `OSError` and `Unknown`, which
// leads to all sorts of downstream problems. Similarly, type variables are often
// defined as `_T = TypeVar("_T")`, without being declared.

inferred.into()
} else {
widen_type_for_undeclared_public_symbol(db, inferred, is_considered_non_modifiable)
.into()
}
}
// Symbol has conflicting declared types
Err((declared, _)) => {
Expand Down
5 changes: 1 addition & 4 deletions crates/red_knot_python_semantic/src/types/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1631,10 +1631,7 @@ mod tests {
assert_eq!(a_name, "a");
assert_eq!(b_name, "b");
// Parameter resolution deferred; we should see B
assert_eq!(
a_annotated_ty.unwrap().display(&db).to_string(),
"Unknown | B"
);
assert_eq!(a_annotated_ty.unwrap().display(&db).to_string(), "B");
assert_eq!(b_annotated_ty.unwrap().display(&db).to_string(), "T");
}

Expand Down
Loading