Add a package option to the Python generator - #143
Merged
Merged
Conversation
The Python target now writes its output to __init__.py rather than device.py, so the output directory alone determines the import path. Generating into src/mypackage/mydevice produces the mypackage.mydevice package rather than a module below it, and the generated relative import of the companion converters module resolves unchanged from the package initializer. Closes harp-tech#141
The Python target now has a package option generating the interface as __init__.py together with a py.typed marker.
bruno-f-cruz
approved these changes
Sep 18, 2026
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.
The Python target gains a package option to generate the interface as
__init__.pytogether with apy.typedmarker, so the output directory alone determines the import path. Generating intosrc/mypackage/mydeviceproduces themypackage.mydevicepackage directly, and a device repository no longer needs a re-export module. The default is unchanged, so callers that do not set the option get the same singledevice.pymodule.The generated relative import of the companion
convertersmodule resolves unchanged from a package initializer, since__package__is the parent package in both forms, and no generated module reads__name__or__module__, so nothing in the output depends on being a leaf module.Public surface
PythonImplementationgainsPackageFileNameandTypedMarkerFileNamebeside the existingDeviceFileName, and anIsPackagemember with a default on the primary constructor.PythonGeneratortakes the flag with a default. Both defaults keep existing call sites compiling, so the change is additive.An option rather than a caller-supplied file name, because the generated module already emits
from .converters import ..., which means a configurable name could produce a module that imports from itself, and any file the generator emits later might do the same.Keeping a hand-written package root
A package that wants to own its root can still have one, by pointing
-oat a private subpackage and re-exporting from a root it writes itself. The generated relative import resolves against that subpackage, so a hand-writtenconvertersmodule moves down with it, and a hand-written module at the root can import the register classes freely.Notes for review
One constraint comes with package mode. A hand-written module beside the generated one must not import back into its own package root, because while
__init__.pyis executing the package is only partly initialized.converters.pynever does, importing only fromharp.protocol, but the failure it would produce is anImportErrorwhose message suggests renaming__init__.pyover a collision with a library name, which is not the cause.The interop fixture writes by enumerating the implementation rather than naming files, so the selection and the
py.typedemission are both covered by tests, with package mode being a test for the layout a published device package uses.The package README gains a paragraph on the import path, with a neutral
mypackageroot example.Closes #141