Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

MonadChains
Copy link
Contributor

@MonadChains MonadChains commented Dec 4, 2022

Closes #99535.
I've added a simple test for checking that annotations are not inherited by derived classes and update the documentation about annotations

@MonadChains MonadChains requested a review from sobolevn Dec 5, 2022
Copy link
Member

@sobolevn sobolevn left a comment

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.

@netlify
Copy link

netlify bot commented Dec 6, 2022

Deploy Preview for python-cpython-preview ready!

Name Link
🔨 Latest commit 671a262
🔍 Latest deploy log https://app.netlify.com/sites/python-cpython-preview/deploys/638fd2b6fb1129000819b84b
😎 Deploy Preview https://deploy-preview-99990--python-cpython-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@MonadChains
Copy link
Contributor Author

MonadChains commented Dec 6, 2022

@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})
Copy link
Member

@JelleZijlstra JelleZijlstra Dec 7, 2022

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?

Suggested change
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
Copy link
Member

@JelleZijlstra JelleZijlstra Dec 7, 2022

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.
Copy link
Member

@JelleZijlstra JelleZijlstra Dec 7, 2022

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.

@JelleZijlstra JelleZijlstra requested a review from larryhastings Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

__annotations__ are not inherited in 3.10 while they are in 3.8
4 participants