Skip to content

python: fix bare cross-file imports in generated .pyi stubs#13

Merged
Bre77 merged 1 commit into
mainfrom
fix/pyi-relative-imports
Jul 22, 2026
Merged

python: fix bare cross-file imports in generated .pyi stubs#13
Bre77 merged 1 commit into
mainfrom
fix/pyi-relative-imports

Conversation

@Bre77

@Bre77 Bre77 commented Jul 22, 2026

Copy link
Copy Markdown
Member

protoc's pyi generator aliases same-package imports differently than its .py generator (_foo_pb2 vs foo__pb2), so protoletariat's rewriter - which matches whole import statements including the alias - leaves the pyi side as a bare import foo_pb2 as _foo_pb2 while the .py sibling correctly gets rewritten to from . import foo_pb2 as foo__pb2. A bare import only resolves when a top-level module of that name exists, which it doesn't once installed as a package, so pyright (and other static checkers) can't resolve types across these files even though runtime behavior is unaffected.

Affected: car_server_pb2.pyi, universal_message_pb2.pyi, vcsec_pb2.pyi, vehicle_pb2.pyi under command/, plus authorization_api_pb2.pyi, common_api_pb2.pyi, transport_pb2.pyi under energy_device/.

Found downstream in python-tesla-fleet-api while migrating it onto this package - pyright strict mode broke on RoutableMessage.signature_data, VehicleData.vehicleData, etc.

scripts/generate.sh now runs a small scripts/fix_pyi_imports.py pass after protol to patch any remaining bare pyi import whose target is an actual sibling module, so regeneration won't reintroduce this.

protoc's pyi generator aliases same-package imports differently than
its .py generator (_foo_pb2 vs foo__pb2), so protoletariat's rewriter -
which matches whole import statements including the alias - leaves the
pyi side as a bare `import foo_pb2 as _foo_pb2` while the .py sibling
correctly gets `from . import foo_pb2 as foo__pb2`. A bare import only
resolves when a top-level module of that name happens to exist, which
it doesn't once installed as a package - so pyright and other static
checkers can't resolve types across these files (car_server, vcsec,
vehicle, universal_message under command/, three files under
energy_device/), even though runtime behavior is unaffected since the
.py files are correct.

scripts/generate.sh now runs scripts/fix_pyi_imports.py after protol to
patch any bare pyi import whose target is an actual sibling module.
@Bre77
Bre77 merged commit ac30f3d into main Jul 22, 2026
1 check passed
Bre77 pushed a commit to Teslemetry/python-tesla-fleet-api that referenced this pull request Jul 22, 2026
0.5.0 fixes the generated .pyi cross-file import bug (Teslemetry/tesla-protocol#13) that broke pyright strict mode; pyright is now clean with no local suppressions needed.
Bre77 added a commit to Teslemetry/python-tesla-fleet-api that referenced this pull request Jul 22, 2026
* feat(proto): adopt tesla-protocol PyPI package as proto source

Replace the vendored proto/ sources and generated tesla_fleet_api/tesla/
vehicle/proto/ modules with the published tesla-protocol package
(Teslemetry/tesla-protocol, generated as tesla_protocol) - the API
client already adopted the npm sibling package, so this brings the
Python library onto the same shared, single source of truth. All
imports now resolve from tesla_protocol.command.<module>_pb2 instead
of the local vendored copies; tools/regenerate_protos.sh and
createProto.sh are gone along with the generated files they produced.

tesla-protocol requires protobuf<7,>=6.32.0 and stamps gencode 6.31.1,
both compatible with Home Assistant core's protobuf==6.32.0 pin.

tesla-protocol 0.4.0's generated .pyi stubs have an upstream bug (bare
cross-file imports that don't resolve, unlike the correctly relative
.py files) that breaks pyright strict mode on 14 lines in commands.py;
fixed upstream in Teslemetry/tesla-protocol#13. Left unsuppressed here
since masking it with local pyright ignores would hide a real upstream
defect - bump the tesla-protocol floor once a release with that fix
ships.

* chore(deps): bump tesla-protocol to >=0.5.0

0.5.0 fixes the generated .pyi cross-file import bug (Teslemetry/tesla-protocol#13) that broke pyright strict mode; pyright is now clean with no local suppressions needed.

* Bump version to 1.7.7

* no-mistakes(review): Restore legacy protobuf import compatibility

* no-mistakes(document): Refresh protobuf documentation and references

---------

Co-authored-by: firstmate crewmate <crewmate@firstmate.local>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant