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
$ mypy --version
mypy 0.640+dev.e12be3ba9e6be3e9242e3a81ffaa9b3a136876fe
$ python3 --version
Python 3.7.0
$ mypy lambdas_n_defs.py
lambdas_n_defs.py:19: error: Argument 1 to "len" has incompatible type "Union[int, str]"; expected "Sized"
lambdas_n_defs.py:23: error: Unsupported operand types for + ("str" and "int")
lambdas_n_defs.py:23: note: Left operand is of type "Union[int, str]"
lambdas_n_defs.py:29: error: Argument 1 to "len" has incompatible type "Union[int, str]"; expected "Sized"
lambdas_n_defs.py:31: error: Unsupported operand types for + ("str" and "int")
lambdas_n_defs.py:31: note: Left operand is of type "Union[int, str]"
Expected output: none, because I believe mypy should treat fails1() and fails2() identically to works()
The text was updated successfully, but these errors were encountered:
I've realized that the only reason that works() works is because I wasn't using --check-untyped-defs, so here's a second test case that gets to what I actually care about:
$ python3 --version
Python 3.7.0
$ mypy --version
mypy 0.640+dev.e12be3ba9e6be3e9242e3a81ffaa9b3a136876fe
$ nl -ba lambdas_n_defs.py
1 from typing import Callable
2 from typing import Union
3
4
5 def works(arg: Union[int, str]) -> Callable[[], int]:
6 if isinstance(arg, str):
7 answer = len(arg)
8
9 def ret() -> int:
10 return answer
11 return ret
12 elif isinstance(arg, int):
13 answer = arg + 8
14
15 def ret() -> int:
16 return answer
17 return ret
18
19
20 def fails(arg: Union[int, str]) -> Callable[[], int]:
21 if isinstance(arg, str):
22 def ret() -> int:
23 return len(arg)
24 return ret
25 elif isinstance(arg, int):
26 def ret() -> int:
27 return arg + 8
28 return ret
$ mypy --strict lambdas_n_defs.py
lambdas_n_defs.py:23: error: Argument 1 to "len" has incompatible type "Union[int, str]"; expected "Sized"
lambdas_n_defs.py:27: error: Unsupported operand types for + ("str" and "int")
lambdas_n_defs.py:27: note: Left operand is of type "Union[int, str]"
Edit: see first comment
Test case:
Actual output:
Expected output: none, because I believe mypy should treat fails1() and fails2() identically to works()
The text was updated successfully, but these errors were encountered: