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
The type annotation on self should constrain the type variable T to be a str. However, this constraint is not applied, and instead T is assumed to be int | str:
File "example.py", line 9, in as_str: bad option 'int' in return type [bad-return-type]
Expected: str
Actually returned: Union[int, str]
This only happens when T has constraints defined. If T is left unconstrained (T = TypeVar("T")), then this code checks as expected.
The text was updated successfully, but these errors were encountered:
A somewhat similar bug can be seen when using Any1 as one of the constraints to TypeVar. Happy to report this as a separate issue, but it's possible the root cause is the same so wanted to check first:
The above code fails to type check with the following error:
File "example.py", line 9, in get_value: bad option 'str' in return type [bad-return-type]
Expected: int
Actually returned: Any
If we replace x.value with an actual Any value, then this code will type check.
Footnotes
To clarify, we're not actually using Any here (that would be a bit silly!), but a type in an external (untyped) Python module. From my understanding, this then gets replaced with Any. ↩
Version Info
Details
Consider the following code:
The type annotation on
self
should constrain the type variableT
to be astr
. However, this constraint is not applied, and insteadT
is assumed to beint | str
:This only happens when
T
has constraints defined. IfT
is left unconstrained (T = TypeVar("T"))
, then this code checks as expected.The text was updated successfully, but these errors were encountered: