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 users from creating messages with wrong parameters when pydantic is used #615

Merged

Conversation

AdrienVannson
Copy link
Contributor

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 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 for black, jinja and isort in pyproject.toml in order to be able to execute the tests... I'm not sure why it used to work before.

Checklist

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

Copy link
Collaborator

@Gobot1234 Gobot1234 left a 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 }
Copy link
Collaborator

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

@AdrienVannson
Copy link
Contributor Author

Thanks! The problem that I had was that if I set optional = True , the packages were not installed... but it should be possible somehow since it was how it was done before. I will have a look tomorrow

@AdrienVannson
Copy link
Contributor Author

It is fixed now :)

@Gobot1234 Gobot1234 merged commit 8fdcb38 into danielgtaylor:master Sep 12, 2024
19 checks passed
@Gobot1234
Copy link
Collaborator

Thank you

@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.

2 participants