GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-99535: Add test for inheritance of annotations and update documentation by MonadChains · Pull Request #99990 · python/cpython

gh-99535: Add test for inheritance of annotations and update documentation #99990

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.

Sign up for GitHub

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

Merged
JelleZijlstra merged 6 commits into python:main from MonadChains:gh-99535/annotations_inheritance_test_and_doc
Dec 24, 2022

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-99535: Add test for inheritance of annotations and update documentation #99990

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
6 commits
Select commit Hold shift + click to select a range
0787052
Add test for inheritance of annotations and update documentation
MonadChains Dec 4, 2022
f5c7cee
Add multiple inheritance case to test
MonadChains Dec 4, 2022
671a262
Add note to get_type_hints
MonadChains Dec 6, 2022
24a118e
Add additional test
MonadChains Dec 7, 2022
67a0175
Rework annotations.rst addition
MonadChains Dec 18, 2022
ad64aab
Improve get_type_hints entry
MonadChains Dec 24, 2022
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

6 changes: 6 additions & 0 deletions 6 Doc/howto/annotations.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ Accessing The Annotations Dict Of An Object In Python 3.10 And Newer
newer is to call :func:`getattr` with three arguments,
for example ``getattr(o, '__annotations__', None)``.

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.
Comment thread
Comment on lines +60 to +64

@AlexWaygood AlexWaygood Jan 7, 2023

Copy link
Copy Markdown
Member

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.

As mentioned in #99535 (comment), there is one edge case where this isn't true: TypedDicts still inherit a parent class's __annotations__, even in Python 3.10. I'm not sure whether it's important to mention that or not -- @hauntsaninja, do you have any thoughts on that?

Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec  6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import TypedDict
>>> class Foo(TypedDict):
...     x: int
...
>>> class Bar(Foo): ...
...
>>> Bar.__annotations__
{'x': <class 'int'>}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@hauntsaninja hauntsaninja Jan 7, 2023

Copy link
Copy Markdown
Contributor

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 think it's fine not to mention over here:

  1. The special case is more that TypedDicts don't have a runtime subclassing relationship, rather than anything to do with __annotations__. In other words, if you know how TypedDict subclassing works at runtime (which is presumably documented somewhere), you can reasonable expect this __annotations__ behaviour. So adding more words here might be confusing; the statement about the language remains true
  2. My understanding of "HOWTO"s is that they're allowed to skip some of the gross details

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction


Accessing The Annotations Dict Of An Object In Python 3.9 And Older
===================================================================
Expand Down
4 changes: 4 additions & 0 deletions 4 Doc/library/typing.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2773,6 +2773,10 @@ Introspection helpers
.. versionchanged:: 3.9
Added ``include_extras`` parameter as part of :pep:`593`.

.. versionchanged:: 3.10
Calling ``get_type_hints()`` on a class no longer returns the annotations
of its base classes.

.. versionchanged:: 3.11
Previously, ``Optional[t]`` was added for function and method annotations
if a default value equal to ``None`` was set.
Expand Down
22 changes: 22 additions & 0 deletions 22 Lib/test/test_grammar.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,28 @@ class Cbad2(C):
x: int
x.y: list = []

def test_annotations_inheritance(self):
# Check that annotations are not inherited by derived classes
class A:
attr: int
class B(A):
pass
class C(A):
Comment thread
MonadChains marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

attr: str
class D:
attr2: int
class E(A, D):
pass
class F(C, A):
pass
self.assertEqual(A.__annotations__, {"attr": int})
self.assertEqual(B.__annotations__, {})
self.assertEqual(C.__annotations__, {"attr" : str})
Comment thread
MonadChains marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

self.assertEqual(D.__annotations__, {"attr2" : int})
self.assertEqual(E.__annotations__, {})
self.assertEqual(F.__annotations__, {})


def test_var_annot_metaclass_semantics(self):
class CMeta(type):
@classmethod
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.