Skip to content

0.920 Regression: literal unions aren't inferred from conditionals anymore #11782

Closed
@elprans

Description

@elprans

test.py:

def foo(h: bytes) -> int:
    if h[5] == 2:
        byteorder = "big"
    elif h[5] == 1:
        byteorder = "little"
    else:
        raise AssertionError(
            f"unexpected ELF endianness: {h[5]}"
        )
    return int.from_bytes(h[16:18], byteorder=byteorder, signed=False)

Results in

$ mypy test.py
1.py:10:17: note: Revealed type is "builtins.str"
1.py:11:47: error: Argument "byteorder" to "from_bytes" of "int" has incompatible type "str"; expected "Union[Literal['little'], Literal['big']]"

With mypy-0.920 and later. 0.910 correctly inferred byteorder as Literal['little'] | Literal['big']

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