Giter Club home page Giter Club logo

Comments (4)

rodaine avatar rodaine commented on June 20, 2024

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

from protovalidate-python.

convoi avatar convoi commented on June 20, 2024

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 )

from protovalidate-python.

rodaine avatar rodaine commented on June 20, 2024

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.

from protovalidate-python.

o-murphy avatar o-murphy commented on June 20, 2024

@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

from protovalidate-python.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.