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

Merged
Show file tree
Hide file tree
Changes from 2 commits
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
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.
MonadChains marked this conversation as resolved.
Show resolved Hide resolved


Accessing The Annotations Dict Of An Object In Python 3.9 And Older
===================================================================
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,27 @@ 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):
MonadChains marked this conversation as resolved.
Show resolved Hide resolved
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})
MonadChains marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(E.__annotations__, {})
self.assertEqual(F.__annotations__, {})


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