python / python/cpython

Misleading "Not an instance or subtype" error when assigning to property of frozen, slotted dataclass

Aberta
#143,969 6 comentários 2 reações 1 responsável Ver no GitHub

@sobolevn já está trabalhando nisso.

Desde 19/5/2026.

stdlib topic-dataclasses type-bug
Linguagem predominante
Python
Estrelas
77.2k
Forks
35.9k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

Bug report

Bug description:

When a dataclass is frozen, attempting to assign to a property (with a setter or without) fails as expected:

from dataclasses import dataclass

@dataclass(frozen=True)
class Example:
	a: int

	@property
	def prop(self):
		return None

example = Example(10)
example.prop = 0
  File "test_dataclass.py", line 12, in <module>
    example.prop = 0
    ^^^^^^^^^^^^
  File "<string>", line 15, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field 'prop'

And when the class is slotted, if the property has no setter, it also fails the way one would expect:

from dataclasses import dataclass

@dataclass(slots=True)
class Example:
	a: int

	@property
	def prop(self):
		return None

example = Example(10)
example.prop = 0
Traceback (most recent call last):
  File "test_dataclass.py", line 12, in <module>
    example.prop = 0
    ^^^^^^^^^^^^
AttributeError: property 'prop' of 'Example' object has no setter

(In this case, of course, adding a setter makes it work fine.)

What's unexpected (and confusing) is when the class is both frozen and slotted:

from dataclasses import dataclass

@dataclass(frozen=True, slots=True)
class Example:
	a: int

	@property
	def prop(self):
		return None

example = Example(10)
example.prop = 0
Traceback (most recent call last):
  File "test_dataclass.py", line 12, in <module>
    example.prop = 0
    ^^^^^^^^^^^^
  File "<string>", line 16, in __setattr__
TypeError: super(type, obj): obj (instance of Example) is not an instance or subtype of type (Example).

One would, of course, expect an instance of Example to in fact be an instance of Example, but maybe this is somehow a collision between the initial vs. dataclass-ified class definitions?

CPython versions tested on:

3.13

Operating systems tested on:

macOS

Linked PRs
  • gh-143971

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.