Skip to content

Mypy incorrectly gives an error for a ternary return statement #15368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
johndoknjas opened this issue Jun 5, 2023 · 0 comments · Fixed by #19249
Closed

Mypy incorrectly gives an error for a ternary return statement #15368

johndoknjas opened this issue Jun 5, 2023 · 0 comments · Fixed by #19249
Labels
bug mypy got something wrong topic-type-context Type context / bidirectional inference

Comments

@johndoknjas
Copy link
Contributor

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

from typing import Union, List

def get_str_float(b: bool) -> Union[str, float]:
    return 8 if b else '8' # mypy correctly gives no error here

def get_list(b: bool) -> Union[List[str], List[List[str]]]:
    return ['a'] if b else [['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']]
    """

def main():
    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
@johndoknjas johndoknjas added the bug mypy got something wrong label Jun 5, 2023
@JelleZijlstra JelleZijlstra added the topic-type-context Type context / bidirectional inference label Jun 5, 2023
ilevkivskyi pushed a commit to ilevkivskyi/mypy that referenced this issue Jun 13, 2025
…f binary ops (python#19249)

Fixes python#12001. Fixes python#6898. Fixes python#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-context Type context / bidirectional inference
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants