'pydantic's `Field`'s default value ignores constraint checks

from pydantic import BaseModel

class User(BaseModel):
  age: int = Field('foo', ge=0)


User()  # doesn't raise an error
# User(age='foo')

Why doesn't this raise an error since a string foo is passed even though an int is expected?

User(age='foo') however raises the ValidationError as expected.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source