From e44e73cb8602cc6fb2f958e2915d21feb006dfae Mon Sep 17 00:00:00 2001 From: Lyz Date: Tue, 24 May 2022 11:07:33 +0200 Subject: [PATCH] fix: remove more traces of mypy-extensions from the code --- src/drode/adapters/aws.py | 29 ++++++++++++++++------------- src/drode/services.py | 2 +- src/drode/views.py | 8 +++----- tests/e2e/test_cli.py | 4 ++-- tests/fake_adapters.py | 8 ++++---- tests/unit/adapters/test_aws.py | 18 +++++++++--------- tests/unit/services/test_status.py | 22 +++++++++++----------- 7 files changed, 46 insertions(+), 45 deletions(-) diff --git a/src/drode/adapters/aws.py b/src/drode/adapters/aws.py index 256538d..fa10952 100644 --- a/src/drode/adapters/aws.py +++ b/src/drode/adapters/aws.py @@ -1,11 +1,11 @@ """Gather the integration with the AWS boto library.""" import logging +from dataclasses import dataclass from typing import Dict, List import boto3 from botocore.exceptions import ClientError, NoRegionError -from mypy_extensions import TypedDict log = logging.getLogger(__name__) @@ -19,9 +19,14 @@ class AWSStateError(Exception): InstanceInfo = Dict[str, str] -AutoscalerInfo = TypedDict( - "AutoscalerInfo", {"template": str, "instances": List[InstanceInfo]}, total=False -) + + +@dataclass +class AutoscalerInfo: + """Model the response of the AWS API regarding ASGs.""" + + instances: List[InstanceInfo] + template: str = "" class AWS: @@ -71,21 +76,19 @@ def get_autoscaling_group(autoscaling_name: str) -> AutoscalerInfo: ec2 = boto3.client("ec2") autoscaling = boto3.client("autoscaling") - autoscaler_info: AutoscalerInfo = { - "template": "", - "instances": [], - } + autoscaler_info = AutoscalerInfo( + template="", + instances=[], + ) try: autoscaling_group = autoscaling.describe_auto_scaling_groups( AutoScalingGroupNames=[autoscaling_name] )["AutoScalingGroups"][0] try: - autoscaler_info["template"] = autoscaling_group[ - "LaunchConfigurationName" - ] + autoscaler_info.template = autoscaling_group["LaunchConfigurationName"] except KeyError: - autoscaler_info["template"] = ( + autoscaler_info.template = ( f'{autoscaling_group["LaunchTemplate"]["LaunchTemplateName"][:35]}' f':{autoscaling_group["LaunchTemplate"]["Version"]}' ) @@ -107,7 +110,7 @@ def get_autoscaling_group(autoscaling_name: str) -> AutoscalerInfo: f':{instance_data["LaunchTemplate"]["Version"]}' ) - autoscaler_info["instances"].append( + autoscaler_info.instances.append( { "Instance": instance_data["InstanceId"], "IP": ec2_data["PrivateIpAddress"], diff --git a/src/drode/services.py b/src/drode/services.py index dc2c4bc..46c933a 100644 --- a/src/drode/services.py +++ b/src/drode/services.py @@ -185,7 +185,7 @@ def project_status(config: Config, aws: AWS) -> ProjectStatus: raise ConfigError("The autoscaler name is not a string") autoscaler_info = aws.get_autoscaling_group(autoscaler_name) except ConfigError: - autoscaler_info = {} + autoscaler_info = AutoscalerInfo(instances=[]) project[environment] = autoscaler_info diff --git a/src/drode/views.py b/src/drode/views.py index 53b0dc1..085991b 100644 --- a/src/drode/views.py +++ b/src/drode/views.py @@ -8,18 +8,16 @@ def print_autoscaling_group_info(autoscaler_info: AutoscalerInfo) -> None: """Print the information of the autoscaler information in table format.""" - print(f"Active Template: {autoscaler_info['template']}") + print(f"Active Template: {autoscaler_info.template}") print( - tabulate.tabulate( - autoscaler_info["instances"], headers="keys", tablefmt="simple" - ) + tabulate.tabulate(autoscaler_info.instances, headers="keys", tablefmt="simple") ) def print_status(project_status: ProjectStatus) -> None: """Print the project environment status.""" for environment, autoscaler_info in project_status.items(): - if autoscaler_info == {}: + if len(autoscaler_info.instances) == 0: continue print(f"# {environment}") print_autoscaling_group_info(autoscaler_info) diff --git a/tests/e2e/test_cli.py b/tests/e2e/test_cli.py index ec157cf..53bc56b 100644 --- a/tests/e2e/test_cli.py +++ b/tests/e2e/test_cli.py @@ -3,12 +3,12 @@ import logging import os import re +from typing import Any, Dict from unittest.mock import patch import pytest from _pytest.logging import LogCaptureFixture from click.testing import CliRunner -from mypy_extensions import TypedDict from py._path.local import LocalPath from tests.fake_adapters import FakeAWS, FakeDrone @@ -18,7 +18,7 @@ from ..factories import BuildInfoFactory -FakeDeps = TypedDict("FakeDeps", {"drone": FakeDrone, "aws": FakeAWS}) +FakeDeps = Dict[str, Any] @pytest.fixture(name="fake_dependencies") diff --git a/tests/fake_adapters.py b/tests/fake_adapters.py index 8f4f5d1..64c7390 100644 --- a/tests/fake_adapters.py +++ b/tests/fake_adapters.py @@ -205,9 +205,9 @@ def get_autoscaling_group(autoscaling_name: str) -> AutoscalerInfo: Raises: AWSStateError: If no autoscaling groups are found with that name. """ - return { - "template": "launch-config-name", - "instances": [ + return AutoscalerInfo( + template="launch-config-name", + instances=[ { "Instance": "i-xxxxxxxxxxxxxxxxx", "IP": "192.168.1.13", @@ -216,4 +216,4 @@ def get_autoscaling_group(autoscaling_name: str) -> AutoscalerInfo: "Template": "old-launch-config-name", } ], - } + ) diff --git a/tests/unit/adapters/test_aws.py b/tests/unit/adapters/test_aws.py index 09fcb23..6e87919 100644 --- a/tests/unit/adapters/test_aws.py +++ b/tests/unit/adapters/test_aws.py @@ -9,7 +9,7 @@ from _pytest.logging import LogCaptureFixture from botocore.exceptions import NoRegionError -from drode.adapters.aws import AWS, AWSConfigurationError, AWSStateError +from drode.adapters.aws import AWS, AutoscalerInfo, AWSConfigurationError, AWSStateError @pytest.fixture(name="boto") @@ -118,9 +118,9 @@ def test_get_autoscaling_returns_instances_info(aws: AWS, boto: Mock) -> None: ], "ResponseMetadata": {"HTTPStatusCode": 200}, } - desired_result = { - "template": "launch-config-name", - "instances": [ + desired_result = AutoscalerInfo( + template="launch-config-name", + instances=[ { "Instance": "i-xxxxxxxxxxxxxxxxx", "IP": "192.168.1.13", @@ -129,7 +129,7 @@ def test_get_autoscaling_returns_instances_info(aws: AWS, boto: Mock) -> None: "Template": "old-launch-config-name", } ], - } + ) result = aws.get_autoscaling_group("production_autoscaling_group_name") @@ -223,9 +223,9 @@ def test_get_autoscaling_handles_launch_templates(aws: AWS, boto: Mock) -> None: ], "ResponseMetadata": {"HTTPStatusCode": 200}, } - desired_result = { - "template": "launch-template-name:1", - "instances": [ + desired_result = AutoscalerInfo( + template="launch-template-name:1", + instances=[ { "Instance": "i-xxxxxxxxxxxxxxxxx", "IP": "192.168.1.13", @@ -234,7 +234,7 @@ def test_get_autoscaling_handles_launch_templates(aws: AWS, boto: Mock) -> None: "Template": "old-launch-template-name:1", } ], - } + ) result = aws.get_autoscaling_group("production_autoscaling_group_name") diff --git a/tests/unit/services/test_status.py b/tests/unit/services/test_status.py index a055ab9..4974d5b 100644 --- a/tests/unit/services/test_status.py +++ b/tests/unit/services/test_status.py @@ -3,7 +3,7 @@ import pytest from drode import services -from drode.adapters.aws import AWS +from drode.adapters.aws import AWS, AutoscalerInfo from drode.config import Config, ConfigError @@ -16,9 +16,9 @@ def test_project_status_happy_path(aws: AWS, config: Config) -> None: result = services.project_status(config, aws) assert result == { - "Production": { - "template": "launch-config-name", - "instances": [ + "Production": AutoscalerInfo( + template="launch-config-name", + instances=[ { "Instance": "i-xxxxxxxxxxxxxxxxx", "IP": "192.168.1.13", @@ -27,10 +27,10 @@ def test_project_status_happy_path(aws: AWS, config: Config) -> None: "Template": "old-launch-config-name", } ], - }, - "Staging": { - "template": "launch-config-name", - "instances": [ + ), + "Staging": AutoscalerInfo( + template="launch-config-name", + instances=[ { "Instance": "i-xxxxxxxxxxxxxxxxx", "IP": "192.168.1.13", @@ -39,7 +39,7 @@ def test_project_status_happy_path(aws: AWS, config: Config) -> None: "Template": "old-launch-config-name", } ], - }, + ), } @@ -56,8 +56,8 @@ def test_project_status_works_for_undefined_autoscaling_groups( result = services.project_status(config, aws) assert result == { - "Production": {}, - "Staging": {}, + "Production": AutoscalerInfo(instances=[]), + "Staging": AutoscalerInfo(instances=[]), }