[issue45897] Frozen dataclasses with slots raise TypeError

2021-12-02 Thread Alex Waygood


Change by Alex Waygood :


--
keywords: +patch
pull_requests: +28119
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/29895

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45897] Frozen dataclasses with slots raise TypeError

2021-12-02 Thread Alex Waygood


Alex Waygood  added the comment:

You get the same error if you subclass a frozen dataclass, then try to set an 
attribute that is not one of the superclass's __slots__:

```
>>> @dataclass(slots=True, frozen=True)
... class Point:
... x: int
... y: int
... 
... 
>>> class Subclass(Point): pass
... 
>>> s = Subclass(1, 2)
>>> s.z = 5
Traceback (most recent call last):
  File "", line 1, in 
s.z = 5
  File "", line 7, in __setattr__
TypeError: super(type, obj): obj must be an instance or subtype of type
```

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45897] Frozen dataclasses with slots raise TypeError

2021-11-25 Thread Eric V. Smith


Eric V. Smith  added the comment:

I think the error should be AttributeError, which is what you'd get if the 
class weren't frozen.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45897] Frozen dataclasses with slots raise TypeError

2021-11-24 Thread Eric V. Smith


Change by Eric V. Smith :


--
versions: +Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45897] Frozen dataclasses with slots raise TypeError

2021-11-24 Thread Eric V. Smith


Change by Eric V. Smith :


--
assignee:  -> eric.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45897] Frozen dataclasses with slots raise TypeError

2021-11-24 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +eric.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45897] Frozen dataclasses with slots raise TypeError

2021-11-24 Thread Alex Waygood


Alex Waygood  added the comment:

This looks to be due to the fact that `slots=True` leads to the creation of an 
entirely new class (see line 1102), meaning that in the `super(cls, self)` 
calls in lines 611 and 618 (in the `_frozen_get_del_attr` function, responsible 
for generating `__setattr__` and `__delattr__` methods), `self` is no longer an 
instance of `cls`.

I believe this can be fixed by tweaking `_frozen_get_del_attr` so that `cls` in 
the generated `__setattr__` and `__delattr__` methods is dynamically computed 
(`cls = type(self)`), rather than read from a closure, as is currently the case.

--
components: +Library (Lib)
nosy: +AlexWaygood

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45897] Frozen dataclasses with slots raise TypeError

2021-11-24 Thread Trey Hunner


New submission from Trey Hunner :

When making a dataclass with slots=True and frozen=True, assigning to an 
invalid attribute raises a TypeError rather than a FrozenInstanceError:

>>> from dataclasses import dataclass
>>> @dataclass(frozen=True, slots=True)
... class Vector:
... x: float
... y: float
... z: float
...
>>> v = Vector(1, 2, 3)
>>> v.a = 4
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 5, in __setattr__
TypeError: super(type, obj): obj must be an instance or subtype of type

--
messages: 406973
nosy: trey
priority: normal
severity: normal
status: open
title: Frozen dataclasses with slots raise TypeError
type: behavior
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com