Skip to content
Merged
Show file tree
Hide file tree
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
Next Next commit
Add test for inheritance of annotations and update documentation
  • Loading branch information
MonadChains committed Dec 4, 2022
commit 0787052d9fda0d54173f7c2cb6b6451def193ff6
4 changes: 4 additions & 0 deletions Doc/howto/annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ 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.
Comment thread
MonadChains marked this conversation as resolved.
Outdated


Accessing The Annotations Dict Of An Object In Python 3.9 And Older
===================================================================
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,19 @@ 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.
attr: str
self.assertEqual(A.__annotations__, {"attr": int})
self.assertEqual(B.__annotations__, {})
self.assertEqual(C.__annotations__, {"attr" : str})
Comment thread
MonadChains marked this conversation as resolved.


def test_var_annot_metaclass_semantics(self):
class CMeta(type):
@classmethod
Expand Down