Skip to content

Inconsistent overload resolution on Generic methods #10924

@Dr-Irv

Description

@Dr-Irv

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:

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:

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:

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)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions