Repository navigation
fix(python): pre-cache Union[str, bytes] in APIMethods._return - #1804
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request refactors the response type checking in api_methods.py by introducing a _UNION_STR_BYTES constant and using identity comparison for the str type. It also adds regression tests to ensure that equivalent but non-identical union structures (such as Union[bytes, str] or PEP 604 union types in Python 3.10+) are correctly evaluated. I have no feedback to provide as there are no review comments.
Contributor
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.
Summary
Follows up on #1715 (
01e249b) to complete the fix and regression coverage for #1705 inpython/looker_sdk/rtl/api_methods.py:_UNION_STR_BYTES = Union[str, bytes]at module scope: In Python 3.14,typing.Unionandtypes.UnionTypewere merged andUnion[...]objects are no longer cached singletons. EvaluatingUnion[str, bytes]inline insideAPIMethods._returnallocated a newUnioninstance on every non-binary SDK response. Pre-caching_UNION_STR_BYTESonce at import time avoids per-call allocations while comparing via==, and restoresstructure is strfor the built-instrsingleton.python/tests/rtl/test_api_methods.py:test_return_plain_text_for_non_identical_union: VerifiesAPIMethods._returndecodes plain-text SQL/CSV responses whenstructureis equal (==) but not identical (is) to_UNION_STR_BYTES(Union[bytes, str],str | bytes,bytes | str), catching identity-check regressions across Python 3.10–3.14.test_run_inline_query_plain_text_response: End-to-end verification through generatedLooker40SDK.run_inline_queryfor plain-textsqlandcsvresponses.Testing
pytest -v tests/rtl/(138 passed, 3 skipped)black --check looker_sdk/rtl/api_methods.py tests/rtl/test_api_methods.pyDeveloper Checklist
ℹ️
Fixes #1705 🦕