false positive incompatible-override error when expanding `bool` args over different overloads
- Dominant language
- Python
- Stars
- 15.6k
- Forks
- 1.8k
- Avg merge
- 12h 13m
- Merged PRs (30d)
- 52
Description
In the example from #10645 only two overloads should be needed:
```py
from typing import Literal, Protocol, overload
class BoolFormatter(Protocol):
def format_bool(self, b: bool, /) -> str: ...
class LiteralBoolFormatter(BoolFormatter, Protocol):
@overload
def format_bool(self, b: Literal[True], /) -> Literal["⊤"]: ...
@overload
def format_bool(self, b: Literal[False], /) -> Literal["⊥"]: ... # ❌ reportIncompatibleMethodOverride
```
From the typing spec section on argument expansion in overloads ([ref](https://typing.python.org/en/latest/spec/overload.html#argument-type-expansion)):
> 2. `bool` should be expanded into `Literal[True]` and `Literal[False]`.
This means that the `BoolFormatter` definition above, is equivalent to the one below:
```py
class BoolFormatter(Protocol):
@overload
def format_bool(self, b: Literal[True], /) -> str: ...
@overload
def format_bool(self, b: Literal[False], /) -> str: ...
```
But if we use this `BoolFormatter` definition instead, the same `LiteralBoolFormatter.format_bool` definition is not reported as an incompatible override ([playground](https://pyright-play.net/?pythonVersion=3.14&strict=true&reportImplicitOverride=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAySMApiAIYA2ANFAArgxgDGYNUYAbmZWOQCYAoQc0rkAzuKgAhMGwBiuCORikQACgZgmrSgEoAXIKgmoAAS48%2BQ01H4lgUYEpUB9AEZzK68SUrBadwNCYjIqAG0AFRAAVxIAXVoAej0oAFoAPihxGBBggDpCqGNTC24QXgESk3tHZxBlGA8vHz8AqCCQtQj5Kl9EqBT0rJy8qEL84pExSS6wylkFF1UydUXKRQaVNVotHTZDavNLCusj2qdl5rZW-0Dgom7KKNiE5NTMuYpngCJAEqIfvECoUjmUrFVbBd6o1rt5fHcOg9Qt9wr1KP13sMvhEfoBSokBwPyQA))
I realize that the [overload subtyping rules](https://typing.python.org/en/latest/spec/callables.html#overloads) are not enough to classify the reported error as a false positive. But by additionally considering the equivalence of these two `BoolFormatter.format_bool` definitions, additional subtyping rules for overloads emerge.
So by following the typing spec, the reported error is already a false positive, albeit indirectly. So that's why I'm reporting it here, and not at discuss.python.org or github.com/python/typing.
But having said that, it would probably be a good idea to update the typing spec to be more explicit about these rules. And for what it's worth, the set-theoretical formulation by @randolf-scholz in https://github.com/python/typing/issues/2021 looks to me like an excellent candidate for that.
Contributor guide
Research direction
Start with the reported BoolFormatter and LiteralBoolFormatter reproducer, then read the typing specification sections on argument expansion in overloads and overload subtyping. Compare behavior with the equivalent expanded BoolFormatter definition and the linked playground. Done means the original example no longer reports reportIncompatibleMethodOverride while the equivalent overload behavior remains correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, tooling
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100