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`
reveal_type(cls) # type[F]
assert isinstance(cls, type)
reveal_type(cls) # type
assert issubclass(cls, Foo)
reveal_type(cls) # type[Foo]
Describe the Bug
This example fails:
but if we uncomment either
assertclause it succeeds. The root cause seems to be that the subsequence asserts narrow the type in a way that forgets the type variable: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
https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN2UYANxiooAfSbEYAHXRyAxlFRw4dAGK5ciOoT1y5mGGE7oIDCGIgAvGAG11OzbgC6ACiVwdU%2B%2BpcBKOgBaAD4NRDk6KIFhUQkfDyg4QLoAYkZmXxdI6JU4GD5OOFY4BgwFGES4ABoM6X8cqMERMUlMqpT0n0a6PIL%2BCFUAV2wlPKra5wb0aJiW%2BPbPTrrfLWz5GeiGSlIdHtmS-gBeOk83admYfAriBgjN2abUQdkN2cEGIcoZw6j0wBlyQDwf3QAKI6ABKME%2B3xgmBWdAABs4EUU6OhcPw8hA2OhUNhYIxcHQjGNBHCPl8Zj5EeoESBqiAhhYkiRyIgQOkAKrM8ykOhgIboBQWXDoOAGLDGfm8GioBjidBDGjYApufA6VgMQKhOilSj3d5Qyn8mQgABySpV%2BrowHwAF9TXJ6SAyIIwFBSIQGLQoBR0gAFUhuj26jA4AinUWQNhfOUQUWEOTpADKMBgdAAFgwGMQvAB6POu4wewi8Nh5mDoPOYXAKOB5hRR7GxkVV6WUXpCZ7KfHpxvoaMt%2BMzXC3YdwRPoMgMDOioIiSjFUV0E6mgDMhAAjAAmR3oEB2hmoYUQETqaAwChoLB4IhkA9AA
(Only applicable for extension issues) IDE Information
No response