From 182a566087872fdb31af0cbc10b368d6b9e47f1c Mon Sep 17 00:00:00 2001 From: vakarisz Date: Tue, 27 Sep 2022 11:33:30 +0300 Subject: [PATCH] Agent: Simplify scan_target_generator.py The responsibility of type-hints are not to implement logic. Implementing logic via type-hints diminishes readability, because it forces you to cross-reference a class instead of just exposing the logic where it's used --- .../network_scanning/scan_target_generator.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/monkey/infection_monkey/network_scanning/scan_target_generator.py b/monkey/infection_monkey/network_scanning/scan_target_generator.py index fa4034792..e1cd93c03 100644 --- a/monkey/infection_monkey/network_scanning/scan_target_generator.py +++ b/monkey/infection_monkey/network_scanning/scan_target_generator.py @@ -4,19 +4,12 @@ import socket from ipaddress import IPv4Interface from typing import Dict, Iterable, List, Optional, Sequence -from typing_extensions import Protocol, runtime_checkable - from common.network.network_range import InvalidNetworkRangeError, NetworkRange from infection_monkey.network import NetworkAddress logger = logging.getLogger(__name__) -@runtime_checkable -class HasDomain(Protocol): - domain_name: str - - def compile_scan_target_list( local_network_interfaces: Sequence[IPv4Interface], ranges_to_scan: Sequence[str], @@ -56,10 +49,11 @@ def _remove_redundant_targets(targets: Sequence[NetworkAddress]) -> List[Network def _range_to_addresses(range_obj: NetworkRange) -> List[NetworkAddress]: addresses = [] for address in range_obj: - if isinstance(range_obj, HasDomain): - addresses.append(NetworkAddress(address, range_obj.domain_name)) - else: - addresses.append(NetworkAddress(address, None)) + try: + domain = range_obj.domain_name # type: ignore + except AttributeError: + domain = None + addresses.append(NetworkAddress(address, domain)) return addresses