Skip to content

[python] Fix import for generation-subdir#10303

Open
msyyc wants to merge 8 commits intomainfrom
fix-import-generation-subdir
Open

[python] Fix import for generation-subdir#10303
msyyc wants to merge 8 commits intomainfrom
fix-import-generation-subdir

Conversation

@msyyc
Copy link
Copy Markdown
Contributor

@msyyc msyyc commented Apr 8, 2026

Description

Fix import generation when using a generation subdirectory (generation-subdir). This ensures imports are correctly resolved for generated code in subdirectories.

Changes

  • Fix import resolution in generator/pygen/codegen/serializers/__init__.py
  • Update regenerate-common.ts script
  • Add test cases for generation-subdir scenarios

Changelog

changeKind: fix
packages:
  - @typespec/http-client-python

@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:python Issue for the Python client emitter: @typespec/http-client-python label Apr 8, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 8, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-python@10303

commit: 1c446d7

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2026

All changed packages have been documented.

  • @typespec/http-client-python
Show changes

@typespec/http-client-python - fix ✏️

fix import for _validation.py/_types.py when "generation-subdir" is configured

@azure-sdk
Copy link
Copy Markdown
Collaborator

azure-sdk commented Apr 8, 2026

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@msyyc msyyc requested a review from m-nash as a code owner April 9, 2026 02:58
@microsoft-github-policy-service microsoft-github-policy-service bot added ide Issues for VS, VSCode, Monaco, etc. emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp eng labels Apr 9, 2026
@msyyc msyyc force-pushed the fix-import-generation-subdir branch from 6dc7451 to 34539c7 Compare April 9, 2026 03:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Python emitter import generation when generation-subdir is used so that generated code in subdirectories can correctly resolve shared modules like _validation.py and _types.py.

Changes:

  • Write _validation.py and _types.py into the generation-subdir output directory to match where operation/client code is generated.
  • Extend CI regeneration scripts/config to generate additional generation-subdir test packages and add a shared preprocess() helper.
  • Add new sync/async mock-api tests that import and exercise generated clients under generation.<subdir>._generated.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/http-client-python/generator/pygen/codegen/serializers/init.py Writes _validation.py / _types.py into generation_dir to fix relative imports under generation-subdir.
packages/http-client-python/eng/scripts/ci/regenerate.ts Switches to shared preprocess() and trims local emitter options.
packages/http-client-python/eng/scripts/ci/regenerate-common.ts Adds multi-output emitter configs (arrays) for new generation-subdir scenarios; introduces shared preprocess().
packages/http-client-python/tests/mock_api/shared/test_generation_subdir_for_generated_code.py New sync test covering generated code under generation-subdir.
packages/http-client-python/tests/mock_api/shared/test_generation_subdir2_for_generated_code.py New sync test validating _validation.py / _types.py usage under generation-subdir.
packages/http-client-python/tests/mock_api/shared/asynctests/test_generation_subdir_for_generated_code_async.py Async variant of generation-subdir coverage.
packages/http-client-python/tests/mock_api/shared/asynctests/test_generation_subdir2_for_generated_code_async.py Async variant validating _validation.py / _types.py under generation-subdir.
packages/http-client-python/tests/conftest.py Suppresses mock server output during tests.
packages/http-client-python/tests/mock_api/unbranded/test_unbranded.py Removes the “microsoft” sensitive-word assertion from generated unbranded outputs.
.chronus/changes/fix-import-generation-subdir-2026-3-8-12-7-49.md Adds a changelog entry for the fix.
Comments suppressed due to low confidence (1)

packages/http-client-python/tests/mock_api/unbranded/test_unbranded.py:58

  • The PR removes the "microsoft" sensitive-word assertion from test_sensitive_word() but this change isn’t mentioned in the PR description and significantly changes what the test is validating. Please either update the PR description to cover this behavioral change (and why it’s needed) or keep an equivalent assertion (updated expected set if needed) so we still detect unexpected "microsoft" occurrences in generated unbranded outputs.
def test_sensitive_word():
    check_folder = (Path(os.path.dirname(__file__)) / "../../generated/unbranded").resolve()
    assert [] == check_sensitive_word(check_folder, "azure")

Comment on lines +76 to +80
# Suppress server stdout/stderr to avoid confusing "Request validation failed" warnings
# in test output. Server readiness is validated via HTTP polling in wait_for_server().
if os.name == "nt":
return subprocess.Popen(cmd, shell=True, cwd=str(cwd), env=env)
return subprocess.Popen(cmd, shell=True, cwd=str(cwd), env=env, preexec_fn=os.setsid)
return subprocess.Popen(cmd, shell=True, cwd=str(cwd), env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return subprocess.Popen(cmd, shell=True, cwd=str(cwd), env=env, preexec_fn=os.setsid, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suppressing the mock server’s stdout/stderr makes failures much harder to diagnose (e.g., if the process exits immediately, we only get an exit code without any error output). Consider redirecting output to a log file (and surfacing it on startup failures) rather than sending it to DEVNULL, so CI failures remain actionable while still keeping normal test output clean.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked current log and confirm it is clean and could show enough error traceback

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp emitter:client:python Issue for the Python client emitter: @typespec/http-client-python eng ide Issues for VS, VSCode, Monaco, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants