Skip to content

Commit 7d5015a

Browse files
committed
Fallback for modern Python syntax
1 parent 0ea9af9 commit 7d5015a

File tree

5 files changed

+32
-35
lines changed

5 files changed

+32
-35
lines changed

health_check/contrib/atlassian.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,24 @@ class GitHub(AtlassianStatusPage):
123123
124124
"""
125125

126-
class EnterpriseRegion(enum.StrEnum):
127-
"""GitHub Enterprise status page regions."""
128-
129-
australia = "au"
130-
"""Australia."""
131-
eu = "eu"
132-
"""Europe."""
133-
japan = "jp"
134-
"""Japan."""
135-
us = "us"
136-
"""United States."""
137-
138-
enterprise_region: EnterpriseRegion | None = None
126+
try:
127+
128+
class EnterpriseRegion(enum.StrEnum):
129+
"""GitHub Enterprise status page regions."""
130+
131+
australia = "au"
132+
"""Australia."""
133+
eu = "eu"
134+
"""Europe."""
135+
japan = "jp"
136+
"""Japan."""
137+
us = "us"
138+
"""United States."""
139+
140+
enterprise_region: EnterpriseRegion | None = None
141+
except AttributeError:
142+
# Python <3.11 doesn't have StrEnum, so fall back to a simple string field with validation
143+
enterprise_region: str | None = None
139144
timeout: datetime.timedelta = dataclasses.field(
140145
default=datetime.timedelta(seconds=10), repr=False
141146
)

health_check/contrib/celery.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import dataclasses
44
import datetime
55
import typing
6-
from types import MappingProxyType
76

87
import celery
98
from celery.app import app_or_default
@@ -26,9 +25,7 @@ class Ping(HealthCheck):
2625
2726
"""
2827

29-
CORRECT_PING_RESPONSE: typing.Final[dict[str, str]] = MappingProxyType(
30-
{"ok": "pong"}
31-
)
28+
CORRECT_PING_RESPONSE: typing.ClassVar[dict[str, str]] = {"ok": "pong"}
3229
app: celery.Celery = dataclasses.field(default_factory=app_or_default)
3330
timeout: datetime.timedelta = dataclasses.field(
3431
default=datetime.timedelta(seconds=1), repr=False

health_check/contrib/rss.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,15 @@ async def run(self):
8989
logger.debug("No entries found in feed")
9090
return
9191

92-
incidents = [entry for entry in feed.entries if self._is_recent_incident(entry)]
93-
94-
if incidents:
92+
if incidents := [
93+
entry for entry in feed.entries if self._is_recent_incident(entry)
94+
]:
9595
raise ServiceWarning(
96-
f"Found {len(incidents)} recent incident(s): {
97-
', '.join(
98-
getattr(entry, 'title', 'Untitled incident')
99-
or 'Untitled incident'
100-
for entry in incidents
101-
)
102-
}"
96+
"\n".join(
97+
f"{getattr(entry, 'title', 'Unknown Incident') or 'Unknown Incident'}:"
98+
f" {getattr(entry, 'link', self.feed_url) or self.feed_url}"
99+
for entry in incidents
100+
)
103101
)
104102

105103
logger.debug("No recent incidents found in feed")

tests/contrib/test_atlassian.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,7 @@ async def test_check_status__ok(self):
234234
def test_base_url_format(self):
235235
"""Verify correct base URL for GitHub."""
236236
assert GitHub().base_url == "https://www.githubstatus.com"
237-
assert (
238-
GitHub(GitHub.EnterpriseRegion.eu).base_url == "https://eu.githubstatus.com"
239-
)
237+
assert GitHub("eu").base_url == "https://eu.githubstatus.com"
240238

241239

242240
class TestCloudflare:

tests/contrib/test_rss.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ async def test_check_status__detects_any_entry_as_incident(self):
5858
result = await check.get_result()
5959
assert result.error is not None
6060
assert isinstance(result.error, ServiceWarning)
61-
assert "1 recent incident(s)" in str(result.error)
6261
assert "Service is operating normally" in str(result.error)
6362

6463
@pytest.mark.asyncio
@@ -103,7 +102,7 @@ async def test_check_status__multiple_incidents(self):
103102
result = await check.get_result()
104103
assert result.error is not None
105104
assert isinstance(result.error, ServiceWarning)
106-
assert "2 recent incident(s)" in str(result.error)
105+
assert "\n" in str(result.error)
107106

108107
@pytest.mark.asyncio
109108
async def test_check_status__no_recent_incidents(self):
@@ -327,7 +326,7 @@ async def test_extract_title__entry_without_title(self):
327326
result = await check.get_result()
328327
assert result.error is not None
329328
assert isinstance(result.error, ServiceWarning)
330-
assert "Untitled incident" in str(result.error)
329+
assert "Unknown Incident" in str(result.error)
331330

332331
def test_feed_url_format(self):
333332
"""Verify correct feed URL format for AWS."""
@@ -796,7 +795,7 @@ async def test_check_status__atom_entry_with_empty_title(self):
796795
result = await check.get_result()
797796
assert result.error is not None
798797
assert isinstance(result.error, ServiceWarning)
799-
assert "Untitled incident" in str(result.error)
798+
assert "Unknown Incident:" in str(result.error)
800799

801800
@pytest.mark.asyncio
802801
async def test_check_status__atom_entry_without_title(self):
@@ -833,4 +832,4 @@ async def test_check_status__atom_entry_without_title(self):
833832
result = await check.get_result()
834833
assert result.error is not None
835834
assert isinstance(result.error, ServiceWarning)
836-
assert "Untitled incident" in str(result.error)
835+
assert "Unknown Incident:" in str(result.error)

0 commit comments

Comments
 (0)