pydantic / bump-pydantic

Convert Pydantic from V1 to V2 ♻
MIT License
323 stars 25 forks source link

Add default none for Field correctly #126

Closed brian-goo closed 1 year ago

brian-goo commented 1 year ago

None should be added if nothing was specified for default of Field if I understood correctly. This PR is to fix https://github.com/pydantic/bump-pydantic/issues/113 as well.

Before:

class Foo(BaseModel):
    a: Optional[int] = Field() # here
    b: Optional[int] = Field(...)
    c: Optional[int] = Field(None, description="spam")
    d: Optional[int] = Field(description="spam") # here
    e: Optional[int] = Field(default=..., description="spam")
    f: Optional[int] = Field(default=None, description="spam")

After:

class Foo(BaseModel):
    a: Optional[int] = Field(None) # here
    b: Optional[int] = Field(...)
    c: Optional[int] = Field(None, description="spam")
    d: Optional[int] = Field(None, description="spam") # here
    e: Optional[int] = Field(default=..., description="spam")
    f: Optional[int] = Field(default=None, description="spam")
Kludex commented 1 year ago

I've added you as a co-author @brian-goo FYI. 🙏