Open AdrienVannson opened 4 weeks ago
I think that the problem can be solved by moving some imports to the end of the generated files, just before all the rebuild_dataclass
. Before I try to do it and see if it actually work as expected, I have a question: is it something that we actually want to do?
Or should we consider that circular package dependencies are not acceptable and should indeed produce an error? As far as I know, they work correctly with the original gRPC python client and betterproto without pydantic, but I'm not sure about the other clients.
There's no reason for them not to work. Please if you do fix this add a test so this doesn't happen in the future.
Great, thanks! Actually, it seems that there is already a test for that: tests/inputs/import_circular_dependency
. However, this test is only executed without pydantic, so no error is shown.
If I replace plugin_output_package = "tests.output_betterproto"
by plugin_output_package = "tests.output_betterproto_pydantic"
in test_inputs.py
, I have the expected error... but a lot of other unrelated tests fail as well.
Actually, I have the impression that it is not that simple to fix. I tried putting the imports from get_type_reference
at the end of the file, as I said. However, in the test import_circular_dependency
, in the line of the generated file: other: "other.OtherPackageMessage" = betterproto.message_field(2)
, I am not sure why, but there seem to be a problem due to the fact that the name other
is used twice.
After I fixed that quickly by adding a prefix, in my project, I still had errors occurring during the calls to rebuild_dataclass
. As I'm not sure how long it would take me to make everything work and how many changes to the project will be needed, I think I'll stop here for now
In the end, I probably have a solution. I will test it, and if it works, I will send a PR once the other issues that I opened are fixed (I need the fix to solve this problem)
Summary
Using Pydantic creates circular imports
Reproduction Steps
Compile the following
.proto
files:A_a.proto
:A_b.proto
:B.proto
:There is no circular dependency between the
.proto
files. However, after the compilation, there is a circular dependency between the two packages.Expected Results
I compiled the
.proto
files with thepydantic_dataclasses
. I would expect importing the library withimport root.apackage
to work correctly.Actual Results
import root.apackage
fails with the following error:System Information
Checklist
pip install -U --pre betterproto
, if possible.Notes
When Pydantic is not used, the circular dependency is handled correctly by Python.