Why does typing.get_type_hints ignore attributes from metaclasses?

The typing.get_type_hints function doesn’t find annotations for attributes inherited from metaclasses, unlike attributes inherited from ordinary classes:

>>> class Meta(type):
...   x: int = 42
... 
>>> class Base(metaclass=Meta):
...   y: float = 3.14
... 
>>> Base.x
42
>>> import typing
>>> typing.get_type_hints(Base)
{'y': <class 'float'>}

Is there a reason for this behavior?

Also, is the procedure for looking up attributes of a class described in full somewhere in the documentation? None of the references I could find explicitly mention the possibility of finding the attribute on the metaclass, and the metaclass isn’t included in the class’s MRO, so it’s not clear what the behavior is supposed to be. (If I understand the CPython implementation correctly, when there are no descriptors involved the search order is the MRO of the class first, followed by the MRO of its metaclass.)

Hmm, if no one has any ideas for why get_type_hints behaves this way perhaps I should report it as a bug. The behavior is definitely counterintuitive to me.

I think the reason for this behavior is probably simply that metaclasses are used infrequently, and typing is still used by a minority of Python users, so nobody has considered whether this behavior makes sense before now :slight_smile:

I’m not sure I’d consider improving the behavior here a bugfix, but I’d certainly be happy to consider making the improvement. (I’m a typing.py maintainer.) I’m unlikely to file a PR for this myself, though, so your chances of getting the behavior changed will go up a lot if you’re able to find the time to submit a PR yourself!