I'm wondering, what is the best practices to handle abstract property type validation?
from abc import ABC, abstractmethod
class Base(ABC):
@property
@abstractmethod
def name(self):
"""
:type str
"""
pass
class MyClass(Base):
name = 1 # TypeError
Use __init_subclass__
. You don't name
to be a property at all, abstract or otherwise.
class Base:
def __init_subclass__(cls, *args, **kwargs):
super().__init_subclass__(**kwargs)
if not isinstance(cls.name, str):
raise TypeError(f'{cls.name} is not a string value')
class MyClass(Base):
name = 1