Skip to content

Pyrefly and Pyright treat conformance for overload matching differently. #5239

Closed as duplicate

Description

@rchiodo

Describe the Bug

Question is about this case. Behavior difference between Pyright and Pyrefly.

@overload
def overloaded(value: list[int]) -> list[int]: ...

@overload
def overloaded(value: list[str]) -> list[str]: ...

def check(value: list[Any]) -> None:
    reveal_type(overloaded(value), expected_text="?")

I believe the type spec says the typechecker has to not show an error when accessing any of the possible values for the returned value.

Pyrefly handles this by just marking all ambiguous matches as Any.

Pyright was handling this by just picking the first match (which is not conformant because it shows errors for the other matches) but is in the process of representing the result as a kind of intersection of list[int] and list[str].

See this Pyright issue:
microsoft/pyright#11731

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

Activity

  1. rchiodo commented on Oct 14, 2026

    @rchiodo
    CollaboratorAuthor

    I believe Ty returns 'Any' in this case

  2. kinto0 commented on Oct 14, 2026

    @kinto0
    Contributor

    Looks like this was added to the conformance test from this post. Pyright still seems to fail because it errors here:

    def check_example5(b: list[Any]) -> None:
        ret = example5(b)
        int_list: list[int] = ret   # must not error
        str_list: list[str] = ret   # must not error
    

    I don't have strong opinions on what Pyrefly should do here yet (or thoughts on how) but it does seem like an improvement in this case to create List[Any] over Any.

  3. rchiodo commented on Oct 14, 2026

    @rchiodo
    CollaboratorAuthor

    Yeah I'm fixing the second case right now in microsoft/pyright#11864.

  4. rchen152 commented on Oct 16, 2026

    @rchen152
    Contributor

    Related: #4910.

    I've been playing around with another idea, which is to use pyrefly's existing Type::Overloaded construct (which behaves like an ordered "any of" type) to represent these ambiguous cases, so in the example above, we'd emit Overloaded[list[int], list[str]], which would be usable anywhere either list[int] or list[str] is. (Pyright's "a kind of intersection of list[int] and list[str]" sounds like it may be a similar idea?) The problem that I've gotten a little stuck on is that Overloaded was introduced for a far more limited use case, and adopting it for overload evaluation would mean it appears in a lot more places that don't currently expect or handle Overloaded.

  5. rchen152 commented on Nov 5, 2026

    @rchen152
    Contributor

    Actually, I'm going to dedup this issue into the other one, since I think they'll need the same solution.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions