Skip to content

Can you prevent 'Any' from always matching the first overload? #1792

Answered by erictraut
maflAT asked this question in Q&A
Discussion options

You must be logged in to vote

Overload resolution behavior isn't currently specified in the typing specification, so you will may see differences between Python type checkers — especially when Any is involved. In your code sample, the behaviors of pyright and mypy appear to match. For a description of pyright's overload resolution behavior, refer to this documentation.

I don't understand the full constraints of your use case, but a TypeVarTuple might be preferable to a TypeVar. Here's how that might look.

Code sample in pyright playground

from __future__ import annotations
from typing import Any, overload

class C[*Ts]:
    @overload
    def foo(self: C[()] | C[Any]) -> tuple[*Ts]:
        ...

    @overload
    def foo(

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@maflAT
Comment options

@maflAT
Comment options

@erictraut
Comment options

Answer selected by maflAT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants