-
-
Notifications
You must be signed in to change notification settings - Fork 298
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
🧹 Clarification: Optional properties and null vs. undefined when used in languages like Python that only has a single None type #1586
Comments
I think @Julian is probably the best person to comment on Python-specific things. I do have a question: would you consider this to be valid data? {
"requiredProp": "",
"optionalProp": null
} Specifically, is a null value interpreted by your code the same as the property just being absent? I'd guess that the Pydantic folks might think it is valid if the property is optional (null and absence are the same), whereas maybe you don't. As far as JSON Schema is concerned, a property with a null value is distinct from the absence of that property. This is the design intent of JSON Schema. |
(The JSON Schema spec doesn't cover schema generation from a language's types, so a bit of this discussion will always be groundless. But nevertheless, yes, opinions below.) What you're asking is mostly about "shortcomings" in the typing annotation system in Python really more than anything else I think. And I put "shortcomings" in quotes here because the case where this matters -- at least when it comes to classes -- is one I would call a bad idea in Python, so I don't personally cry too hard about it not being possible.
class Something:
requiredProp: str
optionalProp: Optional[str] = None I disagree that even this expresses the JSON / JSON Schema notion of "optionalProp may not be present". That notion is expressed by Specifically, for classes it really would look like: class _S1:
requiredProp: str
optionalProp: str
class _S2:
requiredProp: str
Something: _S1 | _S2 but there's no shorthand for that, and clearly it's untenable for multiple such properties -- and again I think for normal Python classes it's ridiculous to design one which sometimes doesn't have an attribute (but this is the direct parallel to dicts not having a key).
I short I'd disagree with that both from a JSON Schema perspective and from a Python developer's perspective, though one not really familiar with Pydantic's norms. I'm not saying this solves the upstream problem, just that "treat None specially" seems very wrong. To me my first guess would be an annotation a la |
As Julian said, the spec doesn't cover how schemas map to a language's type system, but I can share my opinion. First, a couple things the keep in mind. Remember that JSON Schema describes JSON, not JavaScript and IMO, that makes JSON's So, I don't think using a This approach also has the benefit of resulting in simpler and more idiomatic JSON Schemas. Again, there's no official correct or incorrect way to do this. This is just my recommendation. |
(Responding again just in case you didn't know the below Jason, but if you did and still think your way obviously all fine to disagree:
Python's
|
Thanks for the correction Julian! It's been a while since I've written Python and didn't remember that correctly. That means that Python's However, JSON that uses |
@Ark-kun does the above answer your questions? |
Specification section
?
What is unclear?
Please help us.
Pydantic v2 started converting Python's
Optional[str]
type to{"anyOf":[{"type":"string"}, {"type":"null"}]}
Json Schema instead of an optional string property.This breaks many existing tools that use JsonSchemas, but the maintainer claims that JsonSchema is designed this way. pydantic/pydantic#7161Please help us get clarity whether this is really what Json Schema spec design intends.
I want to ask whether this is indeed the intention of JsonSchema design and if it's not the case, then hopefully the maintainers can be persuaded to restore the previous behavior.
Problem background:
Javascript has
null
andundefined
types.Python has
None
singleton type. It's automatically used in some cases. For example, when function does not return anything, the actual returned value isNone
.Let's look at this simple JsonSchema that has an optional field:
Now let's try to represent such schema using Python:
For this type, Pydantic v2 produces the following JsonSchema:
Notice that the "optionalProp" is required and it's type declaration is
{"anyOf":[{"type":"string"}, {"type":"null"}]}
.And if we slightly change the class to add the default value:
The generated schema becomes
The
optionalProp
type declaration still remains{"anyOf":[{"type":"string"}, {"type":"null"}]}
.So it's not possible to generate a normal optional string property.
Is it the intention of JsonSchema that programming languages that do not have the
undefined
/null
duality of Javascript cannot adhere to simple JSON schemas with simple optional properties?Would it be OK to treat Python's
None
as Javascript'sundefined
in cases of optional function/constructor parameters or are these types considered to be fundamentally different?Proposal
I propose to clarify that in non-JS languages optional properties with the default
None
/NULL
/nil
value can be treated as Javascript'sundefined
and can be described using JsonSchema's optional property mechanism.Do you think this work might require an [Architectural Decision Record (ADR)]? (significant or noteworthy)
No
The text was updated successfully, but these errors were encountered: