Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- **`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

### Added
Expand Down
17 changes: 12 additions & 5 deletions tango/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -88,16 +89,22 @@ 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

Expand Down
32 changes: 31 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],
)
Expand All @@ -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"""
Expand Down
Loading