Skip to content

Commit

Permalink
[p] Cover azul.health with mypy (#6821)
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes-ucsc committed Feb 3, 2025
1 parent 5cf898b commit 37a56e3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ modules =
azul.enums,
azul.es,
azul.exceptions,
azul.files
azul.files,
azul.health
packages =
azul.openapi

Expand Down
25 changes: 12 additions & 13 deletions src/azul/health.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from collections.abc import (
Iterable,
Mapping,
Set,
)
from itertools import (
chain,
Expand All @@ -12,7 +11,6 @@
import time
from typing import (
ClassVar,
Optional,
)

import attr
Expand All @@ -36,6 +34,7 @@
cache,
cached_property,
config,
json_bool,
lru_cache,
require,
)
Expand Down Expand Up @@ -65,7 +64,6 @@
)
from azul.types import (
JSON,
MutableJSON,
)

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -110,14 +108,13 @@ def basic_health(self) -> Response:
def health(self) -> Response:
return self._make_response(self._health.as_json(Health.all_keys))

def custom_health(self, keys: Optional[str]) -> Response:
def custom_health(self, keys: str | None) -> Response:
if keys is None:
body = self._health.as_json(Health.all_keys)
elif isinstance(keys, str):
assert keys # Chalice maps empty string to None
keys = keys.split(',')
try:
body = self._health.as_json(keys)
body = self._health.as_json(keys.split(','))
except AssertionError as e:
if R.caused(e):
body = {'Message': 'Invalid health keys'}
Expand Down Expand Up @@ -187,9 +184,9 @@ def lambda_name(self):
return self.controller.lambda_name

def as_json(self, keys: Iterable[str]) -> JSON:
keys = set(keys)
keys = frozenset(keys)
if keys:
require(keys.issubset(self.all_keys))
require(keys < self.all_keys)
else:
keys = self.all_keys
json = {k: getattr(self, k) for k in sorted(keys)}
Expand All @@ -201,13 +198,15 @@ def other_lambdas(self) -> JSON:
"""
Indicates whether the companion REST API responds to HTTP requests.
"""
response: MutableJSON = {
response = {
lambda_name: self._lambda(lambda_name)
for lambda_name in config.lambda_names()
if lambda_name != self.lambda_name
}
response['up'] = all(v['up'] for v in response.values())
return response
return {
'up': all(json_bool(v['up']) for v in response.values()),
**response
}

@health_property
def queues(self):
Expand Down Expand Up @@ -325,7 +324,7 @@ def as_json_fast(self) -> JSON:
p for p in locals().values() if isinstance(p, health_property)
)

all_keys: ClassVar[Set[str]] = frozenset(p.key for p in all_properties)
all_keys: ClassVar[frozenset[str]] = frozenset(p.key for p in all_properties)


class HealthApp(AzulChaliceApp):
Expand Down Expand Up @@ -504,7 +503,7 @@ def fast_health():
]
}
)
def custom_health(keys: Optional[str] = None):
def custom_health(keys: str | None = None):
return self.health_controller.custom_health(keys)

@self.metric_alarm(metric=LambdaMetric.errors,
Expand Down

0 comments on commit 37a56e3

Please sign in to comment.