Skip to content

"Unknown" type eagerly matches overloads #10391

Closed as duplicate
Closed as duplicate

Description

@alexarice

Describe the bug
The Unknown type seems to eagerly match the first function overload rather that matching the most generic overload.

Code or Screenshots

from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Generic, TypeVar, overload, reveal_type

B = TypeVar("B", bound=str | float)

@dataclass
class A(Generic[B]):
    data: B

    @overload
    def method(self: A[str]) -> str: ...
    @overload
    def method(self: A[float]) -> float: ...
    @overload
    def method(self: A[str | float]) -> str | float: ...
    def method(self) -> str | float:
        return self.data

def test(x: Any):
    if isinstance(x, A):
        reveal_type(x)
        reveal_type(x.method())

In the following code, the type of x is revealed to be A[Unknown] (as expected) but then x.method() has type str which I would not expect. Reordering the first two overloads causes the type to be revealed as a float instead. Commenting out the third overload causes no change.

Apologies if this is the intended functionality or has already been reported/fixed.

VS Code extension or command-line
Using pyright 1.1.400 from either emacs or command line produces the same behaviour

Activity

  1. added
    bug
    on Apr 29, 2025
  2. alexarice commented on Apr 29, 2025

    @alexarice
    Author

    I should have added that setting B to be covariant doesn't seem to change anything

  3. erictraut commented on Apr 29, 2025

    @erictraut
    Collaborator

    This is a duplicate of #10232.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions