[Python] How should I specify default values on Pydantic fields with "Validate Always" to...

Discussão em 'Python' iniciado por Stack, Junho 30, 2025.

  1. Stack

    Stack Membro Participativo

    My type checker moans at me when I use snippets like this one from the Pydantic docs:

    from datetime import datetime

    from pydantic import BaseModel, validator


    class DemoModel(BaseModel):
    ts: datetime = None # Expression of type "None" cannot be
    # assigned to declared type "datetime"

    @validator('ts', pre=True, always=True)
    def set_ts_now(cls, v):
    return v or datetime.now()


    My workarounds so far have been:

    ts: datetime = datetime(1970, 1, 1) # yuck


    ts: datetime = None # type: ignore


    ts: Optional[datetime] = None # Not really true. `ts` is not optional.


    Is there a preferred way out of this conundrum?

    Or is there a type checker I could use which doesn't mind this?

    Continue reading...

Compartilhe esta Página