This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
- The data returned from Metrc collection API endpoints may slightly change over time. Extra fields may be added. Don't write python classes that assume an exact data shape, they should fall back gracefully. In 2.0, rows are plain dicts exactly as the API returns them; never rename, drop or validate fields.
- What the library knows about endpoints comes from the T3 OpenAPI spec at runtime. Don't hardcode endpoint lists, ID parameters or per-endpoint behaviour; derive it from the spec (see
t3api_utils/spec/registry.py) so new endpoints work without a release. V2_DESIGN.mdis the design. Where the code and the design differ in small ways, the code wins; keep the docs indocs/matching the code.
This project uses uv. Python 3.12 or later.
uv sync # create .venv with the dev group (includes textual)
uv run pytest # all tests; coverage must stay at 100%
uv run pytest --cov --cov-branch --cov-report=term-missing
uv run pytest tests/test_collect.py # one file
uv run mypy # strict; checks t3api_utils, tests and scripts (see pyproject)
uv run mkdocs serve # docs preview at http://127.0.0.1:8000
uv run mkdocs build --strict # what CI runs; must pass with no warnings
T3_SPEC=bundled uv run t3api endpoints harvest # try the CLI offline against the bundled specBefore each release:
uv run python scripts/update_spec.py # refresh t3api_utils/spec/openapi.json.gz from the live spec
T3_SMOKE_SECRET_KEY=... uv run pytest -m sandboxupdate_spec.py prints how the endpoint-kind counts changed; mention notable changes in CHANGELOG.md. CI (.github/workflows/ci.yml) runs mypy and pytest on Linux, macOS and Windows with Python 3.12 and 3.13, plus a strict docs build. .github/workflows/release.yml runs when a vX.Y.Z tag is pushed: it uploads to PyPI through trusted publishing, then creates the GitHub release from that version's CHANGELOG.md section.
For this repository (t3api-python-utils) only, Claude may merge to master, push, tag and publish a release without asking first, as long as the tests fully pass:
uv run pytest --covpasses at 100% coverage;uv run mypyis clean;uv run mkdocs build --strictpasses;- CI is green on every OS for the commit being released.
If any of these fail, stop and report instead of releasing.
Steps:
- Bump
versioninpyproject.tomland add a## X.Y.Zsection toCHANGELOG.md. - Run the checklist above.
- Push the branch and wait for CI.
- Fast-forward
masterto the release commit and push it. git tag -a vX.Y.Z -m "t3api-utils X.Y.Z"on that commit, thengit push origin vX.Y.Z.- Check that every
release.ymljob succeeds. The jobs publish to PyPI, create the GitHub release and deploy the docs site. If only the docs job fails, deploy from the tagged commit withuv run mkdocs gh-deploy --strict.
A release isn't finished until the docs site at https://classvsoftware.github.io/t3api-python-utils/ shows the released version.
t3api_utils/
__init__.py public API: connect, T3Client, inspect, filter_rows, to_csv, to_json, read_rows,
pick_file, confirm, ask, choose, errors, WriteResult/BulkResult/RowResult, TagList,
Endpoint, auth classes
client.py T3Client: request(), get/post/put/patch/delete(), download(), collect(), iter_rows(),
bulk(), report(), report_url(), whoami(), licenses(), sandbox state, pickers, namespaces
errors.py exception hierarchy; error_from_response() maps status codes to classes
writes.py WriteResult, BulkResult, RowResult, bulk()
reports.py report(), report_url()
tag_lists.py TagLists (client.tag_lists) and TagList
local.py filter_rows(), to_csv(), to_json(), read_rows(), flatten()
pickers.py choose(), pick_license(), pick_endpoint(), pick_file() as Rich tables
prompts.py public confirm() (default no) and ask(parse=...)
_http.py httpx2 client builder, RetryTransport, HTTP log redaction
_ratelimit.py thread-safe token bucket
_api.py send/parse helpers that raise APIError subclasses; atomic streaming download()
_console.py Rich consoles, purple theme, message/prompt/progress helpers (internal)
_paths.py platformdirs config and cache folders (tests patch these)
spec/
loader.py cached live spec -> live fetch -> stale cache -> bundled copy
registry.py Endpoint, Param, Registry: classification, lookup, parameter checks
openapi.json.gz bundled spec
auth/
credentials.py SecretKeyAuth, BearerAuth (httpx2.Auth), JWT expiry and OTP helpers
resolve.py connect(): argument -> env var -> profile -> login flow -> AuthRequired
login.py interactive login flow and secret-key offer
store.py Profile, Store: config.toml plus OS keyring (secrets.toml fallback)
secret_keys.py SecretKeys (client.secret_keys): list/create/rotate/delete
states.py GET /v2/states (requiresEmail/requiresOtp), cached, with 1.x fallback lists
tui/inspector.py Textual inspector, imported lazily by t3.inspect()
cli/main.py Typer app behind the `t3api` command
scripts/ example.py, discontinue-items.py (user-facing examples), update_spec.py
docs/ mkdocs-material site; reference pages are mkdocstrings
- Importing the package has no side effects: no files written, no logging configured, no network calls. The spec loads the first time something needs it.
- Library code never calls
sys.exit()ortyper.Exit. Onlycli/does. - Modules whose names start with
_are private. Anything a user needs is re-exported fromt3api_utils/__init__.py. - Every exception raised on purpose derives from
T3Error(errors.py). EveryAPIErrorkeeps itsresponse. - The library logs to
logging.getLogger("t3api_utils")and never adds handlers or touches the root logger, exceptenable_http_logging()when HTTP logging is explicitly requested. - Prompts and pickers work only when
_console.is_interactive()(a terminal or Jupyter); otherwise they raiseNotInteractive. Every interactive helper must have a non-interactive form.connect()must never hang waiting for input. - Sync only. Parallel collection paging uses a thread pool behind the client's one shared rate limiter.
- Optional dependencies (Textual) are imported inside the function that needs them, with an
ImportErrorthat names the extra. - No DuckDB, pyarrow,
.t3.envorpython-dotenv. Export is CSV/JSON only.
- Endpoint kinds come from spec structure alone (
registry.classify): collection, report, metrc_write, immediate_write, file, object, other. Tests check named endpoints' kinds, not counts. - Writes:
metrc_writeroutes are a dry run unlesssubmit=True;immediate_writeroutes rejectsubmit=entirely. A dry run is static validation only (format, required fields, types), never call it more than that.bulk()validates every row before submitting; row statuses are valid/invalid/submitted/failed/not_sent.submit="ask"raisesNotInteractivewithout a terminal before sending anything. - Body shape:
Endpoint.body_typecomes from the spec. Most Metrc write routes take a JSON list;bulk()wraps each row in a list for them, and single writes warn when thejson=shape doesn't match. - Retries (
_http.RetryTransport): GET/HEAD/OPTIONS retry on connection errors and 429/502/503/504; writes retry only on 429 and pre-send connection failures. Never retry a write on 5xx or a post-send timeout. Max 3 attempts. - Credentials: secret keys go in the
X-T3-API-Keyheader, never the query string, exceptreport_url(). The Metrc password is never stored. - Redaction: HTTP debug logs mask auth headers,
/v2/auth/secretkey/{key}paths,secretKey=queries, andpassword/otp/otpSeed/secretKey/accessToken/refreshTokenbody fields. - Default license (
client.license) is applied only to endpoints whoselicenseNumberis required. An explicitlicense=always wins. - Sandbox:
connect(sandbox=True)/T3_SANDBOXignoresT3_LICENSEand the profile's license,enter_sandbox()clearsclient.license, and explicit licenses are checked against the session's (the sandbox returns an empty 200 for other licenses). The CLI's--sandboxapplies to data commands only, never tologin,logout,keysorlicense.
-
Docstrings are Google style. They're rendered by mkdocstrings on the
docs/reference/pages;docs/_ext/griffe_roles.pyturns Sphinx roles like:func:into inline code at build time. -
The documentation pages must stay up to date. Any change to public behaviour, the CLI, configuration or install steps updates, in the same commit:
- the matching
docs/guide/page; - the docstrings that feed
docs/reference/; README.mdandCHANGELOG.mdwhere they cover it.
uv run mkdocs build --strictmust pass. - the matching
-
The published site is redeployed by
release.ymlon every release (see Releasing). A docs-only fix between releases is deployed withuv run mkdocs gh-deploy --strict, but only if the docs still describe the released version.
- Coverage is 100%, line and branch, enforced:
fail_under = 100andbranch = trueare set inpyproject.toml, souv run pytest --covfails below that. Use# pragma: no coveronly for code that can't run in tests (and say why). - HTTP is mocked with
httpx2.MockTransportthrough theRouterfixture intests/conftest.py:router.add("GET", "/v2/licenses", json=[...])answers a route (paths may contain{placeholders}; later routes win; unmatched requests get 404), androuter.calls/router.requests(...)record what was sent.paginated(rows)serves rows as a T3 paginated collection. Theclientfixture is aT3Clientwith a secret key, the bundled spec, no rate limit and the router's transport. - Autouse fixtures isolate every test: config and cache folders in a temp dir, no
T3_*environment variables, an in-memory keyring, a temp working directory, no terminal (_console.is_interactive()is False unless a test patches it), andtime.sleepreplaced with a recorder so retries don't wait. filterwarnings = ["error"]: a warning fails the test.- Spec tests use the bundled spec and assert the kind of specific named endpoints, not totals.
- Retry tests must check that a POST answered with 400 or 500 is sent exactly once.
- Sandbox smoke tests (
tests/test_sandbox.py, markersandbox) run against the real T3 API and the Metrc sandbox. They're skipped unlessT3_SMOKE_SECRET_KEYis set (T3_SMOKE_API_HOSToptionally points at another host):T3_SMOKE_SECRET_KEY=... uv run pytest -m sandbox. Run them before each release. - mypy is strict for
t3api_utils,testsandscripts.
Primary Purple Theme:
primary-#8B5CF6(Violet-500) - Headers, titles, primary actionsaccent-#A855F7(Purple-500) - Highlights, emphasis, interactive elementsmuted-#C4B5FD(Violet-300) - Secondary text, subdued informationbright-#DDD6FE(Violet-200) - Backgrounds, subtle highlights
Status Colors:
success-#10B981(Emerald-500) - Success messages, completed actionserror-#EF4444(Red-500) - Error messages, failureswarning-#F59E0B(Amber-500) - Warnings, cautionsinfo-#6366F1(Indigo-500) - Information, help text
These are defined as the Rich theme in t3api_utils/_console.py (THEME).
Message Types (use the helpers in t3api_utils/_console.py, which print exactly these):
from t3api_utils import _console
_console.success("Operation completed successfully") # [bold green]✓[/bold green] ...
_console.error(f"Operation failed: {error_message}") # [bold red]✗[/bold red] ...
_console.warning(f"Warning: {warning_message}") # [bold yellow]⚠[/bold yellow] ...
_console.info(info_message) # [bold blue]ℹ[/bold blue] ...
_console.progress(status_message) # [bold magenta]⋯[/bold magenta] ...Headers and Structure:
# Main headers, section headers, sub-headers
_console.header(title) # [bold magenta]═══ {title} ═══[/bold magenta]
console.print("[bold bright_magenta]── {section_name} ──[/bold bright_magenta]")
console.print("[magenta]{sub_header}:[/magenta]")Interactive Elements:
# Tables, prompts, file paths
table = _console.table("Title") # Table(border_style="magenta", header_style="bold magenta")
value = _console.ask("Enter your choice") # Rich prompt, raises NotInteractive without a terminal
ok = _console.confirm("Continue?", default=False)
console.print(f"[cyan]{file_path}[/cyan]")Centralized Styling:
- Use
t3api_utils/_console.pyfor all styling, prompts and progress bars:from t3api_utils._console import console, out. consolewrites to stderr (messages, prompts, tables meant for a person).outwrites to stdout and is only for data a user might pipe or capture (e.g.t3api whoamitables, printed IDs and URLs).- Never use plain
print()in library or CLI code - always use the Rich consoles. (The user-facing example scripts inscripts/may useprint().) - Progress bars go through
_console.progress_bar(label, total, enabled), which is transient and disabled when stderr isn't a terminal.
Consistency Rules:
- All success messages use green with checkmark:
[bold green]✓[/bold green] - All error messages use red with X mark:
[bold red]✗[/bold red] - All progress/status use purple with dots:
[bold magenta]⋯[/bold magenta] - All headers use magenta with decorative borders
Accessibility:
- Always include symbols (✓, ✗, ⚠, ℹ, ⋯) alongside colors
- Use
boldfor important messages to improve readability - Maintain good contrast ratios
All selection interfaces use consistent Rich Table formatting. Build new pickers on choose() in t3api_utils/pickers.py rather than writing the loop again.
Standard Implementation:
from t3api_utils.pickers import choose
def pick_something(items: Sequence[SomeType]) -> SomeType:
"""Standard picker implementation."""
return choose(
"Available Items",
items,
["Name", "Details"], # columns after the "#" column
lambda item: [item.name, item.details], # one string per column
filterable=None, # default: typing text narrows lists of more than 10
)choose() produces:
- A Rich Table from
_console.table()(magenta border, bold magenta header) - A numbered selection column with
style="magenta", justify="right" - The primary content column with
style="bright_white" - Further detail columns with
style="cyan"
Requirements:
- Always validate the user's input range (re-prompt on a bad number).
- Call
_console.require_interactive()first, so a picker without a terminal raisesNotInteractiveinstead of hanging. - Let
KeyboardInterruptpropagate to the caller. Library code never exits; the CLI'shandle_errorsturns Ctrl-C into "Cancelled." and exit code 130.
Examples in Codebase:
choose(),pick_license(),pick_endpoint()andpick_file()int3api_utils/pickers.py- The public
t3.confirm()andt3.ask()int3api_utils/prompts.py T3Client.pick_license()andT3Client.pick_endpoint()int3api_utils/client.py- The failed-rows table in
writes._show_invalid()and the CLI tables int3api_utils/cli/main.py