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
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
Python version used: 3.11.0
OS: Windows 11
The text was updated successfully, but these errors were encountered:
…f binary ops (python#19249)
Fixespython#12001. Fixespython#6898. Fixespython#15368. Improves python#17790 and python#11508.
When encountering `a {and,or} b`, we used to use `a` as primary context
for `b` inference. This results in weird errors when `a` and `b` are
completely unrelated, and in many cases return type/assignment type
context can do much better. Inferring to union should be harmless in
most cases, so use union of `a` and current context instead.
Bug Report
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
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 main.py
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: