Skip to content
This repository was archived by the owner on Apr 11, 2026. It is now read-only.

Fix: dataclass InitVars shouldn't be required on serialization - #1602

Merged
davidhewitt merged 14 commits into
mainfrom
fix-initvar-required
Jan 23, 2025
Merged

davidhewitt merged 14 commits into
mainfrom
fix-initvar-required

Conversation

@sydney-runkle

@sydney-runkle sydney-runkle commented Jan 21, 2025 •

Copy link
Copy Markdown
Contributor

@codspeed

codspeed Bot commented Jan 21, 2025 •

Copy link
Copy Markdown

CodSpeed Performance Report

Merging #1602 will not alter performance

Comparing fix-initvar-required (3e82624) with main (b426cce)

Summary

✅ 157 untouched benchmarks

@sydney-runkle
sydney-runkle marked this pull request as ready for review January 21, 2025 18:53
Comment thread src/serializers/type_serializers/dataclass.rs Outdated

@samuelcolvin samuelcolvin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely need some kind of test for this change of behaviour!

We can manually build the core-schema, but we absolutely need a test or tests.

Comment thread src/serializers/type_serializers/dataclass.rs Outdated
Comment thread src/serializers/type_serializers/dataclass.rs Outdated

@samuelcolvin samuelcolvin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise I think LGTM.

)

s = SchemaSerializer(schema)
assert s.to_python(Foo(x=1), warnings='error') == {'x': 1}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please can you add a test with just the Foo schema, and a test for Bar here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and can you add a test for Foo(x=1, init_var=2)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please can you add a test with just the Foo schema, and a test for Bar here.

Not quite sure what you mean here - I wasn't able to repro the issue without the union.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still it's still worth adding tests for the simplest cases.

Comment thread src/serializers/type_serializers/dataclass.rs

@davidhewitt davidhewitt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@davidhewitt
davidhewitt merged commit 261c7ab into main Jan 23, 2025
@davidhewitt
davidhewitt deleted the fix-initvar-required branch January 23, 2025 19:06
davidhewitt pushed a commit to pydantic/pydantic that referenced this pull request Oct 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dataclass with InitVar and Union gives warning during serialization

3 participants