Skip to content

bool is not treated the same as Union[Literal[True], Literal[False]] in overloads #10194

Closed
@MarcoGorelli

Description

@MarcoGorelli

Bug Report

The Type checking calls to overload docs read

if multiple variants match due to one or more of the arguments being a union, mypy will make the inferred type be the union of the matching variant returns

To Reproduce

from typing import overload, Optional, Literal, Union


@overload
def foo(inplace: Literal[True]) -> None: ...

@overload
def foo(inplace: Literal[False]) -> int: ...

def foo(inplace: bool) -> Optional[int]: ...

inplace_foo: bool
reveal_type(foo(inplace_foo))  # Revealed type is 'Any'



@overload
def bar(inplace: Literal[True]) -> None: ...

@overload
def bar(inplace: Literal[False]) -> int: ...

def bar(inplace: Union[Literal[True], Literal[False]]) -> Optional[int]: ...

inplace_bar: Union[Literal[True], Literal[False]]
reveal_type(bar(inplace_bar))  # Revealed type is 'Union[None, builtins.int]'

https://mypy-play.net/?mypy=latest&python=3.9&gist=051875ec08b32acb049577f7cd6085f7

Expected Behavior

I would've expected that typing a variable as bool would make it behave the same way as Union[Literal[True], Literal[False]]

Actual Behavior

bool is not treated the same as Union[Literal[True], Literal[False]]. However, that's all that bool can be, right, either True or False?

Your Environment

  • Mypy version used: latest
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.9
  • Operating system and version:

See the link to the mypy playground above to reproduce

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