Inconsistent overload resolution on `Generic` methods
- Dominant language
- Python
- Stars
- 15.6k
- Forks
- 1.8k
- Avg merge
- 12h 13m
- Merged PRs (30d)
- 52
Description
**Describe the bug**
According to the pyright docs at https://github.com/microsoft/pyright/blob/main/docs/type-inference.md and the typing docs at https://typing.python.org/en/latest/spec/overload.html#step-5, if there is a conflict in return types of matching overloads, then `pyright` should return `Unknown`. In the example below, that is not happening. `mypy` is reporting `Any`, which we believe is what should happen, while `pyright` is returning `Never`.
**Code or Screenshots**
PYI file `gensub.pyi`:
```python
from __future__ import annotations
from typing import (
Any,
overload,
Generic,
Never,
reveal_type,
Self,
)
from typing_extensions import TypeVar
T = TypeVar("T", int, str)
class Se(Generic[T]):
@overload
def __sub__(self: Se[int], other: Se[int]) -> Never: ...
@overload
def __sub__(self, other: Self) -> Self: ...
```
PY file `gensubtst.py`:
```python
from typing import Any
from gensub import Se
def t1() -> None:
reveal_type(Se[Any]() - Se[Any]()) # mypy: Any, pyright: Never.
```
`pyright gensub.py` output:
```text
gensubtst.py:2:6 - warning: Import "gensub" could not be resolved from source (reportMissingModuleSource)
gensubtst.py:6:17 - information: Type of "Se[Any]() - Se[Any]()" is "Never"
```
`mypy gensub.py` output: (version 1.17.1)
```text
gensubtst.py:6: note: Revealed type is "Any"
```
We think `mypy` is correct here, because the result should be `Unknown` for `mypy` since both overloads match. This is coming up in a PR we are working on for `pandas-stubs`.
**VS Code extension or command-line**
`pyright` command line 1.1.405, python 3.11
Contributor guide
Research direction
Start by reproducing the behavior with gensub.pyi and gensubtst.py using pyright command line 1.1.405, then read the overload-resolution rules in the linked type-inference and typing specification documents. Trace why the matching Generic overloads produce Never instead of the expected Unknown, and verify the corrected revealed type against the example.
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
- 38/100