-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
29 lines (24 loc) · 1.28 KB
/
utils.py
File metadata and controls
29 lines (24 loc) · 1.28 KB
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
26
27
28
29
from .interfaces import AllSemanticsFleafAPI
__all__ = ["inherit_annotations"]
def inherit_annotations(property_method=None, from_class=AllSemanticsFleafAPI):
"""Return a decorator to inherit method annotations from a given class.
If this is used to inherit annotations for a property method, the property decorator
needs to be above/outside this decorator, and property_method should be set appropiately.
Args:
property_method: An optional string of either "fget", "fset", "fdel".
If this is not None, this will instead copy the annotations from either
the getter, setter or deleter of a property with the same name.
from_class: A class to search for a method with the same name that defines annotations.
"""
def decorator(method):
for cls in from_class.__mro__:
parent_method = getattr(cls, method.__name__, None)
if property_method:
parent_method = getattr(parent_method, property_method, None)
if parent_method:
parent_annotations = getattr(parent_method, "__annotations__", {})
if parent_annotations:
setattr(method, "__annotations__", parent_annotations)
break
return method
return decorator