-
Notifications
You must be signed in to change notification settings - Fork 281
Open
Labels
Description
Describe the Bug
This example fails:
class Foo: ...
def initialize[F: Foo](cls: type[F]) -> F:
assert isinstance(cls, type)
assert issubclass(cls, Foo)
try:
inst = cls()
except:
raise
return inst # ❌️ E: Returned type `Foo` is not assignable to declared return type `F`but if we uncomment either assert clause it succeeds. The root cause seems to be that the subsequence asserts narrow the type in a way that forgets the type variable:
reveal_type(cls) # type[F]
assert isinstance(cls, type)
reveal_type(cls) # type
assert issubclass(cls, Foo)
reveal_type(cls) # type[Foo]
My expectation would be that neither assert changes the type, because
type[F] & typeshould equaltype[F](n.b.type[F] & type[Any]would be another story...)type[F] & type[Foo]should equaltype[F], as everyFis aFoo.
Python: 3.14
Sandbox Link
(Only applicable for extension issues) IDE Information
No response
Reactions are currently unavailable