-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Conversation
Deploying with Cloudflare Pages
|
please review |
There was a problem hiding this 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.
docs/usage/models.md
Outdated
@@ -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. |
There was a problem hiding this comment.
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."
There was a problem hiding this comment.
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
pydantic/tests/test_create_model.py
Lines 46 to 56 in 793b484
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
There was a problem hiding this comment.
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
There was a problem hiding this 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
@@ -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. |
There was a problem hiding this comment.
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
2de125b
to
0b02b49
Compare
Thanks @tpdorsey for your suggestion |
Closes #4178
Selected Reviewer: @tpdorsey