Use relative imports for same-package pb2 modules#2
Open
LysanderGG wants to merge 1 commit intomainfrom
Open
Conversation
When a twirp file imports a pb2 module from the same package directory, generate a relative import (from . import v1_pb2) instead of an absolute one (from crypto import v1_pb2). This fixes ModuleNotFoundError when generated files are nested under a different top-level package. Cross-package imports (e.g. google.protobuf) remain absolute. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Author
|
This is a plugin-only change — no runtime modification needed. After merging, a new |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_pb2module from the same package directory, emit a relative import (from . import v1_pb2) instead of an absolute one (from crypto import v1_pb2)google.protobuf) remain absoluteModuleNotFoundErrorwhen generated files are nested under a different top-level package (e.g.bridge.proto.crypto.v1_twirptryingfrom crypto import v1_pb2)Context
In the marketdata-pipeline bridge, protos are at
crypto/v1.proto(relative to--proto_path) but the generated Python files live underbridge/proto/crypto/. The absolute importfrom crypto import v1_pb2fails becausecryptois not a top-level Python package.Changes
import_builder.go: track current file's directory, emit.when import path matchesgenerator.go: pass current filename tonewImportBuildergenerator_test.go: updated tests covering same-package (relative) and cross-package (absolute) casesTest plan
from . importgoogle.protobuf) stay absolute🤖 Generated with Claude Code