Skip to content

Commit

Permalink
pythongh-97799: use inspect.get_annotations in dataclass (python#97800)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
larryhastings authored Oct 3, 2022
1 parent d78aa4e commit 00b5a08
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
7 changes: 2 additions & 5 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,10 +920,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
if getattr(b, _PARAMS).frozen:
any_frozen_base = True

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

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

0 comments on commit 00b5a08

Please sign in to comment.