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 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
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
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):
MonadChains marked this conversation as resolved.
Show resolved Hide resolved
attr: str
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


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