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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default inclusion in FieldInfo.__repr_args__ #8801

Merged
merged 2 commits into from Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion pydantic/fields.py
Expand Up @@ -574,7 +574,9 @@ def __repr_args__(self) -> ReprArgs:
continue
if s == 'serialization_alias' and self.serialization_alias == self.alias:
continue
if s == 'default_factory' and self.default_factory is not None:
if s == 'default' and self.default is not PydanticUndefined:
yield 'default', self.default
elif s == 'default_factory' and self.default_factory is not None:
yield 'default_factory', _repr.PlainRepr(_repr.display_as_type(self.default_factory))
else:
value = getattr(self, s)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_fields.py
Expand Up @@ -107,3 +107,16 @@ class Model(BaseModel):

assert repr(Model.model_fields['non_frozen_field']) == 'FieldInfo(annotation=int, required=True)'
assert repr(Model.model_fields['frozen_field']) == 'FieldInfo(annotation=int, required=True, frozen=True)'


def test_model_field_default_info():
"""Test that __repr_args__ of FieldInfo includes the default value when it's set to None."""

class Model(BaseModel):
a: int | None = Field(default=None)
b: int | None = None

assert str(Model.model_fields) == (
"{'a': FieldInfo(annotation=Union[int, NoneType], required=False, default=None), "
"'b': FieldInfo(annotation=Union[int, NoneType], required=False, default=None)}"
)
6 changes: 4 additions & 2 deletions tests/test_forward_ref.py
Expand Up @@ -64,7 +64,9 @@ class Bar(BaseModel):
assert b.model_dump() == {'b': {'a': {'b': {'a': None}}}}

# model_fields is complete on Foo
assert repr(module.Foo.model_fields['a']) == ('FieldInfo(annotation=Union[Bar, NoneType], required=False)')
assert repr(module.Foo.model_fields['a']) == (
'FieldInfo(annotation=Union[Bar, NoneType], required=False, default=None)'
)

assert module.Foo.__pydantic_complete__ is False
# Foo gets auto-rebuilt during the first attempt at validation
Expand Down Expand Up @@ -159,7 +161,7 @@ class Foo(BaseModel):
]

assert repr(module.Foo.model_fields['a']) == 'FieldInfo(annotation=int, required=False, default=123)'
assert repr(module.Foo.model_fields['b']) == 'FieldInfo(annotation=Foo, required=False)'
assert repr(module.Foo.model_fields['b']) == 'FieldInfo(annotation=Foo, required=False, default=None)'
if sys.version_info < (3, 10):
return
assert repr(module.Foo.model_fields['c']) == ('FieldInfo(annotation=List[Foo], required=False, ' 'default=[])')
Expand Down