Skip to content

Commit

Permalink
Add MyPy checks to Controller function
Browse files Browse the repository at this point in the history
  • Loading branch information
Iain-S committed Feb 6, 2024
1 parent 1d013c6 commit fd8aa56
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 6 deletions.
3 changes: 2 additions & 1 deletion controller_function/controller/logutils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Utils to send logs to Azure Application Insights."""
import logging
from typing import Optional

from opencensus.ext.azure.log_exporter import AzureLogHandler

Expand All @@ -9,7 +10,7 @@
class CustomDimensionsFilter(logging.Filter):
"""Add application-wide properties to AzureLogHandler records."""

def __init__(self, custom_dimensions: dict = None) -> None:
def __init__(self, custom_dimensions: Optional[dict] = None) -> None:
"""Add custom dimensions, if provided, to the log record."""
super().__init__()
self.custom_dimensions = custom_dimensions or {}
Expand Down
2 changes: 1 addition & 1 deletion controller_function/controller/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ def correct_start_and_end(cls, v): # pylint: disable=no-self-argument
@lru_cache()
def get_settings() -> Settings:
"""Get the global app settings object."""
return Settings()
return Settings() # type: ignore
12 changes: 10 additions & 2 deletions controller_function/controller/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ def enable_subscription(subscription_id: UUID) -> None:
try:
SUBSCRIPTION_CLIENT.subscription.enable(str(subscription_id))
except azure_exceptions.HttpResponseError as e:
is_enabled = "not in suspended state" in e.error.message
is_enabled = (
("not in suspended state" in e.error.message)
if (e.error and e.error.message)
else False
)

# It's fine if we can't enable it because it is already active.
if is_enabled:
Expand All @@ -70,7 +74,11 @@ def disable_subscription(subscription_id: UUID) -> None:
try:
SUBSCRIPTION_CLIENT.subscription.cancel(str(subscription_id))
except azure_exceptions.HttpResponseError as e:
is_disabled = "Subscription is not in active state" in e.error.message
is_disabled = (
("Subscription is not in active state" in e.error.message)
if (e.error and e.error.message)
else False
)

# It's fine if we can't disable it because it is already inactive.
if is_disabled:
Expand Down
65 changes: 63 additions & 2 deletions controller_function/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions controller_function/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pytest = "^6.2.5"
pylint = "^2.17.4"
pylint-absolute-imports = "^1.0.1"
isort = "^5.12.0"
mypy = "^1.8.0"
types-requests = "^2.31.0.20240125"

[build-system]
requires = ["poetry-core"]
Expand Down
5 changes: 5 additions & 0 deletions controller_function/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ status=$((status+$?))
python -m coverage report --show-missing
status=$((status+$?))

# Check types with MyPy
echo "Running type checking..."
python -m mypy --config-file tests/mypy.ini controller/ tests/
status=$((status+$?))

# [optional] Check Markdown coding style with Ruby's markdown lint
# https://github.com/markdownlint/markdownlint
if ! command -v mdl &> /dev/null
Expand Down
2 changes: 2 additions & 0 deletions controller_function/tests/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[mypy]
ignore_missing_imports = True

0 comments on commit fd8aa56

Please sign in to comment.