'Make Pydantic chuck error for wrong argument names
Suppose I have the following class:
class ModelConfig(pydantic.BaseModel):
name: str = "bert"
If I were to instantiate it with model_config = ModelConfig(name2="hello")
, this simply ignores that there is no name2
and just keeps name="bert"
. Is there a way to raise an error saying unknown argument in pydantic?
Solution 1:[1]
You can do this using the forbid
Model Config
For example:
class ModelConfig(pydantic.BaseModel, extra=pydantic.Extra.forbid): name: str = "bert"
Passing model_config = ModelConfig(name2="hello")
will throw an error
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | BeeGee |