Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
refactor(connected-accounts,py): hoist terminal_states to module-level frozenset
Promote the in-method `terminal_states = ("FAILED", "EXPIRED", "REVOKED")` tuple to a module-level `_TERMINAL_CONNECTION_STATES: FrozenSet[str]` constant alongside `_LEGACY_RETIRING_OAUTH_SCHEMES` (line 29). Two benefits: - `frozenset` signals set-membership semantics (which is exactly what `if self.status in ...` does), versus `tuple` which signals an ordered sequence. - The TS-contract docstring travels with the constant, making the invariant discoverable without descending into the method body. Surfaced by kieran-python review of PR #3344 (P3#1).
jkomyno committed
May 7, 2026 at 10:54 UTC
d5bead2524bf4f79935382cb011a1f01231e04b4
1 file changed
+10
-9
python/composio/core/models/connected_accounts.py
+10
-9
@@ -28,6 +28,15 @@ logger = logging.getLogger(__name__)
28
# 2026-05-08 / 2026-07-03 retirement when the auth config is Composio-managed.
29
_LEGACY_RETIRING_OAUTH_SCHEMES = frozenset({"OAUTH1", "OAUTH2", "DCR_OAUTH"})
30
31
+# Mirrors the TS contract (`ConnectionRequest.ts` `terminalErrorStates`):
32
+# FAILED / EXPIRED / REVOKED are credentials-gone states and should raise
33
+# immediately rather than poll until timeout. INACTIVE is intentionally
34
+# excluded — it means "user disabled, can be reactivated", and a connection
35
+# can briefly transit through it before settling on ACTIVE.
36
+_TERMINAL_CONNECTION_STATES: t.FrozenSet[str] = frozenset(
37
+ {"FAILED", "EXPIRED", "REVOKED"}
38
+)
39
+
40
# One-time-per-process guard so long-running services don't spam the deprecation
41
# warning on every initiate() call.
42
_legacy_initiate_warning_emitted = False
@@ -74,20 +83,12 @@ class ConnectionRequest(Resource):
83
"""
84
timeout = self.DEFAULT_WAIT_TIMEOUT if timeout is None else timeout
85
deadline = time.time() + timeout
77
- # Mirror the TS contract (`ConnectionRequest.ts` `terminalErrorStates`):
78
- # FAILED / EXPIRED / REVOKED are terminal — credentials are gone and
79
- # cannot transition back to ACTIVE without a fresh auth flow, so
80
- # polling until timeout is wasted wall-clock time.
81
- # INACTIVE is intentionally NOT terminal: it means "user disabled, can
82
- # be reactivated", and a connection can briefly transit through it
83
- # before settling on ACTIVE — failing fast there would regress callers.
84
- terminal_states = ("FAILED", "EXPIRED", "REVOKED")
86
while deadline > time.time():
87
connection = self._client.connected_accounts.retrieve(nanoid=self.id)
88
self.status = connection.status
89
if self.status == "ACTIVE":
90
return connection
90
- if self.status in terminal_states:
91
+ if self.status in _TERMINAL_CONNECTION_STATES:
92
raise exceptions.SDKError(
93
message=(
94
f"Connection {self.id} entered terminal state "