How to parse unix timestamp into datetime without timezone in Fast API

You can use own @validator to parse datetime manually:

from datetime import datetime

from pydantic import BaseModel, validator


class Model(BaseModel):
    dt: datetime = None


class ModelNaiveDt(BaseModel):
    dt: datetime = None

    @validator("dt", pre=True)
    def dt_validate(cls, dt):
        return datetime.fromtimestamp(dt)


print(Model(dt=1600414328))
print(ModelNaiveDt(dt=1600414328))

Output:

dt=datetime.datetime(2020, 9, 18, 7, 32, 8, tzinfo=datetime.timezone.utc)
dt=datetime.datetime(2020, 9, 18, 10, 32, 8)

One more way to achieve it is to create custom type

class UnixDatetime(datetime):
    @classmethod
    def __get_validators__(cls):
        yield cls.validate

    @classmethod
    def validate(cls, v):
        if isinstance(v, datetime):
            print('Some request sends datetime not in UNIX format', file=sys.stderr)
            return v.replace(tzinfo=None)
        elif isinstance(v, int):
            return datetime.fromtimestamp(v)
        assert False, 'Datetime came of %s type' % type(v)