Skip to content

Commit

Permalink
[resotolib][feat] Enable mypy linting (#1560)
Browse files Browse the repository at this point in the history
* [resotolib][feat] Enable mypy linting

* fix types

* make it work again
  • Loading branch information
aquamatthias committed Apr 28, 2023
1 parent 5309e48 commit 8494e68
Show file tree
Hide file tree
Showing 35 changed files with 530 additions and 540 deletions.
4 changes: 2 additions & 2 deletions plugins/gcp/resoto_plugin_gcp/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ def post_process(resource: GCPDiskType, graph: Graph):
graph.remove_node(resource)
return

log.debug((f"Looking up pricing for {resource.rtdname}" f" in {resource.location(graph).rtdname}"))
log.debug((f"Looking up pricing for {resource.rtdname}" f" in {resource.resource_location(graph).rtdname}"))
resource_group_map = {
"local-ssd": "LocalSSD",
"pd-balanced": "SSD",
Expand Down Expand Up @@ -1024,7 +1024,7 @@ def post_process_machine_type(resource: GCPMachineType, graph: Graph):
graph.remove_node(resource)
return

log.debug((f"Looking up pricing for {resource.rtdname}" f" in {resource.location(graph).rtdname}"))
log.debug(f"Looking up pricing for {resource.rtdname}" f" in {resource.resource_location(graph).rtdname}")
skus = []
for sku in graph.searchall(
{
Expand Down
47 changes: 26 additions & 21 deletions resotolib/resotolib/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@
import os
import shutil
import subprocess
import sys
from collections import defaultdict
from typing import List, Dict, Any, Union, Callable
from typing import List, Dict, Any, Union, Callable, Optional, Sequence, Tuple

DEFAULT_ENV_ARGS_PREFIX = "RESOTO_"


class Namespace(argparse.Namespace):
def __getattr__(self, item):
def __getattr__(self, item: str) -> Any:
return None


class _MachineHelpAction(argparse.Action):
def __init__(
self,
option_strings,
dest=argparse.SUPPRESS,
default=argparse.SUPPRESS,
help=None,
):
option_strings: Sequence[str],
dest: str = argparse.SUPPRESS,
default: str = argparse.SUPPRESS,
help: Optional[str] = None,
) -> None:
super(_MachineHelpAction, self).__init__(
option_strings=option_strings,
dest=dest,
Expand All @@ -30,8 +29,14 @@ def __init__(
help=help,
)

def __call__(self, parser, namespace, values, option_string=None):
parser.print_machine_help()
def __call__(
self,
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
values: Union[str, Sequence[Any], None],
option_string: Optional[str] = None,
) -> None:
parser.print_machine_help() # type: ignore
parser.exit()


Expand All @@ -43,10 +48,10 @@ class ArgumentParser(argparse.ArgumentParser):

def __init__(
self,
*args,
*args: Any,
env_args_prefix: str = DEFAULT_ENV_ARGS_PREFIX,
add_machine_help: bool = True,
**kwargs,
**kwargs: Any,
) -> None:
super().__init__(*args, **kwargs)
self.env_args_prefix = env_args_prefix
Expand All @@ -60,22 +65,22 @@ def __init__(
help="print machine readable help",
)

def print_machine_help(self):
def print_machine_help(self) -> None:
for action in self._actions:
if action.default == argparse.SUPPRESS:
continue
for option_string in action.option_strings:
print(option_string)

def parse_known_args(self, args=None, namespace=None):
def parse_known_args(self, args: Any = None, namespace: Any = None) -> Tuple[Namespace, List[str]]:
for action in self._actions:
env_name = None
for option_string in action.option_strings:
if option_string.startswith("--"):
env_name = self.env_args_prefix + option_string[2:].replace("-", "_").upper()
break
if env_name is not None and action.default != argparse.SUPPRESS:
new_default = None
new_default: Any = None
if action.nargs not in (0, None):
new_default = os.environ.get(env_name)
if new_default is not None:
Expand Down Expand Up @@ -106,8 +111,8 @@ def parse_known_args(self, args=None, namespace=None):

action.default = new_default
ret_args, ret_argv = super().parse_known_args(args=args, namespace=namespace)
ArgumentParser.args = ret_args
return ret_args, ret_argv
ArgumentParser.args = ret_args # type: ignore
return ret_args, ret_argv # type: ignore


def get_arg_parser(
Expand All @@ -123,8 +128,8 @@ def get_arg_parser(
NoneType = type(None)


def convert(value: Any, type_goal: Union[type, Callable]) -> Any:
if type_goal is NoneType:
def convert(value: Any, type_goal: Union[type, Callable[[Any], Any]]) -> Any:
if type_goal is NoneType: # type: ignore
return value
elif isinstance(type_goal, type):
try:
Expand All @@ -142,14 +147,14 @@ def convert(value: Any, type_goal: Union[type, Callable]) -> Any:
return type_goal(value)


def args_dispatcher(dispatch_to: List, use_which: bool = False, argv: List = sys.argv[1:]) -> Dict[str, List[str]]:
def args_dispatcher(dispatch_to: List[str], use_which: bool, argv: List[str]) -> Dict[str, List[str]]:
dispatch_args = defaultdict(list)
prog_args = defaultdict(list)

for prog in dispatch_to:
cmd = prog
if use_which:
cmd = shutil.which(prog)
cmd = shutil.which(prog) # type: ignore
result = subprocess.run([cmd, "--machine-help"], capture_output=True)
if result.returncode != 0:
continue
Expand Down
4 changes: 2 additions & 2 deletions resotolib/resotolib/asynchronous/web/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def raw_jwt_from_auth_message(msg: str) -> Optional[str]:
try:
js = json.loads(msg)
assert js.get("kind") == "authorization"
return js.get("jwt")
return js.get("jwt") # type: ignore
except Exception:
return None

Expand Down Expand Up @@ -72,7 +72,7 @@ async def valid_jwt_handler(request: Request, handler: RequestHandler) -> Stream
authorized = False
if auth_header:
# make sure origin and host match, so the request is valid
origin: Optional[str] = urlparse(request.headers.get("Origin")).hostname
origin: Optional[str] = urlparse(request.headers.get("Origin")).hostname # type: ignore
host: Optional[str] = request.headers.get("Host")
if host is not None and origin is not None:
if ":" in host:
Expand Down
2 changes: 1 addition & 1 deletion resotolib/resotolib/asynchronous/web/ws_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async def clean_ws_handler(ws_id: str, websocket_handler: WSHandler) -> None:


def js_str(a: Any) -> str:
return jsons.dumps(a, strip_privates=True)
return jsons.dumps(a, strip_privates=True) # type: ignore


async def accept_websocket(
Expand Down
15 changes: 8 additions & 7 deletions resotolib/resotolib/baseplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from abc import ABC, abstractmethod
from enum import Enum, auto
from threading import Thread, current_thread
from typing import Dict, Optional, Set
from typing import Dict, Optional, Set, Any

from prometheus_client import Counter

Expand Down Expand Up @@ -101,18 +101,18 @@ def __init__(self, tls_data: Optional[TLSData] = None) -> None:
self.tls_data = tls_data

@abstractmethod
def do_action(self, data: Dict):
def do_action(self, data: Dict[str, Any]) -> None:
"""Perform an action"""
pass

def action_processor(self, message: Dict) -> Optional[Json]:
def action_processor(self, message: Json) -> Optional[Json]:
"""Process incoming action messages"""
if not isinstance(message, dict):
log.error(f"Invalid message: {message}")
return None
kind = message.get("kind")
message_type = message.get("message_type")
data = message.get("data")
data: Json = message.get("data", {})
log.debug(f"Received message of kind {kind}, type {message_type}, data: {data}")
if kind == "action":
try:
Expand All @@ -129,12 +129,13 @@ def action_processor(self, message: Dict) -> Optional[Json]:
else:
reply_kind = "action_done"

reply_message = {
return {
"kind": reply_kind,
"message_type": message_type,
"data": data,
}
return reply_message
else:
return None

def run(self) -> None:
try:
Expand Down Expand Up @@ -235,7 +236,7 @@ def pre_cleanup(config: Config, resource: BaseResource, graph: Graph) -> bool:

@staticmethod
def cleanup(config: Config, resource: BaseResource, graph: Graph) -> bool:
return resource.cleanup(graph)
return resource.cleanup(graph) # type: ignore

def go(self) -> None:
self.collect()
Expand Down

0 comments on commit 8494e68

Please sign in to comment.