[New-bugs-announce] [issue39462] DataClass typo-unsafe attribute creation & unexpected behaviour (dataclasses)

Marcel report at bugs.python.org
Mon Jan 27 06:09:27 EST 2020

New submission from Marcel <mpv.visser at gmail.com>:

After instantiation of a variable of a DataClass, it is possible to assign new attributes (that were not defined in defining the DataClass):

data.new_attribute = 3.0  # does NOT raise Error!

This gives unexpected behaviour: if you print the variable, then 'new_attribute' is not printed (since it is not in the definition of the DataClass).

Assigning to an attribute is therefore not typo-safe (which users may expect from a DataClass).

I would expect the behaviour of the DataClass be consistent and typo-safe.

Attached is a file that demonstrates the bug (behaviour) and provides a 'SafeDataClass' by overriding the __setattr__ method.

My suggestion would be to the adjust the library __setattr__ for the DataClass such that is will be typo-safe.

components: Library (Lib)
files: bug_demo_dataclass_typo_unsafe.py
messages: 360752
nosy: marcelpvisser
priority: normal
severity: normal
status: open
title: DataClass typo-unsafe attribute creation & unexpected behaviour (dataclasses)
type: behavior
versions: Python 3.7, Python 3.8, Python 3.9
Added file: https://bugs.python.org/file48865/bug_demo_dataclass_typo_unsafe.py

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list