Skip to content

fix(client): Keep the public client's type in with WorkOSClient() as workos - #771

Merged
gjtorikian merged 1 commit into
mainfrom
fix/client-context-manager-self-type
Nov 5, 2026
Merged

gjtorikian merged 1 commit into
mainfrom
fix/client-context-manager-self-type

Conversation

@stanleyphu

Copy link
Copy Markdown
Contributor

Description

with WorkOSClient() as workos: gives type checkers a client without any of its resources. __enter__ is annotated -> "WorkOSClient" in _base_client.py, where that name is the base class, not the public WorkOSClient from _client.py. AsyncWorkOSClient.__aenter__ has the same problem. The resources (workos.organizations, workos.user_management, …) and create_feature_flags_runtime_client() are defined only on the public subclasses, so every one of them is an error on the with target:

with WorkOSClient() as workos:
    workos.organizations.list_organizations()        # error
    workos.create_feature_flags_runtime_client()     # error

Anyone who opens a client with with or async with and type-checks their code hits this: pyright in its default mode, and mypy in any function it checks (an annotated one, or any with --check-untyped-defs). At runtime nothing changes; both methods already return self.

Both now return Self, imported from typing on 3.11+ and from typing_extensions on 3.10, as _types.py already does.

How I checked it:

  • Two new tests in test_generated_client.py use a resource and create_feature_flags_runtime_client() on the target of with WorkOSClient(...) and async with AsyncWorkOSClient(...). CI's pyright run covers tests/, so that's the regression check. On main it reports 4 errors, such as Cannot access attribute "create_feature_flags_runtime_client" for class "WorkOSClient". On this branch it reports 0.
  • I checked a user-style file outside the repo (the snippet above, plus the async with version) against this repo's install, with pyright 1.1.408 and mypy 2.4.1:
    • main: mypy finds 3 errors ("WorkOSClient" has no attribute "create_feature_flags_runtime_client" [attr-defined], "WorkOSClient" has no attribute "organizations", and the async one); pyright finds the same 3.
    • this branch: mypy Success: no issues found; pyright 0 errors.
  • pyright --pythonversion 3.10 on the changed files reports 0 errors, and test_generated_client.py passes on Python 3.10.20, which takes the typing_extensions import.
  • ruff format --check, ruff check and pyright pass, and the full suite passes: 3,745 tests.

Documentation

Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.

[ ] Yes

No.

🤖 Generated with Claude Code

… workos`

WorkOSClient.__enter__ and AsyncWorkOSClient.__aenter__ were annotated
with the base classes in _base_client.py, so type checkers saw the
target of a with statement as a client without any resources or helpers.
They return Self now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@stanleyphu
stanleyphu marked this pull request as ready for review November 5, 2026 07:47
@stanleyphu
stanleyphu requested review from a team as code owners November 5, 2026 07:47
@greptile-apps

greptile-apps Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge.

Summary

The PR preserves the public client's type inside with and async with by returning Self.

  • Context managers keep the public WorkOS client type.

Reviews (1) · Last reviewed commit: "fix(client): Keep the public client's ty..." · Reviewed by Greptile

@gjtorikian
gjtorikian merged commit 623a29c into main Nov 5, 2026
13 checks passed
@gjtorikian
gjtorikian deleted the fix/client-context-manager-self-type branch November 5, 2026 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants