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: Support pydantic.Field(repr=False) in dataclasses #8511

Merged
merged 2 commits into from Jan 12, 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
46 changes: 27 additions & 19 deletions pydantic/dataclasses.py
Expand Up @@ -143,28 +143,36 @@ def dataclass(

if sys.version_info >= (3, 10):
kwargs = dict(kw_only=kw_only, slots=slots)

def make_pydantic_fields_compatible(cls: type[Any]) -> None:
"""Make sure that stdlib `dataclasses` understands `Field` kwargs like `kw_only`
To do that, we simply change
`x: int = pydantic.Field(..., kw_only=True)`
into
`x: int = dataclasses.field(default=pydantic.Field(..., kw_only=True), kw_only=True)`
"""
for field_name in cls.__annotations__:
try:
field_value = getattr(cls, field_name)
except AttributeError:
# no default value has been set for this field
continue
if isinstance(field_value, FieldInfo) and field_value.kw_only:
setattr(cls, field_name, dataclasses.field(default=field_value, kw_only=True))

else:
kwargs = {}

def make_pydantic_fields_compatible(_) -> None:
return None
def make_pydantic_fields_compatible(cls: type[Any]) -> None:
"""Make sure that stdlib `dataclasses` understands `Field` kwargs like `kw_only`
To do that, we simply change
`x: int = pydantic.Field(..., kw_only=True)`
into
`x: int = dataclasses.field(default=pydantic.Field(..., kw_only=True), kw_only=True)`
"""
# In Python < 3.9, `__annotations__` might not be present if there are no fields.
# we therefore need to use `getattr` to avoid an `AttributeError`.
for field_name in getattr(cls, '__annotations__', []):
field_value = getattr(cls, field_name, None)
# Process only if this is an instance of `FieldInfo`.
if not isinstance(field_value, FieldInfo):
continue

# Initialize arguments for the standard `dataclasses.field`.
field_args: dict = {'default': field_value}

# Handle `kw_only` for Python 3.10+
if sys.version_info >= (3, 10) and field_value.kw_only:
field_args['kw_only'] = True

# Set `repr` attribute if it's explicitly specified to be not `True`.
if field_value.repr is not True:
Copy link
Member

Choose a reason for hiding this comment

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

Why not is False ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@PrettyWood I saw this as checking that repr is not the default value, happy to open a small pr to switch it to is False if needed!

field_args['repr'] = field_value.repr
Comment on lines +171 to +173
Copy link
Member

Choose a reason for hiding this comment

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

This looks good overall. I wonder if we should go ahead and handle the init case here as well, see #8454


setattr(cls, field_name, dataclasses.field(**field_args))

def create_dataclass(cls: type[Any]) -> type[PydanticDataclass]:
"""Create a Pydantic dataclass from a regular dataclass.
Expand Down
45 changes: 37 additions & 8 deletions tests/test_dataclasses.py
Expand Up @@ -522,16 +522,27 @@ class User:
id: int
other: Dict[str, str] = dataclasses.field(default_factory=lambda: {'John': 'Joey'})

user = User(id=123)
assert user.id == 123
# assert user.other == {'John': 'Joey'}
fields = user.__pydantic_fields__
@pydantic.dataclasses.dataclass
class User2:
id: int
other: Dict[str, str] = pydantic.dataclasses.Field(default_factory=lambda: {'John': 'Joey'})

assert fields['id'].is_required() is True
assert repr(fields['id'].default) == 'PydanticUndefined'
user1 = User(id=123)
user2 = User2(id=123)

assert fields['other'].is_required() is False
assert fields['other'].default_factory() == {'John': 'Joey'}
def validate_user(user):
assert user.id == 123
assert user.other == {'John': 'Joey'}
fields = user.__pydantic_fields__

assert fields['id'].is_required() is True
assert repr(fields['id'].default) == 'PydanticUndefined'

assert fields['other'].is_required() is False
assert fields['other'].default_factory() == {'John': 'Joey'}

validate_user(user1)
validate_user(user2)
Copy link
Member

Choose a reason for hiding this comment

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

Similar advice - instead of doing this here, just parametrize the test with the field constructor, then you can just run through the validation checks once 👍



def test_default_factory_singleton_field():
Expand Down Expand Up @@ -1647,6 +1658,24 @@ class B(A):
assert B(1, y=2, z=3) == B(x=1, y=2, z=3)


def test_repr_false():
Copy link
Member

Choose a reason for hiding this comment

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

You could parametrize this (using pytest.mark.parametrize) with a field_constructor variable that takes a value of either pydantic.dataclasses.Field or dataclasses.field for simplicity 👍. Then you only need one class definition, and one set of asserts.

@pydantic.dataclasses.dataclass
class A:
visible_field: str
hidden_field: str = pydantic.dataclasses.Field(repr=False)

@pydantic.dataclasses.dataclass
class B:
visible_field: str
hidden_field: str = dataclasses.field(repr=False)

assert "hidden_field='excluded'" not in repr(B(visible_field='included', hidden_field='excluded'))

instance = A(visible_field='this_should_be_included', hidden_field='this_should_not_be_included')
assert "visible_field='this_should_be_included'" in repr(instance)
assert "hidden_field='this_should_not_be_included'" not in repr(instance)


def dataclass_decorators(include_identity: bool = False, exclude_combined: bool = False):
decorators = [pydantic.dataclasses.dataclass, dataclasses.dataclass]
ids = ['pydantic', 'stdlib']
Expand Down