Is this acceptable?
from abc import ABC, abstractmethod
from typing import Any
class A(ABC):
@abstractmethod
def test(self) -> Any:
...
class B(A):
def test(self) -> int:
return 1
class C(A):
def test(self) -> str:
return "yes"
To clarify, I know they can, I just want to know if this is acceptable in terms of good practices.
Technically yes, but it would be much better to define a generic return type and have the subclasses return subclasses of this return type.
For example, you could define a class GenericType
and have two subclasses GenericTypeA
and GenericTypeB
. Then you could do something like this:
from abc import ABC, abstractmethod
class A(ABC):
@abstractmethod
def test(self) -> GenericType:
...
class B(A):
def test(self) -> GenericTypeA:
...
class C(A):
def test(self) -> GenericTypeB:
...
This way class A is telling the user that the test method always returns a GenericType
object, which is itself an abstract class. That way the user knows that any subclass of class A will return a subclass of GenericType
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With