diff --git a/kubemarine/admission.py b/kubemarine/admission.py index 42897c116..8fde5dd10 100644 --- a/kubemarine/admission.py +++ b/kubemarine/admission.py @@ -16,7 +16,7 @@ import os import uuid import re -from typing import Dict, Any, List, Optional +from typing import Dict, Any, List, Optional, Union import ruamel.yaml import yaml @@ -899,22 +899,22 @@ def get_labels_to_ensure_profile(inventory: dict, profile: str) -> Dict[str, str def label_namespace_pss(cluster: KubernetesCluster, manage_type: str) -> None: first_control_plane = cluster.nodes["control-plane"].get_first_member() # set/delete labels on predifined plugins namsespaces - for namespace, profile in builtin.get_namespace_to_necessary_pss_profiles(cluster).items(): + for ns_name, profile in builtin.get_namespace_to_necessary_pss_profiles(cluster).items(): target_labels = get_labels_to_ensure_profile(cluster.inventory, profile) if manage_type in ["apply", "install"] and target_labels: - cluster.log.debug(f"Set PSS labels for profile {profile} on namespace {namespace}") + cluster.log.debug(f"Set PSS labels for profile {profile} on namespace {ns_name}") command = "kubectl label ns {namespace} {lk}={lv} --overwrite" else: # manage_type == "delete" or default labels are not necessary - cluster.log.debug(f"Delete PSS labels from namespace {namespace}") + cluster.log.debug(f"Delete PSS labels from namespace {ns_name}") command = "kubectl label ns {namespace} {lk}- || true" target_labels = _get_default_labels(profile) for lk, lv in target_labels.items(): - first_control_plane.sudo(command.format(namespace=namespace, lk=lk, lv=lv)) + first_control_plane.sudo(command.format(namespace=ns_name, lk=lk, lv=lv)) procedure_config = cluster.procedure_inventory["pss"] - namespaces = procedure_config.get("namespaces") + namespaces: List[Union[str, Dict[str, dict]]] = procedure_config.get("namespaces") # get the list of namespaces that should be labeled then set/delete labels if namespaces: default_modes = {} diff --git a/kubemarine/core/connections.py b/kubemarine/core/connections.py index 551a16dc1..2c18ac8ed 100644 --- a/kubemarine/core/connections.py +++ b/kubemarine/core/connections.py @@ -14,7 +14,7 @@ import os from typing import Dict, List -import fabric # type: ignore[import] +import fabric # type: ignore[import-untyped] from kubemarine.core import static diff --git a/kubemarine/core/executor.py b/kubemarine/core/executor.py index a567ead76..b259ccfbd 100644 --- a/kubemarine/core/executor.py +++ b/kubemarine/core/executor.py @@ -22,8 +22,8 @@ from types import TracebackType from typing import Tuple, List, Dict, Callable, Any, Optional, Union, OrderedDict, TypeVar, Type, Mapping, Iterable -import fabric # type: ignore[import] -import fabric.transfer # type: ignore[import] +import fabric # type: ignore[import-untyped] +import fabric.transfer # type: ignore[import-untyped] from concurrent.futures.thread import ThreadPoolExecutor diff --git a/kubemarine/core/log.py b/kubemarine/core/log.py index d5be2af20..6ae02a1ca 100644 --- a/kubemarine/core/log.py +++ b/kubemarine/core/log.py @@ -17,7 +17,7 @@ import sys from abc import ABC, abstractmethod -from pygelf import gelf, GelfTcpHandler, GelfUdpHandler, GelfTlsHandler, GelfHttpHandler # type: ignore[import] +from pygelf import gelf, GelfTcpHandler, GelfUdpHandler, GelfTlsHandler, GelfHttpHandler # type: ignore[import-untyped] from copy import deepcopy from typing import Any, List, Optional, cast, Dict, Union diff --git a/kubemarine/core/utils.py b/kubemarine/core/utils.py index 811bfa8fd..54688d5bc 100755 --- a/kubemarine/core/utils.py +++ b/kubemarine/core/utils.py @@ -21,7 +21,7 @@ import time import tarfile -from typing import Tuple, Callable, List, TextIO, cast, Union, TypeVar, Dict +from typing import Tuple, Callable, List, TextIO, cast, Union, TypeVar, Dict, Sequence import yaml import ruamel.yaml @@ -40,9 +40,15 @@ _T_contra = TypeVar("_T_contra", contravariant=True) -class SupportsDunderLT(Protocol[_T_contra]): +class SupportsAllComparisons(Protocol[_T_contra]): def __lt__(self, __other: _T_contra) -> bool: ... + def __gt__(self, __other: _T_contra) -> bool: ... + + def __le__(self, __other: _T_contra) -> bool: ... + + def __ge__(self, __other: _T_contra) -> bool: ... + def do_fail(message: str = '', reason: Exception = None, hint: str = '', logger: log.EnhancedLogger = None) -> None: @@ -381,7 +387,7 @@ def yaml_structure_preserver() -> ruamel.yaml.YAML: return ruamel_yaml -def is_sorted(l: list, key: Callable = None) -> bool: +def is_sorted(l: Sequence[str], key: Callable[[str], SupportsAllComparisons] = None) -> bool: """ Check that the specified list is sorted. @@ -394,7 +400,7 @@ def is_sorted(l: list, key: Callable = None) -> bool: return all(key(l[i]) <= key(l[i + 1]) for i in range(len(l) - 1)) -def map_sorted(map_: CommentedMap, key: Callable[[str], SupportsDunderLT] = None) -> CommentedMap: +def map_sorted(map_: CommentedMap, key: Callable[[str], SupportsAllComparisons] = None) -> CommentedMap: """ Check that the specified CommentedMap is sorted, or create new sorted map from it otherwise. @@ -406,14 +412,14 @@ def map_sorted(map_: CommentedMap, key: Callable[[str], SupportsDunderLT] = None _key = key else: _key = lambda x: x - map_keys = list(map_) + map_keys: List[str] = list(map_) if not is_sorted(map_keys, key=_key): map_ = CommentedMap(sorted(map_.items(), key=lambda item: _key(item[0]))) return map_ -def insert_map_sorted(map_: CommentedMap, k: str, v: object, key: Callable = None) -> None: +def insert_map_sorted(map_: CommentedMap, k: str, v: object, key: Callable[[str], SupportsAllComparisons] = None) -> None: """ Insert new item to the CommentedMap or update the value for the existing key. The map should be already sorted. diff --git a/kubemarine/core/yaml_merger.py b/kubemarine/core/yaml_merger.py index 97fd346dd..f1f715e62 100644 --- a/kubemarine/core/yaml_merger.py +++ b/kubemarine/core/yaml_merger.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from deepmerge import Merger # type: ignore[import] +from deepmerge import Merger # type: ignore[import-untyped] def is_list_extends(nxt: list) -> bool: diff --git a/kubemarine/demo.py b/kubemarine/demo.py index 42a5e9937..e554af8c0 100644 --- a/kubemarine/demo.py +++ b/kubemarine/demo.py @@ -22,7 +22,7 @@ from copy import deepcopy from typing import List, Dict, Union, Any, Optional, Mapping, Iterable, IO, Tuple, cast -import fabric # type: ignore[import] +import fabric # type: ignore[import-untyped] import invoke from kubemarine import system, procedures diff --git a/kubemarine/procedures/check_paas.py b/kubemarine/procedures/check_paas.py index e57ed162c..feba185c4 100755 --- a/kubemarine/procedures/check_paas.py +++ b/kubemarine/procedures/check_paas.py @@ -39,7 +39,7 @@ from kubemarine.kubernetes.deployment import Deployment from kubemarine.kubernetes.object import KubernetesObject from kubemarine.coredns import generate_configmap -from deepdiff import DeepDiff # type: ignore[import] +from deepdiff import DeepDiff # type: ignore[import-untyped] def services_status(cluster: KubernetesCluster, service_type: str) -> None: diff --git a/kubemarine/resources/scripts/check_url_availability.py b/kubemarine/resources/scripts/check_url_availability.py index f04564581..cf30a9dc7 100644 --- a/kubemarine/resources/scripts/check_url_availability.py +++ b/kubemarine/resources/scripts/check_url_availability.py @@ -22,7 +22,7 @@ if major_version == 3: import urllib.request as urllib else: - import urllib2 as urllib # type: ignore[import, no-redef] + import urllib2 as urllib # type: ignore[import-not-found, no-redef] try: source = sys.argv[1] diff --git a/pyproject.toml b/pyproject.toml index 38d5a6f01..4f4c8dee2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,7 +50,7 @@ requires-python = ">=3.7" [project.optional-dependencies] ansible = ["ansible==7.0.*"] mypy = [ - "mypy==1.3.*", + "mypy==1.7.*", "types-PyYAML==6.*", "types-invoke==1.*", "types-toml==0.*", @@ -102,7 +102,7 @@ warn_unreachable = true # strict typing checks check_untyped_defs = true strict_equality = true -strict_concatenate = true +extra_checks = true disallow_subclassing_any = true disallow_untyped_decorators = true disallow_incomplete_defs = true