danielgtaylor / python-betterproto

Clean, modern, Python 3.6+ code generator & library for Protobuf 3 and async gRPC
MIT License
1.56k stars 218 forks source link

Prevent users from creating messages with wrong parameters when pydantic is used #615

Closed AdrienVannson closed 2 months ago

AdrienVannson commented 2 months ago

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 https://github.com/danielgtaylor/python-betterproto/pull/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

AdrienVannson commented 2 months ago

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 commented 2 months ago

It is fixed now :)

Gobot1234 commented 2 months ago

Thank you