-
Notifications
You must be signed in to change notification settings - Fork 214
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 users from creating messages with wrong parameters when pydantic is used #615
Prevent users from creating messages with wrong parameters when pydantic is used #615
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than the packaging issues seems like a good change to me
@@ -12,21 +12,20 @@ packages = [ | |||
] | |||
|
|||
[tool.poetry.dependencies] | |||
python = "^3.7" | |||
black = { version = ">=23.1.0", optional = true } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not using optional here is not acceptable for libraries that bundle their protobufs as it will install black, isort etc
Thanks! The problem that I had was that if I set |
It is fixed now :) |
Thank you |
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 version to avoid having the bug mentionned in #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. This also has the benefit of making the code a bit more clear.One point I am not completely sure about: I had to remove
optional = true
forblack
,jinja
andisort
inpyproject.toml
in order to be able to execute the tests... I'm not sure why it used to work before.Checklist