-
The following code is accepted by mypy, but not by Pyright: from typing import overload
@overload
def f() -> None: ...
@overload
def f(_: bool) -> None: ...
def f(_=False): ...
class C:
f = f
f()
C.f()
f(True)
C.f(True) # Pyright: Expected 0 positional arguments I'm unsure whether such usage is recommended. Should we define a separate type for |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The problem here is that you're defining an instance method that doesn't accept a 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.
@overload
@staticmethod
def f() -> None: ...
@overload
@staticmethod
def f(_: bool) -> None: ...
@staticmethod
def f(_=False): ... Unfortunately, mypy doesn't like this approach.
class C:
@overload
@staticmethod
def f() -> None: ...
@overload
@staticmethod
def f(x: bool, /) -> None: ...
@staticmethod
def f(x: bool = False, /):
return f(x) This admittedly involves duplicating some boilerplate, but it is compatible with mypy. Pyright's error message for your original code snippet isn't very clear, so I've updated the logic to improve the error message. This will be included in the next release. |
Beta Was this translation helpful? Give feedback.
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
To fix your code, here are some suggestions.
staticmethod
:Unfortunately, mypy doesn't like this approach.