New submission from Eric V. Smith <e...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue44443>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to