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
Mypy incorrectly gives an error for a ternary return statement #15368
Hi, I was using mypy and I think I've noticed a bug. For a function that returns either List[str] or List[List[str]], mypy gives an error if the return type is decided as a ternary. However, if the return type is decided with an 'if-else' instead, there are no problems. I also tested a similar function, except where the return type is either a float or str. In that case, mypy had no problems with the ternary usage.
To Reproduce
fromtypingimportUnion, Listdefget_str_float(b: bool) ->Union[str, float]:
return8ifbelse'8'# mypy correctly gives no error heredefget_list(b: bool) ->Union[List[str], List[List[str]]]:
return ['a'] ifbelse [['a']] # mypy incorrectly gives an error""" Mypy gives no error if I replace the above line with the following: if b: return ['a'] else: return [['a']] """defmain():
print(get_list(True))
print(get_str_float(True))
if__name__=="__main__":
main()
Expected Behavior
No errors
Actual Behavior
main.py:7: error: List item 0 has incompatible type "List[str]"; expected "str" [list-item] Found 1 error in 1 file (checked 1 source file)
Your Environment
Mypy version used: mypy 1.3.0 (compiled: yes)
Mypy command-line flags: none - just ran mypy main.py
Mypy configuration options from mypy.ini (and other config files): none
Activity
Use union of current context and left side for right side narrowing o…