From 6f0eb86c898c5d32e82e9bfa2b3bd8fff66143ce Mon Sep 17 00:00:00 2001 From: infinityplusone Date: Mon, 16 Nov 2026 10:42:27 -0500 Subject: [PATCH 1/2] Classify premium_daily and drop wait-time words from limit_type limit_type read the rate name from "exceeded for " but then substring-matched "daily" in it, so a per-feature premium_daily cap came back as "daily". The no-match fallback still treated "minute" as burst and "day" as daily, words that appear in the wait text itself. Map the rate name exactly (daily, burst, premium_daily; anything else is None), and have the fallback match only the whole words "daily" and "burst". Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01YNPQk6UN72ZCQV9Ck6kfu8 --- CHANGELOG.md | 5 +++++ tango/exceptions.py | 16 +++++++++++----- tests/test_client.py | 32 +++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ab914a..878b04e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- **`TangoRateLimitError.limit_type` reports a per-feature cap as `premium_daily`, not `daily`.** A 429 from a premium-gated endpoint (currently the CALC labor-rate endpoints) reads `Rate limit exceeded for premium_daily.`, and the SDK matched "daily" inside the rate's name, so callers couldn't tell a feature cap that blocks one endpoint from the account-wide daily lockout. The rate name now maps exactly to `daily`, `burst`, or `premium_daily`, and a name the SDK doesn't recognize returns `None`. +- **`limit_type` no longer guesses from the wait time when the detail names no rate.** The fallback still treated "minute" as burst and "day" as daily, and those words appear in the wait text itself (`Expected available in 3 minutes.`). It now matches only the words "daily" and "burst", and returns `None` otherwise. + ## [1.21.0] - 2026-10-29 ### Added diff --git a/tango/exceptions.py b/tango/exceptions.py index 52ccae7..36709ff 100644 --- a/tango/exceptions.py +++ b/tango/exceptions.py @@ -5,6 +5,7 @@ # The API names the tripped rate in its 429 detail: "Rate limit exceeded for daily. Please try again in 16 hours, 58 minutes." _RATE_NAME = re.compile(r"exceeded for ([\w-]+)", re.IGNORECASE) +_LIMIT_TYPES = {"daily": "daily", "burst": "burst", "premium_daily": "premium_daily"} class TangoAPIError(Exception): @@ -88,16 +89,21 @@ def detail(self) -> str | None: @property def limit_type(self) -> str | None: - """Which limit was hit: 'burst' or 'daily', parsed from detail.""" + """Which limit was hit, parsed from detail. + + ``"daily"`` or ``"burst"`` for the account's own caps, or ``"premium_daily"`` for a per-feature daily cap (currently the CALC labor-rate endpoints). ``None`` when the detail names no limit the SDK recognizes. + """ d = self.detail if not d: return None - # Read the rate's name, not the whole sentence: a daily lockout's wait reads "16 hours, 58 minutes", which a match on "minute" took for burst. match = _RATE_NAME.search(d) - text = match.group(1).lower() if match else d.lower() - if "daily" in text or re.search(r"\bday\b", text): + if match: + return _LIMIT_TYPES.get(match.group(1).lower()) + # Exact words only: "minute" and "day" also appear in the wait text ("9 hours, 14 minutes"), so they can't identify the limit. + lower = d.lower() + if re.search(r"\bdaily\b", lower): return "daily" - if "burst" in text or "minute" in text: + if re.search(r"\bburst\b", lower): return "burst" return None diff --git a/tests/test_client.py b/tests/test_client.py index c2e9384..b039e1f 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1507,9 +1507,23 @@ def test_429_daily_limit_error(self, mock_request): [ # The API's own wording, with its waits as convert_seconds_to_human_readable_wait_time renders them. ("Rate limit exceeded for daily. Please try again in 16 hours, 58 minutes.", "daily"), + ( + "Rate limit exceeded for daily. Please try again in 9 hours, 14 minutes, 3 seconds.", + "daily", + ), ("Rate limit exceeded for daily. Please try again in 1 minute.", "daily"), - ("Rate limit exceeded for burst. Please try again in 45 seconds.", "burst"), + ("Rate limit exceeded for burst. Please try again in 41 seconds.", "burst"), ("Rate limit exceeded for burst. Please try again in 1 minute.", "burst"), + ( + "Rate limit exceeded for premium_daily. Please try again in 9 hours, 14 minutes, 3 seconds.", + "premium_daily", + ), + ( + "Rate limit exceeded for daily. Please try again in 9 hours, 14 minutes, 3 seconds. " + "This limit is shared by everyone on your team; https://tango.makegov.com/account/team/ shows who to contact.", + "daily", + ), + ("Rate limit exceeded for monthly. Please try again in 3 days.", None), ("Too many requests.", None), ], ) @@ -1518,6 +1532,22 @@ def test_limit_type_reads_the_rate_name_not_the_wait(self, detail, expected): assert error.limit_type == expected + @pytest.mark.parametrize( + ("detail", "expected"), + [ + ("Daily request quota reached.", "daily"), + ("Burst limit reached.", "burst"), + ("Request was throttled. Expected available in 3 minutes.", None), + ("Request was throttled. Expected available in 1 day.", None), + ], + ) + def test_limit_type_without_a_rate_name_matches_only_the_words_daily_and_burst( + self, detail, expected + ): + error = TangoRateLimitError("Rate limit exceeded", 429, {"detail": detail}) + + assert error.limit_type == expected + @patch("tango.client.httpx.Client.request") def test_429_empty_body(self, mock_request): """Test 429 with no content body still works""" From 85706fc97a9cd6e2802f5d8fe66d47d4ff862620 Mon Sep 17 00:00:00 2001 From: infinityplusone Date: Mon, 16 Nov 2026 10:44:49 -0500 Subject: [PATCH 2/2] Tighten limit_type docstring and changelog to house style Single backticks in the docstring, wrapped under 150 chars, and the CHANGELOG fix collapsed to one bullet. Refs #108 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01YNPQk6UN72ZCQV9Ck6kfu8 --- CHANGELOG.md | 3 +-- tango/exceptions.py | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 878b04e..cb649b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- **`TangoRateLimitError.limit_type` reports a per-feature cap as `premium_daily`, not `daily`.** A 429 from a premium-gated endpoint (currently the CALC labor-rate endpoints) reads `Rate limit exceeded for premium_daily.`, and the SDK matched "daily" inside the rate's name, so callers couldn't tell a feature cap that blocks one endpoint from the account-wide daily lockout. The rate name now maps exactly to `daily`, `burst`, or `premium_daily`, and a name the SDK doesn't recognize returns `None`. -- **`limit_type` no longer guesses from the wait time when the detail names no rate.** The fallback still treated "minute" as burst and "day" as daily, and those words appear in the wait text itself (`Expected available in 3 minutes.`). It now matches only the words "daily" and "burst", and returns `None` otherwise. +- **`TangoRateLimitError.limit_type` returns `premium_daily` for a per-feature cap instead of `daily`.** A detail that names no rate is now classified only by the words "daily" or "burst", never by "minutes" or "day" in its wait time. ## [1.21.0] - 2026-10-29 diff --git a/tango/exceptions.py b/tango/exceptions.py index 36709ff..b5f74a9 100644 --- a/tango/exceptions.py +++ b/tango/exceptions.py @@ -91,7 +91,8 @@ def detail(self) -> str | None: def limit_type(self) -> str | None: """Which limit was hit, parsed from detail. - ``"daily"`` or ``"burst"`` for the account's own caps, or ``"premium_daily"`` for a per-feature daily cap (currently the CALC labor-rate endpoints). ``None`` when the detail names no limit the SDK recognizes. + `"daily"` or `"burst"` for the account's own caps, or `"premium_daily"` for a per-feature daily cap + (currently the CALC labor-rate endpoints). `None` when the detail names no limit the SDK recognizes. """ d = self.detail if not d: