-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Change of API in v2 to declare required and optional fields #2295
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
Comments
I was initially thrown off by the union operator, thinking "That's not valid!" but, after looking at it further, I must say, it is highly intriguing. It does look like it would solve the "What is Optional" issue that many have had. Would we even need/use If we added |
What would it mean for an optional field to not have a default value? Would the attribute be omitted from model instances and how would type checkers be made aware? The ellipsis is also only valid as a placeholder in functions: def foo(
# this type checks
bar: str = ...,
) -> None: ...
class Foo:
# this doesn't: 'Incompatible types in assignment (expression has type "ellipsis", variable has type "str")'
bar: str = ... |
I see potential for encountering a lot of AFAIK, you can't tell mypy that specific attr might be undefined on some instances. So we'll be forced to check What are the benefits of having such fields? |
Hmm that's already the case now when we force required fields with
And indeed it would need some extra work on the My main focus is making models more explicit and the DX. |
How's that? So by saying «optional field with no default value», you mean that if field was not in data, attr will still be present on model and value would be |
Sorry @MrMrRobat maybe I was not clear. And yes I just mean the default value used behind the scene is always |
'Forcing' required fields with the ellipsis is redundant which is why it hasn't been an issue so far, I guess.
Well, there are other type checkers out there, some of which do not support plug-ins. Pyright in particular has been gaining in popularity owing to its integration in VS Code. |
You're absolutely right. I only use from pydantic import BaseModel, Unset
class Model(BaseModel):
a: int | None
b: int | None | Unset # or `Undefined` or something else...
c: int | None | Unset with |
That seems reasonable from a typing standpoint, but I'm not sure I fully understand the use case, if you could explain how this would be useful. |
Sorry if I'm not clear :(
My main point is to be able to declare a field that may be omitted, but may not be null if the field is supplied |
I see, so this would be for fields that (a) can be omitted but (b) should also not have a default value in a JSON schema. The discussion of As an aside, if #990 would mean reverting to normal typing semantics and |
from pydantic import BaseModel, Unset
class Model(BaseModel):
a: int | None
b: int | None | Unset # or `Undefined` or something else...
c: int | None | Unset I don't think this is a great idea to attempt to capture this at the type level by using https://www.python.org/dev/peps/pep-0593/ from typing import Annotated, TypeVar, Optional
from pydantic import BaseModel
class _Unset:
pass
UNSET = _Unset()
class M1:
a: Optional[int]
b: Annotated[Optional[int], UNSET] Or T = TypeVar('T')
Unset = Annotated[T, UNSET]
class M2:
a: Optional[int]
b: Unset[Optional[int]] Also, note that proper NoneType and EllipsisType types have been added to 3.10 and should be supported by mypy in a future release. |
What would the value of |
Yes exactly! I also would like this for TypedDict (which I compare with TS interfaces). Thanks for sharing this thread! |
Looks like the PEP the first version of the PEP has been posted the typing-sig mailing list. https://mail.python.org/archives/list/typing-sig@python.org/thread/3NJZS7VCHSF54MD465SO7AF3AXGBGEDO/ It looks like the current version of the spec,
|
Thanks for the update! I hope this is something that we'll be able to leverage. |
This issue was move 4A20 d to a discussion.
You can continue the conversation there. Go to discussion →
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone!
I'm playing a lot with the new builtin generic types in 3.9 and type union operator introduced in 3.10 (I even wrote a backport for 3.6+ since I love them so much 😆).
Playing with them and pydantic, I really feel like the API can be challenged for v2. The idea is:
All fields without a default value would be required and all fields with a default value would be optional.
For example
It would imply that
Optional[str]
does not mean "optional" but more "nullable" (which is what it is 😄)To define an optional field without actually setting a default value, I suggest we use
Ellipsis
. I like the fact that we don't need to import aUndefined
sentinel to say "no value at all".It would be a bit confusing when switching from v1 to v2 since
...
is currently used to define a required field but IMHO it makes more sense to write a model this way with a homogeneous API.For
Field
it would just meanField(description='')
is required whenField(..., description='')
is optional.For
create_model
it's the same@samuelcolvin @StephenBrown2 @MrMrRobat others WDYT?
Partially related issue #990
Related issue #1223
The text was updated successfully, but these errors were encountered: