Skip to content
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

(🐞) call-overload error when calling overloaded constructors which involve exhaustive Literal cases (e.g. for bool type argument) #16777

Open
sg495 opened this issue Jan 13, 2024 · 3 comments
Labels
bug mypy got something wrong

Comments

@sg495
Copy link

sg495 commented Jan 13, 2024

Bug Report

Mypy raises call-overload when attempting to call an overloaded superclass constructor, if the overload hinges on literal True and False values and the call passes a bool value.

This is related to issue #8330

To Reproduce

class A:

    x: bool

    @overload
    def __init__(self, x: Literal[True]) -> None:
        ...
    @overload
    def __init__(self, x: Literal[False] = False) -> None:
        ...

    def __init__(self, x: bool = False) -> None:
        super().__init__()
        self.x = x

class B(A):

    @overload
    def __init__(self, x: Literal[True]) -> None:
        ...
    @overload
    def __init__(self, x: Literal[False] = False) -> None:
        ...

    def __init__(self, x: bool = False) -> None:
        super().__init__(x) # Mypy error: call-overload

# No overload variant of "__init__" of "A" matches argument type "bool"
# Possible overload variants:
#     def __init__(self, x: Literal[True]) -> None
#     def __init__(self, x: Literal[False] = ...) -> NoneMypy

class C(A):

    @overload
    def __init__(self, x: Literal[True]) -> None:
        ...
    @overload
    def __init__(self, x: Literal[False] = False) -> None:
        ...

    def __init__(self, x: bool = False) -> None:
        if x:
            super().__init__(x)  # OK
        else:
            super().__init__(x)  # OK

Please note: this is intended as a minimal example, not as an interesting example. I am happyt to produce interesting examples if they provide additional motivation to tackle this issue.

Expected Behavior

Because the bool type is fully covered by the Literal[True] and Literal[False] cases, it should be possible for Mypy to infer that the call super().__init__(x) is correct in the B constructor.

Actual Behavior

No overload variant of "__init__" of "A" matches argument type "bool"
Possible overload variants:
    def __init__(self, x: Literal[True]) -> None
    def __init__(self, x: Literal[False] = ...) -> NoneMypy

Your Environment

  • Mypy version used: mypy 1.7.1 (compiled: yes)
  • Mypy command-line flags: --strict
  • Python version used: 3.12.0 64-bit
@sg495 sg495 added the bug mypy got something wrong label Jan 13, 2024
@sg495 sg495 changed the title Call-overload error when calling overloaded constructors which involve exhaustive Literal cases (e.g. for bool type argument) (🐞) call-overload error when calling overloaded constructors which involve exhaustive Literal cases (e.g. for bool type argument) Jan 13, 2024
@KotlinIsland
Copy link
Contributor

@sg495
Copy link
Author

sg495 commented Jan 17, 2024

Yeah, it looks like it's related.

@mharding-hpe
Copy link

Just ran into this myself while trying to add mypy into our build pipeline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants