-
Notifications
You must be signed in to change notification settings - Fork 1
/
abstract.py
25 lines (20 loc) · 931 Bytes
/
abstract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from typing import List
class Abstract:
"""
Custom implementation of abstract classes.
Set the `__required_methods__` and `__required_methods__` in the base class.
If they are missing in the child class, AttributeError is raised.
"""
__required_methods__: List[str] = []
__required_attributes__: List[str] = []
def __init_subclass__(cls, **kwargs):
if Abstract in cls.__bases__:
return
for name in cls.__required_attributes__:
if not hasattr(cls, name):
raise AttributeError(f'Missing attribute "{name}" in class "{cls}".')
for name in cls.__required_methods__:
if not hasattr(cls, name):
raise AttributeError(f'Missing method "{name}" in class "{cls}".')
if not callable(getattr(cls, name)):
raise AttributeError(f'Attribute "{name}" in class "{cls}" should be a method.')