You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
__annotations__ are not inherited in 3.10 while they are in 3.8 #99535
I haven't checked the logs, but I suspect this is an intentional change made by @larryhastings as part of his efforts to improve __annotations__ handling. The 3.10+ behavior makes more sense to me: you Cls.__annotations__ should give you the annotations for the current class, not a base class.
@sobolevn I've added a small test to check the behavior of annotations w.r.t. inheritance. I've also updated the docs by adding a line to remark that annotations are not inherited anymore, but I'm not sure it is the right place for it.
i.e. __annotations__ are not inherited, the same as for normal classes. Annotations from base classes are still considered when deciding what fields the subclass should have, though, which is something specific to the use of annotations by NamedTuple. (The same is true for dataclasses.)
TypedDicts do indeed "inherit" __annotations__, but this is a special case because of TypedDicts having very unusual inheritance behavior. When you inherit a TypedDict, all you get is its __annotations__; the base class itself does not even appear in your MRO! Anything subclassing TypedDict or any subclass of TypedDict ends up simply inheriting dict:
The pull request introduced a documentation error for typing.get_type_hints. typing.get_type_hints recursively merges annotations from parent classes, which has always been intended behavior. The pull request mixed up this behavior with the __annotations__ inheritance change, and added an incorrect note to the typing.get_type_hints documentation claiming that typing.get_type_hints no longer does this.
Activity
sobolevn commentedon Nov 16, 2022
I think we don't have tests for this 🤭
At least, I am not able to find them.
We have tests for
TypedDictandNamedTuplethat inherits annotations from parents.But, not for:
Right now
TypedDicts andNamedTuples inherit all annotations from parents. Classes are inconsistent to them.Should we treat this as a bug? 🤔
sobolevn commentedon Nov 16, 2022
3.12
mainbranch:3.11 is the same.
3.10 is the same
3.9.9 is different:
So, yes. I think it is a bug. And should be fixed / backported to 3.10 and 3.11
Why?
Looks like empty
__annotations__attribute is populated in__dict__:Compare it to
3.9:JelleZijlstra commentedon Nov 16, 2022
I haven't checked the logs, but I suspect this is an intentional change made by @larryhastings as part of his efforts to improve
__annotations__handling. The 3.10+ behavior makes more sense to me: youCls.__annotations__should give you the annotations for the current class, not a base class.JelleZijlstra commentedon Nov 16, 2022
2f2b698 and #88067
sobolevn commentedon Nov 16, 2022
Yes, you are right. The change is intentional: #88067 (comment)
sobolevn commentedon Nov 16, 2022
So, adding tests and docs about it should be the way to go? 🤔
MonadChains commentedon Dec 4, 2022
@sobolevn I've added a small test to check the behavior of annotations w.r.t. inheritance. I've also updated the docs by adding a line to remark that annotations are not inherited anymore, but I'm not sure it is the right place for it.
carljm commentedon Dec 16, 2022
One additional note:
I don't think this is quite accurate. NamedTuples behave the same as normal classes as far as the
__annotations__attribute is concerned:i.e.
__annotations__are not inherited, the same as for normal classes. Annotations from base classes are still considered when deciding what fields the subclass should have, though, which is something specific to the use of annotations byNamedTuple. (The same is true for dataclasses.)TypedDicts do indeed "inherit"
__annotations__, but this is a special case because of TypedDicts having very unusual inheritance behavior. When you inherit a TypedDict, all you get is its__annotations__; the base class itself does not even appear in your MRO! Anything subclassingTypedDictor any subclass ofTypedDictends up simply inheritingdict:So in this special case "inheritance" of the
__annotations__attribute is necessary because the inheritance hierarchy itself is not preserved.gh-99535: Add test for inheritance of annotations and update document…
pythongh-99535: Add test for inheritance of annotations and update do…
[3.10] pythongh-99535: Add test for inheritance of annotations and up…
gh-99535: Add test for inheritance of annotations and update document…
[3.10] gh-99535: Add test for inheritance of annotations and update d…
Merged main into regmachine branch (#50)
FrozenBob commentedon Jan 1, 2023
The pull request introduced a documentation error for
typing.get_type_hints.typing.get_type_hintsrecursively merges annotations from parent classes, which has always been intended behavior. The pull request mixed up this behavior with the__annotations__inheritance change, and added an incorrect note to thetyping.get_type_hintsdocumentation claiming thattyping.get_type_hintsno longer does this.