Maybe get-type-hints can be refactored to make writing such a function
simpler. IIRC the part that takes a single annotation and evaluates it is a
private function.

On Tue, Aug 4, 2020 at 12:57 David Mertz <me...@gnosis.cx> wrote:

> 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.
>
> On Tue, Aug 4, 2020, 7:42 AM Dominik Vilsmeier <dominik.vilsme...@gmx.de>
> wrote:
>
>> 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-le...@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/
>>
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@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/OBZ5UEYTPEJG65MYFYMSBUTI46N7HSW7/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-- 
--Guido (mobile)
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/EMGFABAEMVUEEJMGM7STLBVP2TWRSS3R/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to