Skip to content

Is Type Propagation for Overloaded Functions Well-Supported? #9942

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

You must be logged in to vote

The problem here is that you're defining an instance method that doesn't accept a self parameter.

You can see the problem more clearly if you do this:

Code sample in pyright playground

class A:
    def x() -> None: ...
    def y(_: bool) -> None: ...

A.x
A.y

To fix your code, here are some suggestions.

  1. You can mark the function as a staticmethod:
@overload
@staticmethod
def f() -> None: ...
@overload
@staticmethod
def f(_: bool) -> None: ...

@staticmethod
def f(_=False): ...

Unfortunately, mypy doesn't like this approach.

  1. Define a new static method:
class C:
    @overload
    @staticmethod
    def f() -> None: ...
    @overload
    @staticmethod
    def f(x: bool, /) -> None: ...
    @

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@ckwastra
Comment options

@erictraut
Comment options

Answer selected by ckwastra
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