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

Add note about dynamically created model pickling #5781

Merged
merged 1 commit into from
May 19, 2023

Conversation

hramezani
Copy link
Member

@hramezani hramezani commented May 17, 2023

Closes #4178

Selected Reviewer: @tpdorsey

@cloudflare-pages
Copy link

cloudflare-pages bot commented May 17, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0b02b49
Status: ✅  Deploy successful!
Preview URL: https://54840fec.pydantic-docs2.pages.dev
Branch Preview URL: https://create-model-pickle.pydantic-docs2.pages.dev

View logs

@hramezani
Copy link
Member Author

please review

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

LGTM, best to assign to @tpdorsey for docs PRs.

@@ -914,6 +914,9 @@ except ValidationError as e:
Assertion failed, must be alphanumeric [type=assertion_error, input_value='scolvi%n', input_type=str]
"""
```
!!! note
Pickle will work for dynamically created model only if it was defined globally
with its class name and module where it's defined was specified.
Copy link
Contributor

Choose a reason for hiding this comment

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

I may be missing something about this. Would the following capture the same meaning?

"Pickle will work for a dynamically created model only if the model was defined globally, specifying both its class name and the module in which the class was defined."

Copy link
Member Author

Choose a reason for hiding this comment

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

@tpdorsey I just copied the description from

def test_create_model_pickle(create_module):
"""
Pickle will work for dynamically created model only if it was defined globally with its class name
and module where it's defined was specified
"""
@create_module
def module():
import pickle
from pydantic import create_model

It may help you to understand it better.

I think the dynamic model has to have these two conditions to be pickable:

  • Has to provide __module__
  • Has to be defined globally. So, defining a dynamic model in function doesn't work

Copy link
Contributor

Choose a reason for hiding this comment

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

This works, I'll edit the above to use this language. Thx

Copy link
Contributor

@tpdorsey tpdorsey left a comment

Choose a reason for hiding this comment

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

Minor suggested edit

docs/usage/models.md Outdated Show resolved Hide resolved
docs/usage/models.md Outdated Show resolved Hide resolved
@@ -914,6 +914,9 @@ except ValidationError as e:
Assertion failed, must be alphanumeric [type=assertion_error, input_value='scolvi%n', input_type=str]
"""
```
!!! note
Pickle will work for dynamically created model only if it was defined globally
with its class name and module where it's defined was specified.
Copy link
Contributor

Choose a reason for hiding this comment

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

This works, I'll edit the above to use this language. Thx

@hramezani
Copy link
Member Author

Thanks @tpdorsey for your suggestion

@hramezani hramezani merged commit 9348227 into main May 19, 2023
43 checks passed
@hramezani hramezani deleted the create_model_pickle branch May 19, 2023 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pickling error with create_model
3 participants