[New-bugs-announce] [issue44468] Shouldn't `typing.get_type_hints()` default `globalns` to `{}` instead of skipping base classes?
Will Chen
report at bugs.python.org
Mon Jun 21 00:11:39 EDT 2021
New submission from Will Chen <inbox at intralexical.studio>:
An issue was recently closed that caused synthetic classes and base classes with invalid `__module__` attributes to raise `KeyError()` in `typing.get_type_hints()`:
https://bugs.python.org/issue41515
However, the implemented solution appears to be to skip those classes completely with a `continue` statement, instead of getting the annotations that may still be present by using an empty globals dictonary:
https://github.com/python/cpython/pull/25352/files#diff-ddb987fca5f5df0c9a2f5521ed687919d70bb3d64eaeb8021f98833a2a716887
In order to work around this issue in my local install of Blender, I had to change `.get_type_hints()` to use an empty dictionary for `globalns` when encountering invalid modules, rather than skipping them:
https://developer.blender.org/T88986#1179812
>From reading the commit where the broken behaviour was first introduced— Which was described/designed as "backwards compatible"— It looks like the original behaviour was also to use an empty dictionary, and never skip:
https://github.com/python/cpython/commit/f350a268a7071ce7d7a5bb86a9b1229782d4963b#diff-ddb987fca5f5df0c9a2f5521ed687919d70bb3d64eaeb8021f98833a2a716887R1501
Using an empty dictionary also seemed to be mentioned in the bug report linked above.
IMO using an empty dictionary and still returning annotations from classes with invalid modules seems like it'd be more sensible, predictable, and backwards-compatible, while skipping base classes is likely to just replace the obvious `KeyError()` with less reproducible and nastier errors caused by returning incomplete type hints.
----------
messages: 396205
nosy: willchencontact
priority: normal
severity: normal
status: open
title: Shouldn't `typing.get_type_hints()` default `globalns` to `{}` instead of skipping base classes?
type: behavior
versions: Python 3.10, Python 3.11
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44468>
_______________________________________
More information about the New-bugs-announce
mailing list