The following code shows a pylance (pyright) error for AModel()
of Argument missing for parameter "field_b"
:
from pydantic import BaseModel, Field
from typing import Optional, Any
class AModel(BaseModel):
field_a: str = Field()
field_b: Optional[bool] = Field(None)
instance_1 = AModel(field_a="", field_b=None) # No error
instance_2 = AModel(field_a="") # Error
# ^^^^^^^^^^^^^^^^^^
kwargs: dict[str, Any] = {"field_a": "", "field_bad": True}
instance_3 = AModel(**kwargs) # No error but no type checking
Is it possible to instantiate this model without providing field_b=None
and whilst retaining type checking?