Skip to content
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

Optional[Type] with default value None is not nullable #44

Open
jjshinobi opened this issue Jul 3, 2024 · 1 comment
Open

Optional[Type] with default value None is not nullable #44

jjshinobi opened this issue Jul 3, 2024 · 1 comment

Comments

@jjshinobi
Copy link

jjshinobi commented Jul 3, 2024

Hi,

According to https://docs.pydantic.dev/latest/migration/#required-optional-and-nullable-fields
we should be able to specify the class:

class MyModel(SparkBase):

    someProperty: Optional[str] = None
...

But, if we do so, the property is not nullable because it has the default value (

"nullable": "default" not in metadata and value.get("anyOf") is not None,
).
Note from the pydantic docs:

A field annotated as typing.Optional[T] will be required, allowing for a value of None. 
It does not mean that the field has a default value of None. (This is a breaking change from V1.)

Currently, we can only use someProperty: Optional[str] and we have to explicitly set None to the property in the object that is being validated.

@jjshinobi jjshinobi changed the title Optional[Type] with default value is not nullable Optional[Type] with default value None is not nullable Jul 3, 2024
@arcaputo3
Copy link

As a workaround we use someProperty: Optional[str] = Field(default_factory=lambda: None). This repo isn't being maintained as well as, for example, https://github.com/mitchelllisle/sparkdantic, so I'd take a look there as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants