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
WIP Fix: init=False in dataclass fields ignored #8545
Conversation
Why - Previously, setting `init=False` in std field and pydantic's Field had no effect, the field still appears as a parameter in the generated `__init__` What - Added `init` attribute to `FieldInfo` and `Field` - Corrected docstring for `init_var` - Added check for `init` in `make_pydantic_fields_compatible` - Added test to confirm `init=False` fields don't show up in the signature Notes - Unfortunately, can still use the `init=False` as parameters in init... I think this is because of pydantic allowing the "extra" params that aren't explicitly defined - Even setting `ConfigDict(extra='forbid')` doesn't help here. From some debugging, it doesn't look like the model schema understands the `init=False`. So more work needed to actually throw an error.
CodSpeed Performance ReportMerging #8545 will not alter performanceComparing Summary
|
@@ -101,7 +102,9 @@ class FieldInfo(_repr.Representation): | |||
frozen: Whether the field is frozen. | |||
validate_default: Whether to validate the default value of the field. | |||
repr: Whether to include the field in representation of the model. | |||
init_var: Whether the field should be included in the constructor of the dataclass. | |||
init: If true (the default), this field is included as a parameter to the generated `__init__` of the dataclass | |||
init_var: Whether the field is an "init-only" field: used as a parameter to the generated `__init__` method, but |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From https://docs.pydantic.dev/latest/concepts/fields/#dataclass-constraints, and looking at a few of the test cases, i think the previous docstring for init_var
needed to be tweaked.
@sydney-runkle I tried adding DetailsThe signature for Example:
Output:
With
|
Thanks for your initial work on this, and your detailed report / questions. @dmontagu has been kind enough to look into this a bit and has opened these two PRS:
Adding support for The second PR still needs some tests and some additions to documentation. You've done some great work with adding tests already, as well as adding some much needed docs changes. I'll port the changes you've added over to the second PR. I'm going to close this in favor of #8552. Let me know if you have any questions - we appreciate your contributions and hope that you'll continue to help us out with your awesome work 🚀. |
Thanks @sydney-runkle ! Definitely planning on continuing to contribute |
Change Summary
init
attribute toFieldInfo
andField
init
inmake_pydantic_fields_compatible
init=False
fields don't show up in the signatureinit_var
Related issue number
WIP Partial fix for #8454
init=False
in std field and pydantic's Field had no effect, the field still appears as a parameter in the generated__init__
Checklist
Notes
init
parameters yet.init=False
fields as parameters in the dataclass constructor... I think this is because of pydantic allowing the "extra" params that aren't explicitly definedConfigDict(extra='forbid')
doesn't lead to throwing aValidationError
. From some debugging, it doesn't look like the model schema understands theinit=False
. So more work needed to actually throw an error.For example: