Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rework annotations.rst addition
  • Loading branch information
MonadChains committed Dec 18, 2022
commit 67a0175932952f1d39b39c73b3b9951a59fc15ae
8 changes: 5 additions & 3 deletions Doc/howto/annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ 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)``.

Starting from Python 3.10, accessing the annotations
of a class will not give anymore the ones of its base
classes.
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 on lines +60 to +64
Copy link
Copy Markdown
Member

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'>}

Copy link
Copy Markdown
Contributor

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



Accessing The Annotations Dict Of An Object In Python 3.9 And Older
Expand Down