Skip to content

Use src layout for generated Python packages - #228

Merged
leileizhang (lei9444) merged 3 commits into
mainfrom
lei9444-python-generated-src-layout
Oct 30, 2026
Merged

leileizhang (lei9444) merged 3 commits into
mainfrom
lei9444-python-generated-src-layout

Conversation

@lei9444

Copy link
Copy Markdown
Contributor

Summary

Fix static analysis of generated Python packages installed with default pip install -e. Previously, Python could import the package through setuptools' dynamic editable finder, but mypy and Pyright could report missing imports. Generate a standard src layout so default editable installs expose an ordinary source-path .pth that both checkers understand.

Before: <output>/__init__.py, implementation modules, namespace packages
After:  <output>/src/<import_name>/__init__.py, implementation modules, namespace packages
Both:   <output>/pyproject.toml and project-level generation metadata

Bootstrap runtime resources move with the import package. Its build-only hook stays outside that package and is excluded from installed wheels. Generation remains transactional, including package renames, ownership checks and rollback.

Reproduction and expected behavior

Generate and install into a dedicated consumer environment with matching runtime/codegen versions:

dynwinrt-codegen generate --namespace Windows.Foundation --class-name Uri --lang py --output .\generated_uri
python -m pip install -e .\generated_uri

Check this consumer using that environment with mypy and Pyright:

from generated_uri.windows.foundation import Uri

Uri("https://example.com")  # accepted
Uri(123)                    # expected argument-type error

Before: the editable package could be imported at runtime while static analysis failed to resolve it. After: the valid call passes, and the invalid call is rejected as an argument-type error. The negative diagnostic is the expected successful validation, not a generator failure.

Compatibility and upgrade

--output remains the installable project root. Installation commands, normalized import/distribution names, public imports, the exact runtime dependency pin, and runtime API/ABI are unchanged. JS output layout is unchanged; this does not change async callback behavior.

Breaking source-layout change: clean old generated output, regenerate the complete selection, and reinstall. Old-layout output is rejected rather than partially migrated. Code that deliberately imports uninstalled sources must use <output>/src instead of output.parent. Same-path editable regeneration exposes added types to new Python/checker processes without reinstalling; it is not module hot reload.

Validation

All checks used the actual modified codegen, not relocated prototype output.

Check Result
eng/release/python/test_generated_python_layout.py 33 cases passed across no/single/dual-architecture Bootstrap, pip 25.0.1, uv 0.12.1, Poetry 2.5.1, setuptools 68.0.0/84.2.1, wheel/sdist, normal/default editable installs, project sync and incremental generation without reinstall
mypy 2.4.0 / Pyright 1.1.414 consumers Valid calls accepted; invalid arguments rejected; added types discovered without reinstall
Codegen regression tests 1,133 distinct tests passed cumulatively: 1,130 in the full run, then the three initially filtered tests passed under exact selectors after restoring pinned metadata/reusing a trusted JS runtime; 0 failures, 1 existing ignored test. No second full-suite run is claimed
Long-path, upgrade and output checks Typed/untyped packaging, output renames, old-layout rejection/clean upgrade, resource hashes and rollback passed; before/after JS output was byte-identical

Source-level typing regressions used the repository's mypy <2 constraint (1.20.2); installed consumers passed 2.4.0. cargo fmt --all -- --check and git diff --check passed.

Local execution was CPython 3.12 x64; other Python versions and native ARM64 CI have not been run locally. Actual Pylance host startup was attempted twice but blocked before extension startup by the existing VS Code update mutex, so Pylance is not marked passed. No GUI or native Windows App Runtime initialization was exercised. The unrelated E2E runner count assertion (20 != 19) was reproduced with the same selector/interpreter on both main and this branch and is not changed here.

Keep project manifests and resource ownership at the output root, relocate package sources and build-only hooks, and validate packaging, editable imports and upgrade protection.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Mixed-language test coverage

Workflow status: ✅ Passed

Layer Lines Functions Branches/regions
Rust, including native .pyd/.node 86.6% 82.03% 86.2% regions
Python aggregate 70.97% n/a 38.22% branches
Python runtime 98.28% n/a 95% branches
Generated Python WinRT projections 68.86% n/a 30.46% branches
Generated Python WinRT implementations 72% n/a 46.3% branches
JavaScript aggregate 18.38% 24.85% 55.94% branches
JavaScript runtime 41.73% 41.53% 78.33% branches
Generated WinRT projections 17.27% 19.36% 52.51% branches
Generated WinRT implementations 45.99% 59.19% 60.97% branches
Generated Classic COM projections 11.38% 23.88% 53.1% branches

View workflow run and download full HTML/LCOV/XML reports

Point both standard and implementation consumers at each generated project's src root. Verify orchestrator error propagation and MYPYPATH restoration, and exercise src paths in coverage fixtures.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Preserve AsyncEventScope and its cancellation regressions alongside generated src package installation and editable checks. Adapt the new async event runner and sample to the project/source layout.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@lei9444
leileizhang (lei9444) merged commit f668386 into main Oct 30, 2026
33 checks passed
@lei9444
leileizhang (lei9444) deleted the lei9444-python-generated-src-layout branch October 30, 2026 04:33
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.

3 participants