From a0c04c080906e136f3b0fe0168d547f0a98e4dac Mon Sep 17 00:00:00 2001 From: Huy Pham <42948410+hpham@users.noreply.github.com> Date: Tue, 14 Jan 2020 14:02:37 -0800 Subject: [PATCH] type hinting --- daemon/core/emulator/session.py | 1 - daemon/core/gui/images.py | 2 +- daemon/core/gui/nodeutils.py | 36 +++++++++++++++++++++------------ daemon/core/gui/statusbar.py | 2 +- daemon/core/gui/task.py | 7 +++++-- 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/daemon/core/emulator/session.py b/daemon/core/emulator/session.py index a81ba103..2634c817 100644 --- a/daemon/core/emulator/session.py +++ b/daemon/core/emulator/session.py @@ -772,7 +772,6 @@ class Session: Broadcast node location to all listeners. :param core.nodes.base.NodeBase node: node to broadcast location for - :return: nothing """ node_data = NodeData( message_type=0, diff --git a/daemon/core/gui/images.py b/daemon/core/gui/images.py index a6bc8909..c366c0fb 100644 --- a/daemon/core/gui/images.py +++ b/daemon/core/gui/images.py @@ -22,7 +22,7 @@ class Images: cls.images[image.stem] = str(image) @classmethod - def get(cls, image_enum, width: int, height: int = None): + def get(cls, image_enum: Enum, width: int, height: int = None): file_path = cls.images[image_enum.value] return cls.create(file_path, width, height) diff --git a/daemon/core/gui/nodeutils.py b/daemon/core/gui/nodeutils.py index e88ee997..7d86924a 100644 --- a/daemon/core/gui/nodeutils.py +++ b/daemon/core/gui/nodeutils.py @@ -1,8 +1,11 @@ -from typing import Set +from typing import TYPE_CHECKING, Set from core.api.grpc.core_pb2 import NodeType from core.gui.images import ImageEnum, Images +if TYPE_CHECKING: + from core.api.grpc import core_pb2 + ICON_SIZE = 48 ANTENNA_SIZE = 32 @@ -11,14 +14,21 @@ class NodeDraw: def __init__(self): self.custom: bool = False self.image = None - self.image_enum = None + self.image_enum: ImageEnum = None self.image_file = None - self.node_type = None - self.model = None + self.node_type: core_pb2.NodeType = None + self.model: str = None self.services: Set[str] = set() @classmethod - def from_setup(cls, image_enum, node_type, label, model=None, tooltip=None): + def from_setup( + cls, + image_enum: ImageEnum, + node_type: "core_pb2.NodeType", + label: str, + model: str = None, + tooltip=None, + ): node_draw = NodeDraw() node_draw.image_enum = image_enum node_draw.image = Images.get(image_enum, ICON_SIZE) @@ -29,7 +39,7 @@ class NodeDraw: return node_draw @classmethod - def from_custom(cls, name, image_file, services): + def from_custom(cls, name: str, image_file: str, services: str): node_draw = NodeDraw() node_draw.custom = True node_draw.image_file = image_file @@ -55,31 +65,31 @@ class NodeUtils: ANTENNA_ICON = None @classmethod - def is_ignore_node(cls, node_type) -> bool: + def is_ignore_node(cls, node_type: NodeType) -> bool: return node_type in cls.IGNORE_NODES @classmethod - def is_container_node(cls, node_type) -> bool: + def is_container_node(cls, node_type: NodeType) -> bool: return node_type in cls.CONTAINER_NODES @classmethod - def is_model_node(cls, node_type) -> bool: + def is_model_node(cls, node_type: NodeType) -> bool: return node_type == NodeType.DEFAULT @classmethod - def is_image_node(cls, node_type) -> bool: + def is_image_node(cls, node_type: NodeType) -> bool: return node_type in cls.IMAGE_NODES @classmethod - def is_wireless_node(cls, node_type) -> bool: + def is_wireless_node(cls, node_type: NodeType) -> bool: return node_type in cls.WIRELESS_NODES @classmethod - def is_rj45_node(cls, node_type) -> bool: + def is_rj45_node(cls, node_type: NodeType) -> bool: return node_type in cls.RJ45_NODES @classmethod - def node_icon(cls, node_type, model: str) -> bool: + def node_icon(cls, node_type: NodeType, model: str) -> bool: if model == "": model = None return cls.NODE_ICONS[(node_type, model)] diff --git a/daemon/core/gui/statusbar.py b/daemon/core/gui/statusbar.py index e9d77494..6de511c4 100644 --- a/daemon/core/gui/statusbar.py +++ b/daemon/core/gui/statusbar.py @@ -13,7 +13,7 @@ if TYPE_CHECKING: class StatusBar(ttk.Frame): - def __init__(self, master: tk.Widget, app: "Application", **kwargs): + def __init__(self, master: "Application", app: "Application", **kwargs): super().__init__(master, **kwargs) self.app = app self.status = None diff --git a/daemon/core/gui/task.py b/daemon/core/gui/task.py index dc2c1d34..7a86deae 100644 --- a/daemon/core/gui/task.py +++ b/daemon/core/gui/task.py @@ -1,11 +1,14 @@ import logging import threading -from typing import Callable, Optional +from typing import TYPE_CHECKING, Callable + +if TYPE_CHECKING: + from core.gui.app import Application class BackgroundTask: def __init__( - self, master, task: Callable, callback: Optional[Callable] = None, args=() + self, master: "Application", task: Callable, callback: Callable = None, args=() ): self.master = master self.args = args