diff options
author | Eric V. Smith <ericvsmith@users.noreply.github.com> | 2018-03-21 02:00:23 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-21 02:00:23 (GMT) |
commit | 8f6eccdc64cab735c47620fea948e64b19f83684 (patch) | |
tree | 0b77e74193fd10e8cae55446300ae7b3c56225f6 /Lib/dataclasses.py | |
parent | 10b134a07c898c2fbc5fd3582503680a54ed80a2 (diff) | |
download | cpython-8f6eccdc64cab735c47620fea948e64b19f83684.zip cpython-8f6eccdc64cab735c47620fea948e64b19f83684.tar.gz cpython-8f6eccdc64cab735c47620fea948e64b19f83684.tar.bz2 |
bpo-32896: Fix error when subclassing a dataclass with a field that uses a default_factory (GH-6170)
Fix the way that new annotations in a class are detected.
Diffstat (limited to 'Lib/dataclasses.py')
-rw-r--r-- | Lib/dataclasses.py | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index a4afd50..d616432 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -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. - - annotations = getattr(cls, '__annotations__', {}) - return [_get_field(cls, a_name, a_type) - for a_name, a_type in annotations.items()] + # 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. + + # 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): |