This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: dataclass looks up field default value on the class, not the class's __dict__
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: eric.smith
Priority: normal Keywords:

Created on 2021-06-17 12:34 by eric.smith, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg396000 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-06-17 12:34
Consider:

class FtpHelper(ftplib.FTP):
    host: str
    baz: str = 'baz default'

>>> FtpHelper.host
''
>>> FtpHelper.baz
'baz default'
>>> getattr(FtpHelper, "host")
''
>>> getattr(FtpHelper, "baz")
'baz default'

But:
>>> FtpHelper.__dict__['host']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'host'
>>> FtpHelper.__dict__['baz']
'baz default'


Now make this a dataclass, without a default value for baz:

@dataclass
class FtpHelper(ftplib.FTP):
    host: str
    baz: str

This gives an error:
TypeError: non-default argument 'baz' follows default argument

And this is because it's picking up a default value for host from the base class ftplib.FTP, and generating an __init__ like:

def __init__(self, host='', baz):

@dataclass uses getattr(cls, field_name, MISSING) to find the default value for the field, but in this case that's wrong. I think what should happen is that it should use getattr(cls.__dict__, field_name, MISSING). This would be consistent with other features where dataclasses does not look in base classes for various things, but only in the class itself (like __hash__).
History
Date User Action Args
2022-04-11 14:59:46adminsetgithub: 88609
2021-06-17 14:07:17eric.smithsettitle: dataclass looks up default on the class, not the class's __dict__ -> dataclass looks up field default value on the class, not the class's __dict__
2021-06-17 12:46:32eric.smithsettype: behavior
2021-06-17 12:46:21eric.smithsettitle: dataclass looks up default on the class, not the classes __dict__ -> dataclass looks up default on the class, not the class's __dict__
2021-06-17 12:34:23eric.smithcreate