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: Deletion of attributes in dataclass is buggy
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: Kamyar Inanloo, brett.cannon, eric.smith
Priority: normal Keywords:

Created on 2018-08-12 11:55 by Kamyar Inanloo, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg323442 - (view) Author: Kamyar Inanloo (Kamyar Inanloo) Date: 2018-08-12 11:55
When using del or delattr on an instance of dataclass, attribute does not get deleted truly! using vars or getattr still returns the deleted attribute just using delattr again raises error!
msg323454 - (view) Author: Kamyar Inanloo (Kamyar Inanloo) Date: 2018-08-12 20:14
Actually it just sets the attribute to its default value, instead of removing it from the instance.
msg323457 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-08-13 03:24
I think this is just normal behavior with classes. But since you haven't shown any code, I can't tell exactly what you're doing and what you're expecting.

When providing bug reports, you need to provide:
- Exactly what code you're executing.
- Exactly what output you're seeing.
- What you expect if it's different from what you're seeing.

Please provide this in the body of a message as text, not (for example) as an attached image file.

Thanks.
msg323493 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2018-08-13 21:33
I agree with Eric that without concrete details I suspect everything is working as expected. E.g.

```python
class Foo:
   attr = -13

ins = Foo()
ins.attr = 42
print(ins.attr)
del ins.attr
print(ins.attr)
```
msg323509 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-08-14 12:05
I assume this is the behavior you're seeing:

>>> @dataclass
... class C:
...   i: int = 0
...
>>> c = C(10)
>>> c
C(i=10)
>>> del c.i
>>> c
C(i=0)
>>> del c.i
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: i
>>>

If so, that's the expected behavior. You're deleting an instance attribute, so that the class attribute with the same name is seen.

This also happens without dataclasses:

>>> class C:
...   i = 0
...   def __init__(self, i):
...     self.i = i
...
>>> c = C(10)
>>> c.i
10
>>> del c.i
>>> c.i
0
>>> del c.i
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: i
>>>

If you're seeing something different, please re-open this issue.
History
Date User Action Args
2022-04-11 14:59:04adminsetgithub: 78568
2018-08-14 12:05:56eric.smithsetstatus: open -> closed
resolution: not a bug
messages: + msg323509

stage: resolved
2018-08-13 21:33:17brett.cannonsetnosy: + brett.cannon

messages: + msg323493
title: Deletion of attributes in dataclass is buggy! -> Deletion of attributes in dataclass is buggy
2018-08-13 03:24:43eric.smithsetmessages: + msg323457
2018-08-12 20:14:01Kamyar Inanloosetmessages: + msg323454
2018-08-12 19:41:27rhettingersetassignee: eric.smith
2018-08-12 14:25:07berker.peksagsetnosy: + eric.smith, - larry
components: + Library (Lib), - Argument Clinic
2018-08-12 11:55:20Kamyar Inanloocreate