Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Incomplete or inconsistent Literal and Union behavior on conditional checks #9220

Description

@kprzybyla

Used mypy version: 0.782

I stumbled upon some incomplete behavior related to Literal and Union. In most cases Union is not properly expanded upon conditional check when dealing with literals. Below example should describe clearly the situation.

from typing import Union
from typing_extensions import Literal

class A:
    def __bool__(self) -> Literal[True]:
        return True

    @property
    def is_ok(self) -> Literal[True]:
        return True

    def ok(self) -> Literal[True]:
        return True

class B:
    def __bool__(self) -> Literal[False]:
        return False

    @property
    def is_ok(self) -> Literal[False]:
        return False

    def ok(self) -> Literal[False]:
        return False


def get_a_or_b() -> Union[A, B]: ...

thing = get_a_or_b()

if thing:
    reveal_type(thing)  # Revealed type is 'Union[main.A, main.B]'
else:
    reveal_type(thing)  # Revealed type is 'Union[main.A, main.B]'

if bool(thing):
    reveal_type(thing)  # Revealed type is 'Union[main.A, main.B]'
else:
    reveal_type(thing)  # Revealed type is 'Union[main.A, main.B]'

if thing.is_ok:
    reveal_type(thing)  # Revealed type is 'main.A'
else:
    reveal_type(thing)  # Revealed type is 'main.B'

if thing.ok():
    reveal_type(thing)  # Revealed type is 'Union[main.A, main.B]'
else:
    reveal_type(thing)  # Revealed type is 'Union[main.A, main.B]'

I would expect that all examples will give the same result as the one with is_ok property. Maybe the bool() example would require some additional thought but it would seem wise to make it also work. Also, walrus operator makes it so that even the is_ok property example does not work. Other examples behave the same as before:

if thing := get_a_or_b():
    reveal_type(thing)  # Revealed type is 'Union[main.A, main.B]'
else:
    reveal_type(thing)  # Revealed type is 'Union[main.A, main.B]'

if bool(thing := get_a_or_b()):
    reveal_type(thing)  # Revealed type is 'Union[main.A, main.B]'
else:
    reveal_type(thing)  # Revealed type is 'Union[main.A, main.B]'

if (thing := get_a_or_b()).is_ok:
    reveal_type(thing)  # Revealed type is 'Union[main.A, main.B]'
else:
    reveal_type(thing)  # Revealed type is 'Union[main.A, main.B]'

if (thing := get_a_or_b()).ok():
    reveal_type(thing)  # Revealed type is 'Union[main.A, main.B]'
else:
    reveal_type(thing)  # Revealed type is 'Union[main.A, main.B]'

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions