-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Open
Labels
bugmypy got something wrongmypy got something wrong
Description
Bug Report
Using union as a bound for generic function type parameter T.
Function takes one argument of type type[T] and returns T.
mypy cannot infer equivalence between instance of type[T] and return type T.
The error is not reproduced if:
- single concrete type is used for bound (
func[T: int]) - no bound used (
func[T])
To Reproduce
def func[T: bool | int | float | str](ftype: type[T], value: str | None) -> T:
if value is None:
return ftype()
return ftype(value)
if __name__ == '__main__':
print(func(bool, 'True'))
print(func(bool, None))
print(func(int, '1'))
print(func(int, None))
print(func(float, '1.0'))
print(func(float, None))
print(func(str, 'test'))
print(func(str, None))https://mypy-play.net/?mypy=latest&python=3.12&gist=3bc312ee3a5620cbd789bfa1c93152ec
Expected Behavior
No errors
Actual Behavior
main.py:3: error: Incompatible return value type (got "int | float | str", expected "T") [return-value]
main.py:4: error: Incompatible return value type (got "int | float | str", expected "T") [return-value]Your Environment
- Mypy version used: 1.19.1
- Python version used: 3.12
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrong