[Python-checkins] gh-99535: Add test for inheritance of annotations and update documentation (GH-99990)
miss-islington
webhook-mailer at python.org
Sat Dec 24 15:31:15 EST 2022
https://github.com/python/cpython/commit/44b664e057772d1491748809a6a356694b3d3979
commit: 44b664e057772d1491748809a6a356694b3d3979
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-12-24T12:31:10-08:00
summary:
gh-99535: Add test for inheritance of annotations and update documentation (GH-99990)
(cherry picked from commit f5b7b19bf10724d831285fb04e00f763838bd555)
Co-authored-by: MonadChains <monadchains at gmail.com>
files:
M Doc/howto/annotations.rst
M Doc/library/typing.rst
M Lib/test/test_grammar.py
diff --git a/Doc/howto/annotations.rst b/Doc/howto/annotations.rst
index 2bc2f2d4c839..472069032d65 100644
--- a/Doc/howto/annotations.rst
+++ b/Doc/howto/annotations.rst
@@ -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
===================================================================
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index 7fc0aa3a8e62..a3a6181eba29 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -2767,6 +2767,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.
+
.. versionchanged:: 3.11
Previously, ``Optional[t]`` was added for function and method annotations
if a default value equal to ``None`` was set.
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index da8851986271..bcd8d584b0f5 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -415,6 +415,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
More information about the Python-checkins
mailing list