snake_casefunctions, methods, and options.PascalCaseclasses.UPPER_SNAKEmodule constants (RESERVED_PARAMS,TOKEN_EXCHANGE_GRANT_TYPE).- Private helpers carry a leading underscore (
_discover,_fetch_jwks,_prepare_error) and sit together after the public methods ofApiClient. The one pre-existing exception is_resolve_allowed_domains, aboveverify_request. Leave it there. - Error classes end in
Errorand live inerrors.py. - Type hints use builtin generics (
dict[str, Any],list[str]) andX | Noneunions.Optionalremains only for the quoted forward references inconfig.py. ruffUPtarget-versionis kept equal to thepyproject.tomlPython floor.
Every error subclasses BaseAuthError. A new error category defines its HTTP mapping. A specialization of an existing category (MissingOrganizationError, OrganizationNotAllowedError under VerifyAccessTokenError) inherits it:
class DomainsResolverError(BaseAuthError):
"""Error raised when domains resolver function fails."""
def get_status_code(self) -> int:
return 500
def get_error_code(self) -> str:
return "domains_resolver_error"Wrap third-party failures in a typed error and chain the cause:
# Good (api_client.py, _resolve_allowed_domains)
try:
result = self._allowed_domains(context)
if asyncio.iscoroutine(result) or asyncio.isfuture(result):
result = await result
except Exception as e:
raise DomainsResolverError(
f"Domains resolver function failed: {str(e)}"
) from eInterpolating str(e) is acceptable here because the resolver is integrator code. Do not interpolate exception text that can carry token contents or a response body.
# Bad
def get_token(self, options):
client = httpx.Client()
resp = client.post(url, data=options)
if resp.status_code != 200:
print(resp.text)
return None
return resp.json()The snippet has six violations, in order. It is sync and untyped in an async SDK, the client has no options.timeout, the OAuth parameters are unfiltered (no RESERVED_PARAMS check), it prints a possibly token-bearing body, it returns a None a caller can mistake for success, and it lets an unvalidated response into the public API.
Public errors meant for integrators are exported from __init__.py (ApiError, AuthServerUnavailableError, ConfigurationError, DomainsResolverError, GetAccessTokenForConnectionError, GetClientCredentialsTokenError, GetTokenByExchangeProfileError, MissingOrganizationError, OrganizationNotAllowedError, VerifyAccessTokenError). Others are imported from auth0_api_python.errors.
- Options object: all configuration goes through
ApiClientOptions.ApiClient.__init__raisesConfigurationErrororMissingRequiredArgumentErrorat construction foraudience, thedomain/domainscombination,client_idwithoutdomain, the cache settings,organization_policy/organization_id,scope_matching, andclient_info. Not validated there:timeout,dpop_iat_leeway,dpop_iat_offset, and the format of a singledomain. A bad entry in adomainslist raises a plainValueErrorfromnormalize_domain. - Async I/O: network calls use
httpx.AsyncClient, or the integrator'scustom_fetchwhen provided. Public verification and token methods areasync def. - Pluggable cache:
CacheAdapterABC withget/set/delete/clear.InMemoryCacheis the default. Entry TTL ismin(Cache-Control max-age, cache_ttl_seconds)when the discovery/JWKS response sendsmax-age. - Token store:
- Interfaces.
AbstractTokenStoreABC withget/set/deleteplusencrypt/decrypt, separate fromCacheAdapter.IndexedTokenStoreaddsadd_index_member/list_index_membersforscope_matching="non_strict". - Encryption helpers. They are opt-in. The SDK never calls them, so a store encrypts in its own
get/set. - Internals. Cache classes and key builders live in
_internal/and are not public. - Failures. A store
getfailure is logged and treated as a miss. Asetfailure is logged and the fresh token is still returned. A malformed entry from a custom store can still raise, sinceexpires_atreachesis_freshoutside thetryin the client credentials and OBO paths, while the Token Vault cache checks its type first.
- Interfaces.
- Docstrings: Google style with
Args:/Returns:/Raises:sections on public methods.verify_dpop_proofpredates this convention and uses a numbered list instead. - Placement:
api_client.pyhas the banner# ===== Private Methods =====, and_internal/obo_cache.pyhas a lowercase# ===== Private methods =====. Inapi_client.py, new public methods go above the banner after the existing ones, and new helpers below it. In files without a banner, put new code next to related code or at the end, never at an arbitrary position.
Enforced on every change (code, tests, docs, commit messages):
- Comments earn their place. Only a non-obvious why: a protocol citation (
# RFC 9449), a security decision, an intentional omission. Never restate the line below. Default to one sharp line or none. - Docstring format. One-line description, then
Args, thenReturns/Raises. No design rationale. That belongs in the PR description. - Plain hyphens only. No em or en dashes in comments, docstrings, commit messages, or Markdown.
- No semicolon clause-splicing. Split independent clauses into separate sentences.
- No internal references. No ticket numbers, planning-doc paths, internal links, or references to other SDKs as a comparison or justification. PR bodies follow the PR template instead, whose References section is where tickets and links go.
- Helpers stay grouped. Never interleave a private helper between public methods.