Skip to content

Commit

Permalink
[3.10] pythongh-99535: Add test for inheritance of annotations and up…
Browse files Browse the repository at this point in the history
…date documentation (pythonGH-99990).

(cherry picked from commit f5b7b19)

Co-authored-by: MonadChains <monadchains@gmail.com>
  • Loading branch information
MonadChains authored and JelleZijlstra committed Dec 24, 2022
1 parent 3ea6f7f commit bd00e25
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Doc/howto/annotations.rst
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.


Accessing The Annotations Dict Of An Object In Python 3.9 And Older
===================================================================
Expand Down
4 changes: 4 additions & 0 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2233,6 +2233,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.

.. function:: get_args(tp)
.. function:: get_origin(tp)

Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,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):
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})
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

0 comments on commit bd00e25

Please sign in to comment.