8000 missing attribute dataclass copy with slots=True & validate_assignment=True · Issue #11768 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

missing attribute dataclass copy with slots=True & validate_assignment=True #11768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
dpinol opened this issue Apr 17, 2025 · 3 comments · Fixed by #11769
Closed
1 task done

missing attribute dataclass copy with slots=True & validate_assignment=True #11768

dpinol opened this issue Apr 17, 2025 · 3 comments · Fixed by #11769
Assignees
Labels
bug V2 Bug related to Pydantic V2

Comments

@dpinol
Copy link
dpinol commented Apr 17, 2025

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When copying a pydantic v2 dataclass with both "validate_assignment": True and slots=True, I get object has no attribute for an existing field.

Example Code

from copy import copy

from pydantic.dataclasses import dataclass


@dataclass(slots=True, config={"validate_assignment": True})
class Sut:
    b1: int

sut = Sut(0)
print(sut.b1)
copy(sut)

Python, Pydantic & OS Version

pydantic version: 2.11.3
        pydantic-core version: 2.33.1
          pydantic-core build: profile=release pgo=false
                 install path: /home/dani/.virtualenvs/sc-api-DipHMMiG/lib/python3.12/site-packages/pydantic
               python version: 3.12.9 (main, Apr  4 2025, 08:52:04) [GCC 14.2.0]
                     platform: Linux-6.11.0-8-generic-x86_64-with-glibc2.40
             related packages: typing_extensions-4.13.2 mypy-1.15.0
                       commit: unknown
@dpinol dpinol added bug V2 Bug related to Pydantic V2 pending Is unconfirmed labels Apr 17, 2025
@Viicos
Copy link
Member
Viicos commented Apr 17, 2025

I'm not sure how to fix this yet. We could define a custom __copy__() method on Pydantic dataclasses, but the issue lies in the fact that pickling (see repro below) uses setattr(), and pydantic-core excepts field values to be present when validating on assignment. Probably we can override __setstate__() but I'll have to check with the stdlib implementation.

Repro with pickling:

import pickle

from pydantic.dataclasses import dataclass


@dataclass(slots=True, config={"validate_assignment": True})
class Sut:
    b1: int

pickle.loads(pickle.dumps(Sut(0)))
# AttributeError

@dpinol
Copy link
Author
dpinol commented May 27, 2025

Thanks for the fix!
Any timeframe for a release?

@Viicos
Copy link
Member
Viicos commented May 28, 2025

2.12 is planned on June 30th.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0