You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When implementing the following generic class with a single upper bound of a union type, you get the following errors, while no errors are raised when using multiple upper-bounds.
error: Incompatible return value type (got "int", expected "T") [return-value]
error: Incompatible return value type (got "str", expected "T") [return-value]
Your Environment
Mypy version used: 1.13.0
Mypy command-line flags: --strict
Mypy configuration options from mypy.ini (and other config files):
Python version used: 3.12.3
The text was updated successfully, but these errors were encountered:
Your Example1 does not use multiple bounds, but constraints, which work differently. A TypeVar with constraints can be solved only to exactly int or str, and the method must return int if an int is given, and str if a str is given. But Example2 uses a bound, and the signature says that the return type must be exactly the same as the input type, for any consistent subtype of int | str. For example, bool is a subclass of int, so Example2().bar(True) should return bool, but in your implementation it doesn't: it returns 2, an int.
Bug Report
When implementing the following generic class with a single upper bound of a union type, you get the following errors, while no errors are raised when using multiple upper-bounds.
To Reproduce
Expected Behavior
Class
Example2
should raise no errors.Actual Behavior
Class
Example2
raises the following error:Your Environment
--strict
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: