Skip to content

fix(python): pre-cache Union[str, bytes] in APIMethods._return - #1804

Merged
drstrangelooker merged 1 commit into
mainfrom
fix/python-union-str-bytes
Nov 16, 2026
Merged

drstrangelooker merged 1 commit into
mainfrom
fix/python-union-str-bytes

Conversation

@drstrangelooker

Copy link
Copy Markdown
Collaborator

Summary

Follows up on #1715 (01e249b) to complete the fix and regression coverage for #1705 in python/looker_sdk/rtl/api_methods.py:

  • Pre-cache _UNION_STR_BYTES = Union[str, bytes] at module scope: In Python 3.14, typing.Union and types.UnionType were merged and Union[...] objects are no longer cached singletons. Evaluating Union[str, bytes] inline inside APIMethods._return allocated a new Union instance on every non-binary SDK response. Pre-caching _UNION_STR_BYTES once at import time avoids per-call allocations while comparing via ==, and restores structure is str for the built-in str singleton.
  • Add regression tests in python/tests/rtl/test_api_methods.py:
    • test_return_plain_text_for_non_identical_union: Verifies APIMethods._return decodes plain-text SQL/CSV responses when structure is 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 generated Looker40SDK.run_inline_query for plain-text sql and csv responses.

Testing

  • pytest -v tests/rtl/ (138 passed, 3 skipped)
  • black --check looker_sdk/rtl/api_methods.py tests/rtl/test_api_methods.py

Developer Checklist

ℹ️

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Appropriate docs were updated (if necessary)

Fixes #1705 🦕

@drstrangelooker
drstrangelooker requested a review from a team as a code owner November 16, 2026 22:51

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@github-actions

Copy link
Copy Markdown
Contributor

Python Tests

 11 files  ± 0   11 suites  ±0   1m 40s ⏱️ -1s
172 tests +14  167 ✅ +14   5 💤 ±0  0 ❌ ±0 
971 runs  +48  943 ✅ +48  28 💤 ±0  0 ❌ ±0 

Results for commit 57ca788. ± Comparison against base commit 21b6ed1.

@drstrangelooker
drstrangelooker merged commit c0f4e06 into main Nov 16, 2026
32 checks passed
@drstrangelooker
drstrangelooker deleted the fix/python-union-str-bytes branch November 16, 2026 23:01
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.

Python 3.14: APIMethods._return raises DeserializeError for plain text responses (Union singleton identity check)

1 participant