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

Add default none for Field correctly #126

Closed
wants to merge 2 commits into from

Conversation

brian-goo
Copy link
Contributor

@brian-goo brian-goo commented Sep 17, 2023

None should be added if nothing was specified for default of Field if I understood correctly.
This PR is to fix #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")

" h: Optional[int] = Field(None)",
" i: Optional[int] = Field(default_factory=lambda: None)",
" h: Optional[int] = Field(...)",
" i: Optional[int] = Field(None, default_factory=lambda: None)",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. You shouldn't have both default and default_factory being set.

@Kludex
Copy link
Member

Kludex commented Sep 18, 2023

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

@Kludex Kludex closed this Sep 18, 2023
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

Successfully merging this pull request may close these issues.

Required Optional field that uses Field(...) is made Non-Required
2 participants