Skip to content

Commit

Permalink
Initial attempt at hoisting type annotations from the descriptors to …
Browse files Browse the repository at this point in the history
…the containing class.
  • Loading branch information
amcgregor committed Oct 21, 2019
1 parent 050879c commit c2a4554
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions marrow/schema/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,13 @@ class DataAttribute(Element):
The base attribute class which implements the descriptor protocol, pulling the instance value of the attribute from
the containing object's ``__data__`` dictionary. If an attempt is made to read an attribute that does not have a
corresponding value in the data dictionary an ``AttributeError`` will be raised.
Python type annotations relating to the value read/written on instances may be declared as the `annotation`. This
will be used to populate the class `__annotations__` for any class an instance of this is assigned to.
"""

annotation = None

def __get__(self, obj, cls=None):
"""Executed when retrieving a DataAttribute instance attribute."""

Expand Down
2 changes: 2 additions & 0 deletions marrow/schema/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def process(name, attr):
# Iteratively process the Element subclass instances and update their definition.
attributes.update(process(k, v) for k, v in attrs.items() if isinstance(v, Element))
attrs['__attributes__'] = odict(sorted(attributes.items(), key=lambda t: t[1].__sequence__))
attrs.setdefault('__annotations__', dict())
attrs['__annotations__'].update({k: v.annotation for k, v in attributes.items() if hasattr(v, 'annotation')})

# Construct the new class.
cls = type.__new__(meta, str(name), bases, attrs)
Expand Down

0 comments on commit c2a4554

Please sign in to comment.