Skip to content

Commit 375d37a

Browse files
authored
fix(corekit): read only a class's own annotations when building an Info constructor (#1692) (#1702)
Below Python 3.10 (bpo-43901), cls.__annotations__ of a class declaring none is its nearest base's. info_final therefore built a field-less Info subclass's __init__ from Info's own __finalised__, __additional__ and __excluded__ on 3.6-3.9, and _nested_types resolved an inherited annotation in the subclass's namespace rather than the declaring class's. - infoclass: _own_annotations reads a class's own __dict__ below 3.10 and inspect.get_annotations from 3.10 on, where 3.14's lazy annotations are not in __dict__. info_final and _nested_types both use it. - tests: OwnAnnotationsTests pins the generated signature of a field-less subclass, a subclass of one, a class with fields and a diamond, plus a from_dict rebuild through a field-less subclass; the signature check also runs through the pre-3.10 reader on every version. The issue's three tests and the new ones fail on origin/main under 3.8 and 3.9 and pass here on 3.8, 3.9, 3.10 and 3.14.
1 parent 24d8f23 commit 375d37a

2 files changed

Lines changed: 148 additions & 3 deletions

File tree

‎pcapkit/corekit/infoclass.py‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import collections.abc
1818
import contextlib
1919
import enum
20+
import inspect
2021
import itertools
2122
import sys
2223
import types
@@ -69,6 +70,33 @@ class FinalisedState(EnumLookup, enum.IntEnum):
6970
FINAL = enum.auto()
7071

7172

73+
#: :func:`inspect.get_annotations`, new in Python 3.10, or :data:`None` below it.
74+
_get_annotations = getattr(inspect, 'get_annotations', None)
75+
76+
77+
def _own_annotations(cls: 'type') -> 'dict[str, Any]':
78+
"""Return the annotations ``cls`` declares itself, never those of a base.
79+
80+
Below Python 3.10 (bpo-43901), ``cls.__annotations__`` of a class whose body
81+
declares none is its nearest base's, so a field-less :class:`Info`
82+
subclass read :class:`Info`'s own ``__finalised__``, ``__additional__`` and
83+
``__excluded__`` as its fields (:issue:`1692`). There, a class's own
84+
annotations are in its ``__dict__``; from 3.10 on, they are read with
85+
:func:`inspect.get_annotations`, as from 3.14 they are evaluated lazily and
86+
are not in ``__dict__`` at all.
87+
88+
Args:
89+
cls: Class to read.
90+
91+
Returns:
92+
The annotations ``cls``'s own body declares, which may be empty.
93+
94+
"""
95+
if _get_annotations is None:
96+
return cls.__dict__.get('__annotations__', {})
97+
return _get_annotations(cls)
98+
99+
72100
def info_final(cls: 'ST', *, _finalised: 'bool' = True) -> 'ST':
73101
"""Finalise info class.
74102
@@ -166,8 +194,10 @@ def info_final(cls: 'ST', *, _finalised: 'bool' = True) -> 'ST':
166194
break
167195

168196
# NOTE: We iterate in reversed order to ensure that the type
169-
# annotations of the superclasses are considered first.
170-
for key in reversed(cls_.__annotations__):
197+
# annotations of the superclasses are considered first. Each class
198+
# contributes only the annotations it declares itself (see
199+
# :func:`_own_annotations`), on every Python version.
200+
for key in reversed(_own_annotations(cls_)):
171201
# NOTE: We skip duplicated annotations to avoid duplicate
172202
# argument in function definition.
173203
if key in args_:
@@ -1161,7 +1191,7 @@ def _nested_types(cls: 'Type[Info]') -> 'dict[str, Type[Info]]':
11611191
module = sys.modules.get(cls_.__module__)
11621192
globalns = dict(vars(module)) if module is not None else {}
11631193
localns = dict(vars(cls_))
1164-
for (key, ann) in cls_.__dict__.get('__annotations__', getattr(cls_, '__annotations__', {})).items():
1194+
for (key, ann) in _own_annotations(cls_).items():
11651195
if key in seen:
11661196
continue
11671197
seen.add(key)

‎tests/corekit/test_infoclass.py‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,120 @@ class Child(Base):
103103
self.assertEqual(Info.from_dict(c=3).to_dict().to_dict(), {'c': 3})
104104

105105

106+
@unittest.skipUnless(HAS_RUNTIME, 'runtime dependencies not installed')
107+
class OwnAnnotationsTests(unittest.TestCase):
108+
"""GitHub issue #1692: each class contributes only the annotations it declares.
109+
110+
Below Python 3.10 (bpo-43901), ``cls.__annotations__`` of a class declaring
111+
none is its nearest base's. These pin the 3.10+ answers on every version.
112+
113+
"""
114+
115+
#: Generated ``__init__`` signature of each class :meth:`_signatures` builds.
116+
EXPECTED = {
117+
'Bare': '(self, dict_=None, **kwargs)',
118+
'BareChild': '(self, dict_=None, **kwargs)',
119+
'Point': '(self, x, y)',
120+
'PointChild': '(self, x, y)',
121+
'Chain': '(self, a, c)',
122+
# NOTE: MRO ``Diamond, B, C, A``. Reading ``B.__annotations__`` gave
123+
# ``A``'s ``a`` ahead of ``C``'s ``c``, i.e. ``(self, c, a, d)``.
124+
'Diamond': '(self, a, c, d)',
125+
}
126+
127+
def setUp(self) -> None:
128+
reimport_once_per_class(self)
129+
130+
@staticmethod
131+
def _signatures() -> dict[str, str]:
132+
"""Build fresh classes, finalise them, and return each ``__init__`` signature."""
133+
import inspect
134+
135+
from pcapkit.corekit.infoclass import Info, info_final
136+
137+
class Bare(Info):
138+
pass
139+
140+
class BareChild(Bare):
141+
pass
142+
143+
class Point(Info):
144+
x: int
145+
y: int
146+
147+
class PointChild(Point):
148+
pass
149+
150+
class A(Info):
151+
a: int
152+
153+
class B(A):
154+
pass
155+
156+
class Chain(B):
157+
c: int
158+
159+
class C(A):
160+
c: int
161+
162+
class Diamond(B, C):
163+
d: int
164+
165+
signatures = {}
166+
for cls in (Bare, BareChild, Point, PointChild, Chain, Diamond):
167+
# NOTE: ``_finalised=False`` is what :meth:`Info.__new__` passes; it
168+
# leaves the class open, so ``BareChild`` can follow ``Bare``.
169+
info_final(cls, _finalised=False)
170+
signatures[cls.__name__] = str(inspect.signature(cls.__init__))
171+
return signatures
172+
173+
def test_generated_signature_is_the_same_on_every_version(self) -> None:
174+
self.assertEqual(self._signatures(), self.EXPECTED)
175+
176+
from pcapkit.corekit.infoclass import Info
177+
178+
class Bare(Info):
179+
pass
180+
181+
class BareChild(Bare):
182+
pass
183+
184+
for cls in (Bare, BareChild):
185+
with self.subTest(cls=cls.__name__):
186+
info = cls({'a': 1}, b=2)
187+
self.assertEqual(list(info.items()), [('a', 1), ('b', 2)])
188+
189+
def test_the_pre_3_10_reader_gives_the_same_signatures(self) -> None:
190+
# NOTE: ``_get_annotations`` set to ``None`` selects the reader Python
191+
# 3.6-3.9 use, so every leg checks that branch. It is sound on 3.14 too
192+
# because this module's ``from __future__ import annotations`` stores a
193+
# class's own annotations in its ``__dict__`` there as well.
194+
from pcapkit.corekit import infoclass
195+
196+
with mock.patch.object(infoclass, '_get_annotations', None):
197+
self.assertEqual(self._signatures(), self.EXPECTED)
198+
199+
def test_from_dict_rebuilds_a_field_inherited_by_a_field_less_subclass(self) -> None:
200+
from pcapkit.corekit.infoclass import Info
201+
202+
class Leaf(Info):
203+
v: int
204+
205+
class Holder(Info):
206+
# NOTE: an annotation is resolved in the namespace of the class
207+
# declaring it; ``Kind`` stands in for a base module's globals.
208+
# Below 3.10, ``Sub`` read ``child`` as its own and resolved it in
209+
# its own namespace, where ``Kind`` is unknown, so it was skipped.
210+
Kind = Leaf
211+
child: Kind
212+
213+
class Sub(Holder):
214+
pass
215+
216+
sub = Sub.from_dict(child={'v': 1})
217+
self.assertIsInstance(sub['child'], Leaf)
218+
self.assertEqual(sub['child']['v'], 1)
219+
220+
106221
if __name__ == '__main__':
107222
unittest.main()

0 commit comments

Comments
 (0)