-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
@overload
s on @abstractmethod
s shouldn't need an implementation
#11488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Try this: from abc import ABC, abstractmethod
from typing import overload
class Foo(ABC):
@overload
def foo(self, value: int) -> int:
...
@overload
def foo(self, value: str) -> str:
...
@abstractmethod
def foo(self, value: str | int) -> str | int:
...
class Bar(Foo):
@overload
def foo(self, value: int) -> int:
...
@overload
def foo(self, value: str) -> str:
...
def foo(self, value: str | int) -> str | int:
return 1 Works like a charm! 👍 |
yeah, but the implementation is useless in the abstract method. i'd prefer if i coulkd just go from abc import ABC, abstractmethod
from typing import overload
class Foo(ABC):
@abstractmethod
@overload
def foo(self, value: int) -> int:
...
@abstractmethod
@overload
def foo(self, value: str) -> str:
...
class Bar(Foo):
@overload
def foo(self, value: int) -> int:
...
@overload
def foo(self, value: str) -> str:
...
def foo(self, value: str | int) -> str | int:
return 1 |
I agree the original declaration seems reasonable. FWIW, there seems to be a workaround using Protocols (low confidence in there not being something wrong with it): from abc import ABC, abstractmethod
from typing import Protocol, overload
class FooMethod(Protocol):
@overload
def __call__(self, value: int) -> int:
...
@overload
def __call__(self, value: str) -> str:
...
class Foo(ABC):
@property
@abstractmethod
def foo(self) -> FooMethod:
...
class Bar(Foo):
@overload
def foo(self, value: int) -> int:
...
@overload
def foo(self, value: str) -> str:
...
def foo(self, value):
return 1 |
I'm not sure that this is the same issue, but at least very similar. It would be great to allow omitting implementation in from __future__ import annotations
from typing import overload, TYPE_CHECKING
if TYPE_CHECKING:
@overload # E: An overloaded function outside a stub file must have an implementation [no-overload-impl]
def f(self, obj: int) -> int: ...
@overload
def f(self, obj: str) -> str: ...
else:
from other.module import f Here's a playground with this sample. Implementation signature will be discarded anyway, and there is nothing to typecheck. Discovered in this SO answer. |
@sterliakov i would raise a separate issue for that |
Some specific logic might be needed for this case. Though you couldn't instantiate |
Would it be possible to make the base class a generic bases on T input type and U output type. Hmm nope this does not work:
Errors:
|
…an implementation is not required. This is related to mypy issue python/mypy#11488.
https://mypy-play.net/?mypy=latest&python=3.10&gist=9c19741f8c5682b5854d59f3cf5f9ade
the implementation on the abstract class serves no purpose and should not be required imo
The text was updated successfully, but these errors were encountered: