Skip to content

Commit ef3d3eb

Browse files
committed
Account for non-fully-static TypeIs types in is_assignable_to()
1 parent f894e8d commit ef3d3eb

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

crates/ty_python_semantic/resources/mdtest/type_properties/is_assignable_to.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,4 +789,17 @@ def g3(obj: Foo[tuple[A]]):
789789
f3(obj)
790790
```
791791

792+
## `TypeGuard` and `TypeIs`
793+
794+
`TypeGuard[...]` and `TypeIs[...]` are always assignable to `bool`.
795+
796+
```py
797+
from ty_extensions import Unknown, is_assignable_to, static_assert
798+
from typing_extensions import Any, TypeGuard, TypeIs
799+
800+
# TODO: TypeGuard
801+
# static_assert(is_assignable_to(TypeGuard[Unknown], bool))
802+
static_assert(is_assignable_to(TypeIs[Any], bool))
803+
```
804+
792805
[typing documentation]: https://typing.python.org/en/latest/spec/concepts.html#the-assignable-to-or-consistent-subtyping-relation

crates/ty_python_semantic/src/types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,10 @@ impl<'db> Type<'db> {
16961696
(Type::ProtocolInstance(_), _) => false,
16971697
(_, Type::ProtocolInstance(protocol)) => self.satisfies_protocol(db, protocol),
16981698

1699+
(Type::TypeIs(_), _) => KnownClass::Bool
1700+
.to_instance(db)
1701+
.is_assignable_to(db, target),
1702+
16991703
// TODO other types containing gradual forms
17001704
_ => self.is_subtype_of(db, target),
17011705
}

0 commit comments

Comments
 (0)