In one of my projects I'm reusing class-level type annotations to identify
relevant attributes for serialization, e.g. similar to the following:
attrs = {name: getattr(obj, name) for name in get_type_hints(type(obj))}
This is convenient because it merges the type annotations from the various
stages in the class hierarchy, e.g.
class Base:
a: int
class Derived(Base):
b: str
results in `attrs == dict(a=..., b=...)`.
However it becomes inconvenient if external base classes are involved that
define their own, unrelated type annotations, e.g.
class External: # from some other distribution
unrelated: float
class Base(External):
a: int
It would be helpful if `get_type_hints` had a `boundary` parameter that, when
used with a class object, determines the upper boundary for the MRO. So it
could be used in the following way:
get_type_hints(type(derived_obj), boundary=Base)
to exclude any type annotations further up the class hierarchy (including the
ones from `Base`).
Regarding the implementation this would effectively skip over base classes in
the reverse MRO until it reaches the `boundary`.
What do you think?
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/T6K4DWENPM7LYXSDVYQYDVFEVBMA5K3L/
Code of Conduct: http://python.org/psf/codeofconduct/