mypy
mypy copied to clipboard
(🐞) call-overload error when calling overloaded constructors which involve exhaustive Literal cases (e.g. for bool type argument)
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 https://github.com/python/mypy/issues/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
- could this be related to #14764?
Yeah, it looks like it's related.
Just ran into this myself while trying to add mypy into our build pipeline