Describe the Bug
from typing import TypeVar, reveal_type
T = TypeVar("T", int, str, float)
def test(x: T) -> T:
if isinstance(x, (int, float)):
return x + 1
else:
return x.lower()
reveal_type(test(42))
reveal_type(test("HEY there"))
Python: 3.12
There are multiple problems, pyrefly fails to infer that x can only be a str in the else branch and it complains in the if branch, that an object of int | float cannot be returned to a type of T, even though it's literally a subset.
For pyright this works just fine
Sandbox Link
https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AVZjABqqSgBo6lGADcYqKAH0mxGAB10mgXQC8g4WMoAKdSAFmprBlLgNJdMFFyoGASk2esMMI3gNjfERBNzoAWgA%2BQURNOjjOXwg4VjsMAGMYQKljayknF3c3GPR40ukYBgBXShL8OgBqOgBGWPiYKDgYYrL4mSqauiJnAHcYEw8tSZl5RRVhYwZ-YwAWACY3CemFZVVMxbtTEAAJAFEATUYACzGNEA2QCRAyGSdSQgZaKAoAYjoABVILygpDoaCweDqaVw6EgbGqrgg0MIml%2BAGUYDA6JcGAxiHBEAB6AnPHzAwi8NgEmDoAmYXBpOAEqEwiBwygI6EExy8OioWSoaCobCwOjM2HwhiIkq4YiS6FwZHoMgMS7QsLySjJaF6OhmADMhCaqzMmhAAF9Hqg0pL5AAxaAwChgnAEEjkM1AA
(Only applicable for extension issues) IDE Information
No response