mirror of
https://github.com/python/cpython.git
synced 2025-10-04 22:20:46 +00:00
bpo-32896: Fix error when subclassing a dataclass with a field that uses a default_factory (GH-6170) (GH-6171)
Fix the way that new annotations in a class are detected.
(cherry picked from commit 8f6eccdc64
)
Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
This commit is contained in:
parent
56f530ef8a
commit
22136c94b6
3 changed files with 62 additions and 10 deletions
|
@ -574,17 +574,18 @@ def _get_field(cls, a_name, a_type):
|
|||
|
||||
def _find_fields(cls):
|
||||
# Return a list of Field objects, in order, for this class (and no
|
||||
# base classes). Fields are found from __annotations__ (which is
|
||||
# guaranteed to be ordered). Default values are from class
|
||||
# attributes, if a field has a default. If the default value is
|
||||
# a Field(), then it contains additional info beyond (and
|
||||
# possibly including) the actual default value. Pseudo-fields
|
||||
# ClassVars and InitVars are included, despite the fact that
|
||||
# they're not real fields. That's dealt with later.
|
||||
# base classes). Fields are found from the class dict's
|
||||
# __annotations__ (which is guaranteed to be ordered). Default
|
||||
# values are from class attributes, if a field has a default. If
|
||||
# the default value is a Field(), then it contains additional
|
||||
# info beyond (and possibly including) the actual default value.
|
||||
# Pseudo-fields ClassVars and InitVars are included, despite the
|
||||
# fact that they're not real fields. That's dealt with later.
|
||||
|
||||
annotations = getattr(cls, '__annotations__', {})
|
||||
return [_get_field(cls, a_name, a_type)
|
||||
for a_name, a_type in annotations.items()]
|
||||
# If __annotations__ isn't present, then this class adds no new
|
||||
# annotations.
|
||||
annotations = cls.__dict__.get('__annotations__', {})
|
||||
return [_get_field(cls, name, type) for name, type in annotations.items()]
|
||||
|
||||
|
||||
def _set_new_attribute(cls, name, value):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue