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
Variance inference works out whether a type variable should be co-, contra- or in-variant, based on the way it is used as an argument type, a result type, or both.
But it seems it cannot see through a Union type used in a result. In this example, it correctly determines that T in class Example is used as both argument and result, therefore is invariant, and does not complain becuase the TypeVar is (by default) invariant. But in class Example2, T is still used as both argument and result, albeit through a Union. It seems that mypy cannot see through the union, and reports errors just as though the method had no variables in its return type. For comparison, in Example3, the type variables are not used at all in the return type, so it correctly infers that T should be contravariant; mypy gives the same error messages as Example2, suggesting that it doesn't see S|T as containing any type variables at all.
In the class Example2, I expect mypy to recognize that T is used positively in method arguments and result types, and therefore to infer that T should be an invariant variable. (The inference for S is not important for this report; but the bug does not repro if the other type is a concrete type like int.)
Actual Behavior
$ mypy example.py
example.py:10: error: Invariant type variable "S" used in protocol where covariant one is expected [misc]
example.py:10: error: Invariant type variable "T" used in protocol where contravariant one is expected [misc]
example.py:14: error: Invariant type variable "S" used in protocol where covariant one is expected [misc]
example.py:14: error: Invariant type variable "T" used in protocol where contravariant one is expected [misc]
Your Environment
Mypy version used: 1.11.2 (compiled: yes)
Mypy command-line flags: none, just the python file name
Mypy configuration options from mypy.ini (and other config files): none
Python version used: 3.10.14
The text was updated successfully, but these errors were encountered:
Bug Report
Variance inference works out whether a type variable should be co-, contra- or in-variant, based on the way it is used as an argument type, a result type, or both.
But it seems it cannot see through a Union type used in a result. In this example, it correctly determines that
T
in classExample
is used as both argument and result, therefore is invariant, and does not complain becuase the TypeVar is (by default) invariant. But in classExample2
,T
is still used as both argument and result, albeit through a Union. It seems that mypy cannot see through the union, and reports errors just as though the method had no variables in its return type. For comparison, inExample3
, the type variables are not used at all in the return type, so it correctly infers that T should be contravariant; mypy gives the same error messages asExample2
, suggesting that it doesn't seeS|T
as containing any type variables at all.To Reproduce
Expected Behavior
In the class Example2, I expect mypy to recognize that
T
is used positively in method arguments and result types, and therefore to infer thatT
should be an invariant variable. (The inference forS
is not important for this report; but the bug does not repro if the other type is a concrete type likeint
.)Actual Behavior
Your Environment
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: