-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 auto generate conversion table to doc #5324
Conversation
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 looking good.
I guess we need to add more types before we see how this really works. In particular, do we need more information to describe generic types?
docs/blog/pydantic-v2.md
Outdated
| `is_instance` | - | both | JSON | never valid | | ||
| `callable` | `Any` | both | python | `callable()` check returns `True` | | ||
| `callable` | - | both | JSON | never valid | | ||
{{ conversion_table }} |
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.
Probably best to leave the one in the blog post unchanged.
Instead a new page to usage with the conversion table.
We'll need to add a big heading to this blog post that it's superseded.
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.
docs/plugins/conversion_table.py
Outdated
class Row: | ||
field_type: type[Any] | ||
input_type: type[Any] | ||
mode: Literal['lax', 'strict', 'both'] |
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.
mode: Literal['lax', 'strict', 'both'] | |
mode: Literal['lax', 'strict'] |
"strict" should always imply "lax" as well, if there are exceptions, we can add specific comments.
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 are right. both
removed
docs/plugins/conversion_table.py
Outdated
'lax', | ||
'python', | ||
condition='assumes UTF-8, error on unicode decoding error', | ||
valid_examples=[b's'], |
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.
valid_examples=[b's'], | |
valid_examples=[b'this is bytes'], |
probably good to change the others to something more readable too.
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.
All changed
docs/plugins/conversion_table.py
Outdated
field_type: type[Any] | ||
input_type: type[Any] | ||
mode: Literal['lax', 'strict', 'both'] | ||
input_format: Literal['python', 'JSON', 'python, JSON'] |
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.
input_format: Literal['python', 'JSON', 'python, JSON'] | |
input_format: Literal['python', 'JSON', 'python & JSON'] |
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.
Done
docs/plugins/conversion_table.py
Outdated
'both', | ||
'python, JSON', | ||
condition='max abs value 2^64 - i64 is used internally', | ||
core_schema=core_schema.IntSchema, |
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.
can you add an invalid example.
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've added an invalid example. Also, I've separated the strict
and lax
mode for int
type in table because it raises ValidationError
for number > limit in strict
mode but it will limit the input to the max in lax
mode.
I've added descriptions for these in the condition field but I am not sure about the wording.
I've added tests for both above-mentioned cases.
docs/plugins/conversion_table.py
Outdated
core_schema: type[CoreSchema] = None | ||
|
||
|
||
table_infos: list[Row] = [ |
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.
table_infos: list[Row] = [ | |
table: list[Row] = [ |
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.
Done!
docs/plugins/main.py
Outdated
'Defined in', | ||
] | ||
|
||
|
||
def md2html(s: str) -> 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.
we might want other things, can we use the proper markdown -> html library from mkdocs?
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.
Not very familiar with mkdocs and it's the related library.
@tpdorsey Do you have an idea?
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.
if this isn't easy, keep it as-is, at least for now.
please update. |
a89e3fd
to
f6e9ab6
Compare
core_schema: type[CoreSchema] = None | ||
|
||
|
||
table: list[Row] = [ |
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.
@samuelcolvin
The row list will be distributed as JSON lines or another format? in the wheel.
Does the list affect the behavior of the strict
argument? Or does the list explain the behavior pydantic-core
?
Also, Does the user custom type use the Row
class?
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.
if including the JSON in the wheel/sdist would help you, we definitely can.
Yes, the strict
argument is relevant here, that logic is mostly implemented in pydantic-core
, some is implemented in types in pydantic.
There's currently no way for user custom types to provide Row
information.
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.
if including the JSON in the wheel/sdist would help you, we definitely can.
Thank you. I will pick up the JSON from the package.
Yes, the strictargument is relevant here, that logic is mostly implemented in pydantic-core, some is implemented in types in pydantic.
I see.
There's currently no way for user custom types to provide Row information.
How can I get the expected type of user custom types?
I don't need the answer now. But, we should be clear sometime.
5bc1742
to
74fa64c
Compare
docs/plugins/conversion_table.py
Outdated
Row( | ||
date, | ||
date, | ||
'stric', |
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.
Strict
docs/plugins/conversion_table.py
Outdated
Row( | ||
None, | ||
None, | ||
'stric', |
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.
Strict
docs/plugins/conversion_table.py
Outdated
datetime, | ||
'lax', | ||
'python', | ||
condition='must be exact date, eg. no H, M, S, f', |
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.
condition='must be exact date, eg. no H, M, S, f', | |
condition='must be exact date, eg. no `H`, `M`, `S`, `f`', |
docs/plugins/conversion_table.py
Outdated
'lax', | ||
'python & JSON', | ||
condition=( | ||
'interpreted as seconds or ms from epoch, ' |
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.
'interpreted as seconds or ms from epoch, ' | |
'Interpreted as seconds or ms from epoch. ' |
docs/plugins/conversion_table.py
Outdated
'python & JSON', | ||
condition=( | ||
'interpreted as seconds or ms from epoch, ' | ||
'see [speedate](https://docs.rs/speedate/latest/speedate/), must be exact date' |
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.
'see [speedate](https://docs.rs/speedate/latest/speedate/), must be exact date' | |
'See [speedate](https://docs.rs/speedate/latest/speedate/). Must be exact date.' |
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.
Suggest applying same change to equivalent conditions.
docs/plugins/conversion_table.py
Outdated
str, | ||
'strict', | ||
'JSON', | ||
condition='format YYYY-MM-DDTHH:MM:SS.f etc. see [speedate](https://docs.rs/speedate/latest/speedate/)', |
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.
condition='format YYYY-MM-DDTHH:MM:SS.f etc. see [speedate](https://docs.rs/speedate/latest/speedate/)', | |
condition='Format: `YYYY-MM-DDTHH:MM:SS.f`. See [speedate](https://docs.rs/speedate/latest/speedate/).', |
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.
Again, apply same edit to similar conditions.
docs/plugins/conversion_table.py
Outdated
Any, | ||
'strict', | ||
'python', | ||
condition='`isinstance()` check returns True', |
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.
condition='`isinstance()` check returns True', | |
condition='`isinstance()` check returns `True`.', |
0f99980
to
1df088e
Compare
Thanks @tpdorsey, All addressed. please review |
Change Summary
Add auto-generate conversion table
This is the initial work to generate the conversion table automatically.
I've replaced the conversion table in v2 blog with
{{ conversion_table }}
. The conversion table rows are not complete yet and I will update the PR later.Meanwhile, I am going to add missed tests as well.
I've built the docs locally and the new
build_conversion_table
works fine and generates the conversion table as expected.Related issue number
Fixes #4680
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)
Selected Reviewer: @adriangb