Skip to content

Commit

Permalink
add failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Dec 14, 2024
1 parent ac31b26 commit 812aeec
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# `type[Any]`

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

## Simple

```py
def f(x: type[Any]):
def f(x: type[Any], y: type[str]):
reveal_type(x) # revealed: type[Any]
# TODO: could be `<object.__repr__ type> & Any`
reveal_type(x.__repr__) # revealed: Any

z = x
x = y
y = z

class A: ...

x: type[Any] = object
Expand Down Expand Up @@ -70,3 +76,20 @@ def test(x: Any, y: SomethingUnknown):
reveal_type(y.__class__) # revealed: type[Unknown]
reveal_type(y.__class__.__class__.__class__.__class__) # revealed: type[Unknown]
```

## `type[Unknown]` has similar properties to `type[Any]`

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

has_unknown_type = SomethingUnknown.__class__
reveal_type(has_unknown_type) # revealed: type[Unknown]

def test(a: type[Any], b: type[str], c: type[Any], d: type[str]):
"""Both `type[Any]` and `type[Unknown]` are assignable to all `type[]` types"""
a = b
b = c
c = has_unknown_type
d = has_unknown_type
```
4 changes: 4 additions & 0 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3351,6 +3351,7 @@ pub(crate) mod tests {
},
Tuple(Vec<Ty>),
SubclassOfAny,
SubclassOfUnknown,
SubclassOfBuiltinClass(&'static str),
StdlibModule(CoreStdlibModule),
SliceLiteral(i32, i32, i32),
Expand Down Expand Up @@ -3398,6 +3399,7 @@ pub(crate) mod tests {
Type::tuple(db, elements)
}
Ty::SubclassOfAny => Type::subclass_of_base(ClassBase::Any),
Ty::SubclassOfUnknown => Type::subclass_of_base(ClassBase::Unknown),
Ty::SubclassOfBuiltinClass(s) => Type::subclass_of(
builtins_symbol(db, s)
.expect_type()
Expand Down Expand Up @@ -3471,6 +3473,8 @@ pub(crate) mod tests {
#[test_case(Ty::BuiltinInstance("type"), Ty::SubclassOfBuiltinClass("object"))]
#[test_case(Ty::BuiltinInstance("type"), Ty::BuiltinInstance("type"))]
#[test_case(Ty::BuiltinClassLiteral("str"), Ty::SubclassOfAny)]
#[test_case(Ty::SubclassOfBuiltinClass("str"), Ty::SubclassOfUnknown)]
#[test_case(Ty::SubclassOfUnknown, Ty::SubclassOfBuiltinClass("str"))]
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 812aeec

Please sign in to comment.