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
{{ message }}
Repository navigation
Union was callable in 0.770 but is not in 0.780 #8953
Using version 0.770, I had some code passing type checking just fine, but now using 0.780 the same code yields "object" not callable. the code in question is the expression Union[bytes, str](returned).
Did 0.780 change a Union from callable to non-callable? Or was it always non-callable and just not flagged before? Or what?
I checked the release notes for 0.780 and didn't see anything that seemed relevant to me...
I'm not sure what behavior to expect. I don't have that good of a grasp of Python type checking. Should a Union be callable, the way any other type like int or str are? Or not? 🤷♂️
I'm using Python 3.7.something. (3.7.2 locally, possibly a different version in my CI, where this is also happening.)
Mypy generally doesn't like using types in a runtime context. Union[bytes, str](returned) doesn't work at runtime either, at least in 3.6 (it throws TypeError: Cannot instantiate typing.Union). I'm not sure what it could mean either.
Yes, this was always not callable and is now flagged.
The relevant change was to typeshed, not mypy: python/typeshed#3933.
Note this applies to some simpler types like typing.List as well. Type checkers will now correctly complain about code that fails at runtime like:
~/delete λ cat test89.py
from typing import List
List()
~/delete λ python3.9 test89.py
Traceback (most recent call last):
File "/Users/shantanu/delete/test89.py", line 2, in <module>
List()
File "/Users/shantanu/.pyenv/versions/3.9-dev/lib/python3.9/typing.py", line 614, in __call__
raise TypeError(f"Type {self._name} cannot be instantiated; "
TypeError: Type List cannot be instantiated; use list() instead
@jstasiak it's possible in theory mypy could learn those semantics, but it would need to be supported by CPython first so that it doesn't crash at runtime. I feel it's unlikely CPython will make that change, since Union[bytes, str](x) looks more like it's meant to do something at runtime than cast(Union[bytes, str], x).
Activity
JelleZijlstra commentedon Jun 4, 2020
Mypy generally doesn't like using types in a runtime context.
Union[bytes, str](returned)doesn't work at runtime either, at least in 3.6 (it throwsTypeError: Cannot instantiate typing.Union). I'm not sure what it could mean either.jstasiak commentedon Jun 4, 2020
Not knowing anything about the code using this I'd interpret it as equivalent to
cast(Union[bytes, str], returned), I'm curious how far off am I.hauntsaninja commentedon Jun 4, 2020
Yes, this was always not callable and is now flagged.
The relevant change was to typeshed, not mypy: python/typeshed#3933.
Note this applies to some simpler types like
typing.Listas well. Type checkers will now correctly complain about code that fails at runtime like:@jstasiak it's possible in theory mypy could learn those semantics, but it would need to be supported by CPython first so that it doesn't crash at runtime. I feel it's unlikely CPython will make that change, since
Union[bytes, str](x)looks more like it's meant to do something at runtime thancast(Union[bytes, str], x).jstasiak commentedon Jun 4, 2020
Oh sure, I'm not proposing Mypy and Python implement this, I'm just trying to guess the intended meaning of the code in the report.
JukkaL commentedon Jun 8, 2020
There seems to be nothing to do here; mypy seems to be behaving now as expected, as a false negative was fixed in 0.780.
Python: pin mypy to <= 0.770