Repository navigation
Fix Python typing for bytes in typespec - #11233
Conversation
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
|
Copilot continue commit for this PR and current progress is : #11233 (comment) NOTE: CI repports the following error: |
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Fixed the pylint |
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
|
Copilot fix the following typing check error in generated code of spector case |
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
... Fixed in bf61eb3. |
There was a problem hiding this comment.
Pull request overview
This PR updates the Python HTTP client emitter to support backward-compatible typing for binary bytes request bodies by adding an IO[bytes] overload alongside bytes, and adjusts codegen to avoid redundant runtime branching that breaks mypy type narrowing.
Changes:
- Extend preprocess
add_body_param_typeto emit a combinedbytes | IO[bytes]body type for non-JSON, non-multipartbytesbodies (excludingtypeddictmode). - Update request-body serialization generation to collapse the
isinstancebranch when both overloads serialize to raw requestcontent, preventing mypy narrowing issues. - Add unit + mock API coverage (sync/async) and a Chronus changelog entry for the fix.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/http-client-python/tests/unit/test_bytes_body_overloads.py | Unit tests validating the new combined body type and overload behavior. |
| packages/http-client-python/tests/mock_api/shared/test_encode_bytes.py | Mock API sync test exercising passing io.BytesIO for binary bytes bodies. |
| packages/http-client-python/tests/mock_api/shared/asynctests/test_encode_bytes_async.py | Mock API async test exercising passing io.BytesIO for binary bytes bodies. |
| packages/http-client-python/generator/pygen/utils.py | Update body-type description helper to label bytes as “byte” in overload content-type docs. |
| packages/http-client-python/generator/pygen/preprocess/init.py | Add preprocess logic to introduce IO[bytes] overload for binary bytes bodies. |
| packages/http-client-python/generator/pygen/codegen/serializers/builder_serializer.py | Collapse redundant binary/non-binary isinstance branching when both paths serialize to raw content. |
| packages/http-client-python/generator/pygen/codegen/models/operation.py | Avoid importing IOBase when the combined type is the bytes/IO[bytes] case and the runtime branch is removed. |
| .chronus/changes/http-client-python_fix-bytes-body-io-overload-2026-7-14-3-14-0.md | Changelog entry documenting the fix for @typespec/http-client-python. |
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
commit: |
|
All changed packages have been documented.
Show changes
|
…es body as binary Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 082cab3e-1c55-4eb5-951a-819a9842137f
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 082cab3e-1c55-4eb5-951a-819a9842137f
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
packages/http-client-python/generator/pygen/utils.py:118
- The PR description calls out describing the
bytesoverload content-type parameter as a "byte" body, butget_body_type_for_descriptionmapsbytesto "binary", so the generated description will say "binary body" for bothbytesandIO[bytes]overloads.
return "binary"
if body_parameter["type"]["type"] == "string":
return "string"
return "JSON"
…y-statements Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 082cab3e-1c55-4eb5-951a-819a9842137f
Design
design is approved in teams channel
Summary
Add an
IO[bytes]overload alongsidebytesfor binarybytesbodies in the Python emitter, to keep backward compatibility for services migrating from swagger (whose binary bodies were typed asIO).bytesbodies in preprocess (add_body_param_type)bytesoverload content-type param as "byte" bodytest_bytes_body_overloads.py) — passingCode diff
Azure/azure-sdk-for-python#48040