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

[BUG] ImportError on import buf/validate #96

Closed
o-murphy opened this issue Nov 6, 2023 · 4 comments
Closed

[BUG] ImportError on import buf/validate #96

o-murphy opened this issue Nov 6, 2023 · 4 comments
Labels
Bug Something isn't working

Comments

@o-murphy
Copy link

o-murphy commented Nov 6, 2023

Description

if we generate the python sources with buf and --include-imports flag, the protovalidate tries to import buf locally, but if we want to make generated sources a part of other module, the protovalidate raises ImportError on buf/validate import, so we need modify the protovalidate module sources to set where we want to import buf/validate

@o-murphy o-murphy added the Bug Something isn't working label Nov 6, 2023
@o-murphy o-murphy changed the title [BUG] [BUG] ImportError on import buf/validate Nov 6, 2023
@rodaine
Copy link
Member

rodaine commented Nov 17, 2023

Hey @o-murphy! Thanks for the report. Can you give more details about the steps you're running and the errors you are seeing?

@convoi
Copy link

convoi commented Nov 17, 2023

I believe I have the same problem.
My source code is structured like

src/my_module/stuff.py
protos/events/v1/some.proto

my buf.gen.yaml:

# buf.gen.yaml
version: v1

plugins:
  - plugin: buf.build/protocolbuffers/python:v21.9
    out: ./src/my_module
  - plugin: buf.build/protocolbuffers/pyi:v21.9
    out: ./src/my_module

I generate my python proto files into my module:

buf generate --include-imports protos

This puts the generated python classes and the buf validate files in

src/my_module/events
src/my_module/buf/validate

so if I wanted to importe the validate_pb2.py I need to do

 from my_module.buf.validate import expression_pb2

but protovalidate wants from buf.validate import expression_pb2.

(Disclaimer, to make generated python proto classes work with this module src layout you have to do a sed rewrite trick - I was just hoping that I do not need some other hacks for validation. Also here it would be trickier since I would have to rewrite protovalidate module )

@rodaine
Copy link
Member

rodaine commented Nov 17, 2023

Got it! Yes this is a known issue (also reported on buf) for protobuf python generation in general. There are some workarounds in that linked thread, but without a rewrite or a sys.path.insert, you'll need to generate into the root of the python project.

@rodaine rodaine closed this as completed Nov 17, 2023
@o-murphy
Copy link
Author

@rodaine

I want to distribute my project to PyPi, I use protovalidate as a dependency. I also want to generate *_fb2.py and buf files and distribute it as a part of my project, so I need to move generated *_fb2.py files and buf directory to the top-level package's dir. But in this case I get an import error if I use import protovalidate in one of the inner modules, cause the protovalidate tries to import the buf from the project root dir but not from the package top-level dir. It happen even if I change the import paths in a *_pb2.py files.

Now I have a temporary sollution: copying the protovalidate directly to my library and fix the import's in each module of protovalidate to solve it, for example:

# protovalidate.validator.py

from a7p.buf.validate import expression_pb2  # type: ignore
from a7p.protovalidate.internal import constraints as _constraints
from a7p.protovalidate.internal import extra_func

Can you provide me a way how can I set the top-level of my library to protovalidate module to get the valid import of buf module?

Look at this project tree please
In my case: pya7p - the project root and the a7p - the package top level dir

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants