Skip to content

Boolean literals not recognized as Literals #11232

Closed
@jolaf

Description

@jolaf

The following code:

from typing import Literal, Tuple, Union

x: bool = False

def f() -> Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]:
    if x:
        return (True, 5)
    else:
        return (False, 'oops')

print(f())

produces the following ouput:

$ python3 test.py
(False, 'oops')

$ mypy test.py
test.py: note: In function "f":
test.py:7:16: error: Incompatible return value type (got "Tuple[bool, int]", expected "Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]")  [return-value]
            return (True, 5)
                   ^
test.py:9:16: error: Incompatible return value type (got "Tuple[bool, str]", expected "Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]")  [return-value]
            return (False, 'oops')
                   ^
Found 2 errors in 1 file (checked 1 source file)

$ mypy --version
mypy 0.910

$ python3 --version
Python 3.8.10

$ cat /etc/issue
Ubuntu 20.04.3 LTS \n \l

$ uname -a
Linux ubuntu 5.4.0-88-generic #99-Ubuntu SMP Thu Sep 23 17:29:00 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

From my point of view, the code looks correct, and mypy should not object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions