Skip to content

Commit 00b5a08

Browse files
gh-97799: use inspect.get_annotations in dataclass (#97800)
dataclass used to get the annotations on a class object using cls.__dict__.get('__annotations__'). Now that it always imports inspect, it can use inspect.get_annotations, which is modern best practice for coping with annotations.
1 parent d78aa4e commit 00b5a08

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

Lib/dataclasses.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -920,10 +920,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
920920
if getattr(b, _PARAMS).frozen:
921921
any_frozen_base = True
922922

923-
# Annotations that are defined in this class (not in base
924-
# classes). If __annotations__ isn't present, then this class
925-
# adds no new annotations. We use this to compute fields that are
926-
# added by this class.
923+
# Annotations defined specifically in this class (not in base classes).
927924
#
928925
# Fields are found from cls_annotations, which is guaranteed to be
929926
# ordered. Default values are from class attributes, if a field
@@ -932,7 +929,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
932929
# actual default value. Pseudo-fields ClassVars and InitVars are
933930
# included, despite the fact that they're not real fields. That's
934931
# dealt with later.
935-
cls_annotations = cls.__dict__.get('__annotations__', {})
932+
cls_annotations = inspect.get_annotations(cls)
936933

937934
# Now find fields in our class. While doing so, validate some
938935
# things, and set the default values (as class attributes) where
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`dataclass` now uses :func:`inspect.get_annotations` to examine the
2+
annotations on class objects.

0 commit comments

Comments
 (0)