From 0787052d9fda0d54173f7c2cb6b6451def193ff6 Mon Sep 17 00:00:00 2001 From: monadchains Date: Sun, 4 Dec 2022 14:52:39 +0100 Subject: [PATCH 1/6] Add test for inheritance of annotations and update documentation --- Doc/howto/annotations.rst | 4 ++++ Lib/test/test_grammar.py | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/Doc/howto/annotations.rst b/Doc/howto/annotations.rst index 2bc2f2d4c839e2..c63814492c99e5 100644 --- a/Doc/howto/annotations.rst +++ b/Doc/howto/annotations.rst @@ -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. + Accessing The Annotations Dict Of An Object In Python 3.9 And Older =================================================================== diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index 58f907eac09d53..81ee5242d8ae29 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -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): + attr: str + self.assertEqual(A.__annotations__, {"attr": int}) + self.assertEqual(B.__annotations__, {}) + self.assertEqual(C.__annotations__, {"attr" : str}) + + def test_var_annot_metaclass_semantics(self): class CMeta(type): @classmethod From f5c7cee895128170e359f597ce43fd8785860094 Mon Sep 17 00:00:00 2001 From: monadchains Date: Sun, 4 Dec 2022 15:53:20 +0100 Subject: [PATCH 2/6] Add multiple inheritance case to test --- Lib/test/test_grammar.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index 81ee5242d8ae29..b77362601f6f98 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -423,9 +423,17 @@ 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(E.__annotations__, {}) + self.assertEqual(F.__annotations__, {}) def test_var_annot_metaclass_semantics(self): From 671a262a77003415debe81560515330af38fc2e7 Mon Sep 17 00:00:00 2001 From: monadchains Date: Wed, 7 Dec 2022 00:39:17 +0100 Subject: [PATCH 3/6] Add note to get_type_hints --- Doc/library/typing.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 94c9cb11f02d6d..66a4c648839fcf 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2773,6 +2773,10 @@ Introspection helpers .. versionchanged:: 3.9 Added ``include_extras`` parameter as part of :pep:`593`. + .. versionchanged:: 3.10 + ``__annotations__`` of a class does not contain anymore the annotations + of its base classes. + .. versionchanged:: 3.11 Previously, ``Optional[t]`` was added for function and method annotations if a default value equal to ``None`` was set. From 24a118e89fc44871ac0dc4fce373f41a712438c1 Mon Sep 17 00:00:00 2001 From: monadchains Date: Wed, 7 Dec 2022 19:13:35 +0100 Subject: [PATCH 4/6] Add additional test --- Lib/test/test_grammar.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index b77362601f6f98..5b946020994e31 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -432,6 +432,7 @@ class F(C, A): 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__, {}) From 67a0175932952f1d39b39c73b3b9951a59fc15ae Mon Sep 17 00:00:00 2001 From: monadchains Date: Mon, 19 Dec 2022 00:14:12 +0100 Subject: [PATCH 5/6] Rework annotations.rst addition --- Doc/howto/annotations.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Doc/howto/annotations.rst b/Doc/howto/annotations.rst index c63814492c99e5..472069032d6509 100644 --- a/Doc/howto/annotations.rst +++ b/Doc/howto/annotations.rst @@ -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. Accessing The Annotations Dict Of An Object In Python 3.9 And Older From ad64aab06e7bb33f9b76f86e6fe4a0c5dc3fc090 Mon Sep 17 00:00:00 2001 From: monadchains Date: Sat, 24 Dec 2022 19:36:27 +0100 Subject: [PATCH 6/6] Improve get_type_hints entry --- Doc/library/typing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 66a4c648839fcf..869f41b9c984f2 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2774,7 +2774,7 @@ Introspection helpers Added ``include_extras`` parameter as part of :pep:`593`. .. versionchanged:: 3.10 - ``__annotations__`` of a class does not contain anymore the annotations + Calling ``get_type_hints()`` on a class no longer returns the annotations of its base classes. .. versionchanged:: 3.11 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy