New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-99535: Add test for inheritance of annotations and update documentation #99990
base: main
Are you sure you want to change the base?
gh-99535: Add test for inheritance of annotations and update documentation #99990
Conversation
LGTM, except one more idea: let's add a note here as well: https://docs.python.org/3/library/typing.html#typing.get_type_hints
I think that the common way to mark version changes is .. versionchanged::
directive.
This will notify users of get_type_hints
(who are the most affected) about this.
|
Name | Link |
---|---|
671a262 | |
https://app.netlify.com/sites/python-cpython-preview/deploys/638fd2b6fb1129000819b84b | |
https://deploy-preview-99990--python-cpython-preview.netlify.app | |
To edit notification comments on pull requests, go to your Netlify site settings.
@sobolevn I've added the note to the function. |
pass | ||
self.assertEqual(A.__annotations__, {"attr": int}) | ||
self.assertEqual(B.__annotations__, {}) | ||
self.assertEqual(C.__annotations__, {"attr" : str}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not test D's annotations?
self.assertEqual(C.__annotations__, {"attr" : str}) | |
self.assertEqual(C.__annotations__, {"attr": str}) | |
self.assertEqual(D.__annotations__, {"attr2": int}) |
@@ -2773,6 +2773,10 @@ Introspection helpers | |||
.. versionchanged:: 3.9 | |||
Added ``include_extras`` parameter as part of :pep:`593`. | |||
|
|||
.. versionchanged:: 3.10 | |||
``__annotations__`` of a class does not contain anymore the annotations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels weird to mention that here since there's no change to get_type_hints
itself.
Starting from Python 3.10, accessing the annotations | ||
of a class will not give anymore the ones of its base | ||
classes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say something like this:
Before Python 3.10, accessing __annotations__
on a class that defines no annotations but that has a parent class with annotations would return the parent's __annotations__
. In Python 3.10 and newer, the child class's annotations will be an empty dict instead.
Closes #99535.
I've added a simple test for checking that annotations are not inherited by derived classes and update the documentation about annotations
__annotations__
are not inherited in 3.10 while they are in 3.8 #99535