Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Remove dataclass field with init_var from __pydantic__fields__ #6141

Merged
merged 1 commit into from Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions pydantic/_internal/_fields.py
Expand Up @@ -200,6 +200,9 @@ def collect_dataclass_fields(
continue

if isinstance(dataclass_field.default, FieldInfo):
if dataclass_field.default.init_var:
# TODO: same note as above
continue
field_info = FieldInfo.from_annotated_attribute(ann_type, dataclass_field.default)
else:
field_info = FieldInfo.from_annotated_attribute(ann_type, dataclass_field)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_fields.py
Expand Up @@ -31,6 +31,20 @@ class Model:
assert e.value.args == ('InitVar is not supported in Python 3.7 as type information is lost',)


def test_init_var_field():
@pydantic.dataclasses.dataclass
class Foo:
bar: str
baz: str = Field(init_var=True)

class Model(BaseModel):
foo: Foo

model = Model(foo=Foo('bar', baz='baz'))
assert 'bar' in model.foo.__pydantic_fields__
assert 'baz' not in model.foo.__pydantic_fields__


def test_root_model_arbitrary_field_name_error():
with pytest.raises(
NameError, match="Unexpected field with name 'a_field'; only 'root' is allowed as a field of a `RootModel`"
Expand Down