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 pickle/copy support for the missing singleton #2029

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mattclay
Copy link

@mattclay mattclay commented Oct 1, 2024

Fix pickle/copy support for the missing singleton:

  • Fix pickle support for the missing singleton.
  • Add unit test for pickling the missing singleton.
  • Add unit test for copying the missing singleton.

Fixes #2027

mattclay and others added 2 commits October 1, 2024 15:25
* Fix pickle support for the `missing` singleton.
* Add unit test for pickling the `missing` singleton.
* Add unit test for copying the `missing` singleton.
@mattclay
Copy link
Author

mattclay commented Oct 1, 2024

Returning "missing" from __reduce__ is a documented solution for singleton types:

If a string is returned, the string should be interpreted as the name of a global variable. It should be the object’s local name relative to its module; the pickle module searches the module namespace to determine the object’s module. This behaviour is typically useful for singletons.

"MissingType",
(),
{"__repr__": lambda x: "missing", "__reduce__": lambda x: "missing"},
)()
Copy link
Author

@mattclay mattclay Oct 1, 2024

Choose a reason for hiding this comment

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

I used this form to minimize changes, but it could be replaced with an actual class definition if desired.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that makes more sense. I don't see a reason a "singleton" was done this way, it's enough to call the class _MissingType to indicate it's not public.

Choose a reason for hiding this comment

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

Always the challenge when interacting with unfamiliar projects/maintainers around finding the balance of "minimize changes" vs "if you were building this today, it would look very different" 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot pickle jinja2.utils.missing
3 participants