Skip to content

refactor(core): generate the sync client from the async client - #263

Merged
MarTrepodi merged 4 commits into
developfrom
refactor/sync-client-codegen
Nov 9, 2026
Merged

MarTrepodi merged 4 commits into
developfrom
refactor/sync-client-codegen

Conversation

@MarTrepodi

@MarTrepodi MarTrepodi commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Makes SwgohComlinkAsync the single source for both clients: swgoh_comlink.py is now generated from swgoh_comlink_async.py by .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.

Commit Change
fix(async): give back the pacing slot on any exception while sleeping SwgohComlinkAsync._admit released its reserved pacing slot only on asyncio.CancelledError. A KeyboardInterrupt or other exception during a pacing wait kept the slot, so later calls to that endpoint waited out a call that never ran. It now catches BaseException, 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 client Adds the generator (standard library + ruff, no new dependency), a --check step 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 generated swgoh_comlink.py differs from the hand-written one only by its # GENERATED header and the reworded _admit docstring, so sync users see no runtime change.
fix(client): attribute the credential deprecation warning past in-package subclasses The ACCESS_KEY/SECRET_KEY deprecation warning used a fixed stacklevel=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_prefixes would do this natively but needs Python 3.12). User-code subclasses are unchanged.
chore(ci): regenerate the sync client in a pre-commit hook A local unasync pre-commit hook regenerates swgoh_comlink.py whenever 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 existing uv run / language: system hooks; 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

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactor (code change that neither fixes a bug nor adds a feature)
  • Performance improvement
  • Documentation
  • Tests
  • CI / build / dependencies

Checklist

  • This PR targets the develop branch (only release PRs target main)
  • My commits follow the Conventional Commits / Angular convention (feat:, fix:, refactor:, etc.) — they become the changelog
  • I have not edited CHANGELOG.md by hand (it is generated from commit messages)
  • I have added/updated docstrings with type hints for any new or changed public methods
  • I have updated the docs (README.md, docs/, examples/) if user-facing behavior changed
  • I have added unit tests that cover my changes (mocked, not requiring a live comlink service)
  • Tests pass (uv run pytest tests/)
  • Ruff linter and formatter pass (uv run ruff check src/ tests/ and uv run ruff format --check src/ tests/)
  • Type check passes (uv run ty check src/swgoh_comlink/)
  • I have not bundled unrelated changes in this PR (the warning fix is related, see above)

Testing

Static and unit (on this branch, rebased onto current develop with 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 --check over src/ tests/ examples/ parity/: clean.
  • ty check src/swgoh_comlink/: clean.
  • pytest tests/: 1994 passed, 31 skipped; 100% coverage on _base.py, swgoh_comlink.py and swgoh_comlink_async.py.
  • commitlint (CI's hook) passes on all four commits.
  • The unasync hook, 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 to swgoh_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):

  • Ran the committed examples unchanged (default url pointed at that server): Sync and Async get_latest_game_data_version, get_player, get_guild, get_events, plus Async concurrent_requests. All exit 0.
  • Pacing with RetryPolicy(min_interval=1): two /metadata calls 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.
  • With ACCESS_KEY/SECRET_KEY set, the deprecation warning points at the constructing line in the caller's file for both clients.

Reviewer notes:

  • Edit swgoh_comlink_async.py, never swgoh_comlink.py; then run uv run python .github/scripts/unasync.py, or let the pre-commit hook do it. CI fails on a hand edit.
  • The generator's text renames apply to every string literal, not only docstrings (that is how __all__ and __comlink_type__ become "SwgohComlink"). Runtime strings containing renamed words would be rewritten too; none do today.
  • The ruff version now affects both the format check and --check; a ruff bump that reformats code fails both together.

🤖 Generated with Claude Code

@MarTrepodi MarTrepodi self-assigned this Nov 9, 2026
@MarTrepodi MarTrepodi added the code maintenance General code housekeeping items label Nov 9, 2026
MarTrepodi and others added 3 commits November 9, 2026 12:49
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
MarTrepodi force-pushed the refactor/sync-client-codegen branch from 94b743d to 374541a Compare November 9, 2026 17:52
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>
@github-actions github-actions Bot added documentation Improvements or additions to documentation ci Changes related to continuous integration processes code testing labels Nov 9, 2026
@MarTrepodi
MarTrepodi merged commit 6e24746 into develop Nov 9, 2026
12 checks passed
@MarTrepodi
MarTrepodi deleted the refactor/sync-client-codegen branch November 9, 2026 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Changes related to continuous integration processes code maintenance General code housekeeping items code documentation Improvements or additions to documentation testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant