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 placeholder deep copy #611

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/betterproto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,22 @@ class Casing(builtin_enum.Enum):
SNAKE = snake_case #: A snake_case sterilization function.


PLACEHOLDER: Any = object()
class Placeholder:
__slots__ = ()

def __repr__(self) -> str:
return "<PLACEHOLDER>"

def __copy__(self) -> Self:
return self

def __deepcopy__(self, _) -> Self:
return self


# We can't simply use object() here because pydantic automatically performs deep-copy of mutable default values
# See #606
PLACEHOLDER: Placeholder = Placeholder()
Copy link
Collaborator

@Gobot1234 Gobot1234 Sep 17, 2024

Choose a reason for hiding this comment

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

Suggested change
PLACEHOLDER: Placeholder = Placeholder()
PLACEHOLDER: Any = Placeholder()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure to understand, is there any reason for this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's so it can be used anywhere and the type checker won't complain

Copy link
Contributor Author

@AdrienVannson AdrienVannson Sep 17, 2024

Choose a reason for hiding this comment

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

I think the type checker complains anyway: with mypy, your changes turns:
src/betterproto/__init__.py:224: error: Argument "default" to "field" has incompatible type "Placeholder | None"; expected "Field[Any]" [arg-type]
into
src/betterproto/__init__.py:224: error: Argument "default" to "field" has incompatible type "Any | None"; expected "Field[Any]" [arg-type]

It is coherent since if x has type T and y has type Any, then x = y should not be a valid operation. Wouldn't it be better to add # type: ignore on the lines where it is needed? I think the line 224 is the only one with this kind of error.

Copy link
Collaborator

@Gobot1234 Gobot1234 Sep 17, 2024

Choose a reason for hiding this comment

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

It should still be an Any, but the type ignore would be appreciated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated the PR



@dataclasses.dataclass(frozen=True)
Expand Down
Loading