This definitely feels to me like though if an oddball case that "write your own function" seems like the best solution. I accept the OP needs it, but I have trouble imagining that many others would.
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 -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/T6K4DWENPM7LYXSDVYQYDVFEVBMA5K3L/
Code of Conduct: http://python.org/psf/codeofconduct/