(🐞) call-overload error when calling overloaded constructors which involve exhaustive Literal cases (e.g. for bool type argument)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the minimal example with mypy 1.7.1 and --strict, focusing on the super().init(x) call in class B and comparing it with class C. Trace the overload checking involved in exhaustive Literal[True] and Literal[False] cases; done means the B constructor no longer produces call-overload and the regression is covered by a test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100