godatadriven / pydantic-spark

MIT License
25 stars 8 forks source link

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

Open jjshinobi opened 4 months ago

jjshinobi commented 4 months ago

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 (https://github.com/godatadriven/pydantic-spark/blob/d61c660decf5745a3fc69ec386b945ddf72e5e11/src/pydantic_spark/base.py#L143). 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.

arcaputo3 commented 3 months ago

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.