Repository navigation
refactor(core): generate the sync client from the async client - #263
Merged
Merged
Conversation
SwgohComlinkAsync._admit returned its reserved slot only on asyncio.CancelledError, while the sync client returns it on any exception, KeyboardInterrupt included. A KeyboardInterrupt or other exception raised during an async pacing wait therefore kept the slot, and later calls to the endpoint waited out a call that never ran. Catch BaseException in both, so the clients behave the same and the sync loop can be generated from the async one without a runtime-specific branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
swgoh_comlink.py is now generated from swgoh_comlink_async.py by
.github/scripts/unasync.py, so the two clients can no longer drift. The
script rewrites tokens and then runs ruff to import-sort and format the
output. It uses only the standard library and ruff, so no new dependency.
- async/await are dropped.
- A grouping paren is removed only when it wraps a single awaited primary
expression, so precedence cannot change.
- Names, dotted names and the asyncio import are renamed from tables at
the top of the script.
- In docstrings and comments, only those names and the code in examples
change.
The async client becomes the source. Its runtime-specific prose is made
neutral ("an HTTP request", not "an async HTTP request"), and its class
docstring gains the context-manager example the sync one had. The
generated sync client differs from the hand-written one only by its
header and the reworded _admit docstring.
CI's lint job runs the script with --check, which prints a diff and fails
when swgoh_comlink.py is not what the async client generates. A hand edit
to swgoh_comlink.py was shown to fail it. tests/unit/test_unasync.py pins
the rewrite rules and checks the committed file is current.
CONTRIBUTING.md documents the workflow.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…kage subclasses A fixed stacklevel=4 assumed the client's own __init__ was the only frame between the caller and SwgohComlinkBase. A subclass defined inside the package adds one more, so the warning blamed that subclass's module and deduped onto its line. Count frames inside the package instead; skip_file_prefixes would do it but needs Python 3.12. Subclasses in user code are unchanged: the warning still points at their super().__init__ call, the first frame outside the package. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MarTrepodi
force-pushed
the
refactor/sync-client-codegen
branch
from
November 9, 2026 17:52
94b743d to
374541a
Compare
CI's lint job only checks that swgoh_comlink.py is current, so a forgotten regeneration surfaced at PR time. A local pre-commit hook now runs .github/scripts/unasync.py whenever either client or the generator is staged. When the file changes, the hook fails the commit; stage it and commit again. A hand edit to swgoh_comlink.py is overwritten the same way. It follows the existing local hooks (uv run, language: system) so ruff comes from uv.lock, as in CI. pre-commit sets unstaged changes aside first, so the generated file reflects only what is staged. CONTRIBUTING.md mentions the hook. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Description
Makes
SwgohComlinkAsyncthe single source for both clients:swgoh_comlink.pyis now generated fromswgoh_comlink_async.pyby.github/scripts/unasync.py, so the two can no longer drift. Two small fixes ride along, each in its own commit so each gets its own changelog line.fix(async): give back the pacing slot on any exception while sleepingSwgohComlinkAsync._admitreleased its reserved pacing slot only onasyncio.CancelledError. AKeyboardInterruptor other exception during a pacing wait kept the slot, so later calls to that endpoint waited out a call that never ran. It now catchesBaseException, as the sync client already did. This also lets the sync loop be generated without a runtime-specific branch.refactor(core): generate the sync client from the async clientruff, no new dependency), a--checkstep in CI's lint job,tests/unit/test_unasync.py, and a "Changing a client" section in CONTRIBUTING. The async client's docstrings are made runtime-neutral. The generatedswgoh_comlink.pydiffers from the hand-written one only by its# GENERATEDheader and the reworded_admitdocstring, so sync users see no runtime change.fix(client): attribute the credential deprecation warning past in-package subclassesACCESS_KEY/SECRET_KEYdeprecation warning used a fixedstacklevel=4, which assumes a fixed call depth and blames the wrong frame when a subclass defined inside the package adds one._stacklevel_outside_package()walks to the first frame outside the package instead (skip_file_prefixeswould do this natively but needs Python 3.12). User-code subclasses are unchanged.chore(ci): regenerate the sync client in a pre-commit hookunasyncpre-commit hook regeneratesswgoh_comlink.pywhenever either client or the generator is staged and fails the commit when the file changed, so a forgotten regeneration is caught at commit time instead of in CI. Follows the existinguv run/language: systemhooks; CONTRIBUTING mentions it.Why the warning fix is bundled
A computed stacklevel is the same expression in both runtimes, whereas a fixed number can differ between a sync method and its async twin when wrappers are involved. That makes it the pattern to use for warnings in code the generator emits.
Related Issues
None.
Type of Change
Checklist
developbranch (only release PRs targetmain)feat:,fix:,refactor:, etc.) — they become the changelogCHANGELOG.mdby hand (it is generated from commit messages)README.md,docs/,examples/) if user-facing behavior changeduv run pytest tests/)uv run ruff check src/ tests/anduv run ruff format --check src/ tests/)uv run ty check src/swgoh_comlink/)Testing
Static and unit (on this branch, rebased onto current
developwith ruff 0.17.4):uv run python .github/scripts/unasync.py --check: passes; generator output is byte-identical on Python 3.11, 3.12, 3.13 and 3.14.ruff check/ruff format --checkoversrc/ tests/ examples/ parity/: clean.ty check src/swgoh_comlink/: clean.pytest tests/: 1994 passed, 31 skipped; 100% coverage on_base.py,swgoh_comlink.pyandswgoh_comlink_async.py.unasynchook, run through pre-commit: passes on a clean tree; fails and regenerates when the async client is staged without the sync file (then passes once it is staged); reverts a staged hand edit toswgoh_comlink.py; is skipped for unrelated files; and with a partly staged async client, generates from the staged content only while the unstaged edit survives.Live, against the public Comlink release (spec 0.40.5):
urlpointed at that server): Sync and Asyncget_latest_game_data_version,get_player,get_guild,get_events, plus Asyncconcurrent_requests. All exit 0.RetryPolicy(min_interval=1): two/metadatacalls take 1.09 s (generated sync) and 1.07 s (async). A caller cancelled during its pacing wait gives its slot back; the next call is admitted in 0.05 s.ACCESS_KEY/SECRET_KEYset, the deprecation warning points at the constructing line in the caller's file for both clients.Reviewer notes:
swgoh_comlink_async.py, neverswgoh_comlink.py; then runuv run python .github/scripts/unasync.py, or let the pre-commit hook do it. CI fails on a hand edit.__all__and__comlink_type__become"SwgohComlink"). Runtime strings containing renamed words would be rewritten too; none do today.--check; a ruff bump that reformats code fails both together.🤖 Generated with Claude Code