-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add back support for json_encoders
#6811
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
Conversation
Deploying with
|
| Latest commit: |
2e756f4
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://9c93e4ad.pydantic-docs2.pages.dev |
| Branch Preview URL: | https://json-encoders.pydantic-docs2.pages.dev |
114a4c6 to
a6c4e3b
Compare
|
please review |
| @dataclass | ||
| # MYPY: error: Expression type contains "Any" (has type overloaded function) [misc] | ||
| class Foo: | ||
| foo: int | ||
|
|
||
|
|
||
| @dataclass(config=ConfigDict(title='Bar Title')) | ||
| # MYPY: error: Expression type contains "Any" (has type "Type[ConfigDict]") [misc] | ||
| # MYPY: error: Expression type contains "Any" (has type "ConfigDict") [misc] | ||
| class Bar: | ||
| bar: str |
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.
@hramezani did you intentionally add this error?
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.
No, It was generated by the script. Actually, we can say by your change.
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.
The point of this test was specifically to make sure this error didn't get created. However, I've discovered that using a type alias for the callable in json_encoders seems to make this go away. (At least with mypy 1.1.1...), which I've just pushed in a commit. So hopefully that resolves the issue.
| if 'ref' in schema: | ||
| return core_schema.definition_reference_schema(schema_ref=schema['ref'], serialization=serialization) # type: ignore |
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 seems problematic to me — it's not currently hit by any of the tests (as determined by putting if 'ref' in schema: assert False, and it seems like in principle it could result in a definition getting removed from the schema. Does that make sense? I'm trying to look further into it now, maybe it happens to be the case that this can't cause issues...
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 haven't been able to reproduce it (though investigating did lead me to the other comment I made, about self-referencing encoders), but yeah this does seem to make the assumption that the original definition will be present elsewhere, I'm not sure whether that is a safe assumption or not, maybe you know based on the way definitions get stored in GenerateSchema or whatever.
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.
You're right, it's not needed. We're pretty good about always retuning a definitions-reference schema and putting the schema into definitions so we never really end up passing a round a schema with a 'ref'.
|
Maybe enough of an edge case to ignore, but it might be nice to improve the way you can use a self-referential JSON encoder. In particular, the following works: from pydantic import BaseModel
class Model1(BaseModel):
model_config = {'json_encoders': {}}
x: 'Model1 | None'
Model1.model_config['json_encoders'][Model1] = lambda x: x.x if x.x is not None else 1
del Model1.__pydantic_core_schema__
Model1.model_rebuild(force=True)
print(Model1(x=Model1(x=None)).model_dump_json())
#> {"x":1}But obviously this is a lot worse than doing something like I guess maybe we can postpone this as a feature request for the future. |
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.
Other than the comments I've made, this all looks good to me, so I'll approve for now so you can merge after reviewing those comments and making changes if appropriate.
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
Yeah I don't think this worked in v1 so let's table it. |
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.
otherwise LGTM. Do we need to add, alter or remove something in the migration guide?
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
| f'`json_encoders` is deprecated. See https://docs.pydantic.dev/{VERSION}/usage/serialization/#custom-serializers for alternatives', | ||
| PydanticDeprecatedSince20, |
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 is incorrect, it should only be the major and minor versions.
Maybe the solution is to have a proper warnings page like we have for user and validation errors. I can work on this today.
Fixing this should not block release.
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.
It actually redirects, e.g. https://docs.pydantic.dev/2.0.2/usage/serialization/#custom-serializers works
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.
only for specific versions see #6527.
I'll provide a general fix.
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.
Ah I see. Okay then we can just strip the patch version.
json_encoders
|
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.
Closes #6726, closes #6375,
Selected Reviewer: @dmontagu