[New-bugs-announce] [issue44443] dataclass looks up default on the class, not the classes __dict__

Eric V. Smith report at bugs.python.org
Thu Jun 17 08:34:23 EDT 2021


New submission from Eric V. Smith <eric at trueblade.com>:

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__).

----------
assignee: eric.smith
components: Library (Lib)
messages: 396000
nosy: eric.smith
priority: normal
severity: normal
status: open
title: dataclass looks up default on the class, not the classes __dict__
versions: Python 3.10, Python 3.11, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44443>
_______________________________________


More information about the New-bugs-announce mailing list