arkh
arkh

Reputation: 63

How to keep the attributes strictly to the properties of a pydantic class

For example, this is my class:

from pydantic import BaseModel

class Config(BaseModel):
    name: str
    age: int

And here's a dictionary:

data = {"name": "XYZ", "age": 18, "height": "180cm"}

Now because height is not a property of the Config class, doing

d = Config(**data)

should throw an error but it doesn't. How can I make it throw an error if the dictionary isn't exactly what the class wants?

Upvotes: 2

Views: 672

Answers (1)

user3064538
user3064538

Reputation:

Add this

from pydantic import Extra


class Config(BaseModel, extra=Extra.forbid):
    ...

The default is Extra.ignore.

https://pydantic-docs.helpmanual.io/usage/model_config/

Upvotes: 2

Related Questions