How do I handle typing for extra fields of models?

34 Views Asked by At
class MyModel(BaseSettings):
    model_config = SettingsConfigDict(
        extra="allow",
    )
    my_field: str

obj = MyModel({"my_field":"sdfsd", "my_extra":"blabh"})

# Type of "my_extra" is unknown
obj.my_extra
# Same typing error
var = str(c.not_in_model)
# Same typing error
var = cast(str, c.not_in_model)

If I use extra fields like this and I'm doing strict type checking how do I deal with this typing error? Do I just ignore the lines where I use it or is there a way to tell the type checkers what it is?

0

There are 0 best solutions below