Prevent user from creating messages with wrong parameters when pydantic is used #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Python's dataclass prevent the user from creating instances of a dataclass with invalid fields (see the test that I added). However, this is not the case by default with Pydantic: the invalid fields are just removed from the message ( https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.extra ).
This can be the cause of errors if there is a typo in the name of a parameter. For this reason, I think it is better to add
config={"extra": "forbid"}
to pydantic dataclasses so that aValidationError
is raised in such a situation.To avoid adding type checking errors (this parameter does not exist in python's dataclasses), I had to remove the
if TYPE_CHECKING
condition that was used to import Python's dataclass instead. I updatedmypy
to its last versionto avoid having the bug mentionned in danielgtaylor#460 . To do this, it was needed to remove support for python 3.7... but I think it is fine given the fact that the tests are no longer run with this python version, which is very old anyway.Checklist