Skip to content

Commit

Permalink
Fix tests and add a few more
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Dec 14, 2024
1 parent 812aeec commit ccb6abf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# `type[Any]`

This file contains tests for non-fully-static `type[]` types, such as `type[Any]` and `type[Unknown]`.
This file contains tests for non-fully-static `type[]` types, such as `type[Any]` and
`type[Unknown]`.

## Simple

Expand Down Expand Up @@ -80,6 +81,7 @@ def test(x: Any, y: SomethingUnknown):
## `type[Unknown]` has similar properties to `type[Any]`

```py
import abc
from typing import Any
from does_not_exist import SomethingUnknown # error: [unresolved-import]

Expand All @@ -92,4 +94,9 @@ def test(a: type[Any], b: type[str], c: type[Any], d: type[str]):
b = c
c = has_unknown_type
d = has_unknown_type

def test2(a: type[Any], b: abc.ABCMeta):
"""Instances of `type` subclasses are also assignable to `type[Any]` or `type[Unknown]`"""
b = a
b = has_unknown_type
```
18 changes: 10 additions & 8 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,26 +854,26 @@ impl<'db> Type<'db> {
}
(
Type::SubclassOf(SubclassOfType {
base: ClassBase::Any,
base: ClassBase::Any | ClassBase::Todo(_) | ClassBase::Unknown,
}),
Type::SubclassOf(_),
) => true,
(
Type::SubclassOf(SubclassOfType {
base: ClassBase::Any,
base: ClassBase::Any | ClassBase::Todo(_) | ClassBase::Unknown,
}),
Type::Instance(target),
) if target.class.is_known(db, KnownClass::Type) => true,
Type::Instance(_),
) if target.is_subtype_of(db, KnownClass::Type.to_instance(db)) => true,
(
Type::Instance(class),
Type::Instance(_),
Type::SubclassOf(SubclassOfType {
base: ClassBase::Any,
base: ClassBase::Any | ClassBase::Todo(_) | ClassBase::Unknown,
}),
) if class.class.is_known(db, KnownClass::Type) => true,
) if self.is_subtype_of(db, KnownClass::Type.to_instance(db)) => true,
(
Type::ClassLiteral(_) | Type::SubclassOf(_),
Type::SubclassOf(SubclassOfType {
base: ClassBase::Any,
base: ClassBase::Any | ClassBase::Todo(_) | ClassBase::Unknown,
}),
) => true,
// TODO other types containing gradual forms (e.g. generics containing Any/Unknown)
Expand Down Expand Up @@ -3475,6 +3475,8 @@ pub(crate) mod tests {
#[test_case(Ty::BuiltinClassLiteral("str"), Ty::SubclassOfAny)]
#[test_case(Ty::SubclassOfBuiltinClass("str"), Ty::SubclassOfUnknown)]
#[test_case(Ty::SubclassOfUnknown, Ty::SubclassOfBuiltinClass("str"))]
#[test_case(Ty::SubclassOfAny, Ty::AbcInstance("ABCMeta"))]
#[test_case(Ty::SubclassOfUnknown, Ty::AbcInstance("ABCMeta"))]
fn is_assignable_to(from: Ty, to: Ty) {
let db = setup_db();
assert!(from.into_type(&db).is_assignable_to(&db, to.into_type(&db)));
Expand Down

0 comments on commit ccb6abf

Please sign in to comment.