Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent user from creating messages with wrong parameters when pydantic is used #2

Closed
wants to merge 6 commits into from

Conversation

AdrienVannson
Copy link
Owner

@AdrienVannson AdrienVannson commented Sep 11, 2024

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 a ValidationError 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 updated mypy 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

  • If code changes were made then they have been tested.
  • This change has an associated test.

@AdrienVannson AdrienVannson deleted the branch todel October 8, 2024 11:09
@AdrienVannson AdrienVannson deleted the remove_type_check_condition branch October 8, 2024 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant