|
14 | 14 | from huggingface_hub.constants import ENDPOINT |
15 | 15 | from huggingface_hub.errors import HfHubHTTPError, OfflineModeIsEnabled |
16 | 16 | from huggingface_hub.utils._http import ( |
| 17 | + _WARNED_TOPICS, |
17 | 18 | RateLimitInfo, |
18 | 19 | _adjust_range_header, |
| 20 | + _warn_on_warning_headers, |
19 | 21 | default_client_factory, |
20 | 22 | fix_hf_endpoint_in_url, |
21 | 23 | get_async_session, |
@@ -560,3 +562,45 @@ def test_429_without_ratelimit_headers(self): |
560 | 562 |
|
561 | 563 | assert "429 Too Many Requests" in str(exc_info.value) |
562 | 564 | assert "api/models" in str(exc_info.value) |
| 565 | + |
| 566 | + |
| 567 | +class TestWarnOnWarningHeaders: |
| 568 | + def test_warn_on_warning_headers(self, caplog): |
| 569 | + # Request #1 (multiple warnings) |
| 570 | + response = Mock(spec=httpx.Response) |
| 571 | + response.headers = httpx.Headers( |
| 572 | + [ |
| 573 | + ("X-HF-Warning", "Topic1; This is the first warning message."), |
| 574 | + ("X-HF-Warning", "Topic2; This is the second warning message."), |
| 575 | + ("X-HF-Warning", "Topic1; This is a repeated warning message for Topic1."), |
| 576 | + ("X-HF-Warning", "This is a warning without a topic."), |
| 577 | + ("X-HF-Warning", "This is another warning without a topic."), |
| 578 | + ] |
| 579 | + ) |
| 580 | + |
| 581 | + with caplog.at_level("WARNING"): |
| 582 | + _warn_on_warning_headers(response) |
| 583 | + |
| 584 | + assert _WARNED_TOPICS == {"Topic1", "Topic2", ""} |
| 585 | + warnings = [record.message for record in caplog.records if record.levelname == "WARNING"] |
| 586 | + assert "WARNING: This is the first warning message." in warnings |
| 587 | + assert "WARNING: This is the second warning message." in warnings |
| 588 | + assert "WARNING: This is a repeated warning message for Topic1." not in warnings |
| 589 | + assert "WARNING: This is a warning without a topic." in warnings |
| 590 | + assert "WARNING: This is another warning without a topic." not in warnings |
| 591 | + # Request #2 (exact same warnings, should not warn again) |
| 592 | + caplog.clear() |
| 593 | + with caplog.at_level("WARNING"): |
| 594 | + _warn_on_warning_headers(response) |
| 595 | + warnings = [record.message for record in caplog.records if record.levelname == "WARNING"] |
| 596 | + assert len(warnings) == 0 # No new warnings should be added |
| 597 | + |
| 598 | + # Request #3 (single warning with new topic, should warn) |
| 599 | + response.headers = httpx.Headers({"X-HF-Warning": "Topic4; Another warning."}) |
| 600 | + caplog.clear() |
| 601 | + with caplog.at_level("WARNING"): |
| 602 | + _warn_on_warning_headers(response) |
| 603 | + warnings = [record.message for record in caplog.records if record.levelname == "WARNING"] |
| 604 | + assert len(warnings) == 1 |
| 605 | + assert warnings == ["WARNING: Another warning."] |
| 606 | + assert "Topic4" in _WARNED_TOPICS |
0 commit comments