Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Minor update to
Navigation
docs #190Minor update to
Navigation
docs #190Changes from 16 commits
1af0dc7
65d5581
4ad350a
d2f97e6
fb78e85
afe2968
86227d0
f62f041
6b1683f
85619df
52d0d47
66b113c
32a1624
36138b8
9622e86
712067b
8179bba
b716d6f
8c7e92b
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
@antonymilne - here I wasn't sure whether the
Optional
was removed intentionally?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.
To be confirmed...
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.
After reading through this section of the migration guide for pydantic V2 and this forum post I don't have a strong preference, both should work fine.
To sum up the relevant parts:
Runtime implications
In practice this means that for the following model all fields behave identically in the sense that neither is required during initialization because all of them will receive None as their value, when no other value is provided, and the type of each of them is set to be Optional[str].
It seems that the g case, the one we are talking about here is under the hood converted to Optional[str] = None:
Migration to V2
What will change in pydantic V2 is the way how Optional[X] are handled if no defaults are provided as far as I understand e.g.
Optional[str]
andOptional[str] = None
were both considered as non-required and a default of None was automatically set in V1.This is not the case anymore with V2.
Optional[str]
without a default will be considered as a required field, whileOptional[str] = None
is not required, can be None, and is None by default. That being said you are right thatstr = None
will result in the same, so that field being considered as non-required, can be None and is None by default. So functionally both are the same.I have a slight preference for
Optional[XXX] = None
if we want to be more explicit, but as you already mentioned this sometimes leads to these mypy issues where we have to solve it via cast, making it a bit difficult to understand for new developers. So it’s questionable whether this improves it. So either solutions are actually fine for me :)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.
Interesting conversation.
Optional[XXX] = None
seems to be a little more what python developer are used to I guess, but if it causes other problems, happy to changeThere 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.
As discussed w/ @antonymilne : Changes will be reverted here and handled properly in separate PR
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 #210