Skip to content

Inherit from pydantic BaseModel (if possible) #1151

@gdementen

Description

@gdementen

We currently use a lot of pydantic internal API, which is bound to bite us at some point. This issue is about exploring pydantic v2 new features to see whether it is possible to avoid all those internal API use and simply inherit from BaseModel instead. See discussion from PR #1144 below:


I do not remember why I implemented a simplified version of the ModelMetaclass. I think it was to allow unloaded members after the initialization of the checked session (when reading from a file). I was also to allow the declaration of members without a default value in CheckedParameters (defined using the frozen flag of pydantic.

In the __init__ method of the CheckedSession class, we have:

            for name, field in self.__pydantic_fields__.items():
                value = input_data.pop(name, NotLoaded())

                if isinstance(value, NotLoaded):
                    if field.default is PydanticUndefined:
                        warnings.warn(f"No value passed for the declared variable '{name}'", stacklevel=2)
                        self.__setattr__(name, value, skip_frozen=True, skip_validation=True)
                    else:
                        self.__setattr__(name, field.default, skip_frozen=True)
                else:
                    self.__setattr__(name, value, skip_frozen=True)

            # --- undeclared variables
            for name, value in input_data.items():
                self.__setattr__(name, value, skip_frozen=True, stacklevel=2)

Maybe the v2 version of pydantic has a special mechanism to write a sort of dedicated __init__ method or to skip validation and set frozen to False during the initialization process only. So that we could make CheckedSession to inherit from pydantic BaseModel. But that requires more investigation in the pydantic documentation plus the time to make some proof of concept implementation. But I don't have time for the moment. But I can open an issue for that and come back to it later if you want.

Originally posted by @alixdamman in #1144 (comment)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions