From 0bb5da5cebb8837c484d0e564727a85d6e3a9e41 Mon Sep 17 00:00:00 2001 From: Jan Luebbe Date: Wed, 25 Aug 2021 09:03:14 +0200 Subject: [PATCH] mypy: add some initial typo annotations mypy was unable to infer these types, so specifiy them explicitly. Signed-off-by: Jan Luebbe --- labgrid/binding.py | 3 ++- labgrid/environment.py | 3 ++- labgrid/remote/exporter.py | 3 ++- labgrid/resource/common.py | 11 ++++++----- labgrid/util/ssh.py | 3 ++- mypy.ini | 2 ++ 6 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 mypy.ini diff --git a/labgrid/binding.py b/labgrid/binding.py index 55444cdf9..ee692112f 100644 --- a/labgrid/binding.py +++ b/labgrid/binding.py @@ -1,5 +1,6 @@ import enum from functools import wraps +from typing import Any, Dict import attr @@ -36,7 +37,7 @@ class BindingMixin: callback). """ - bindings = {} + bindings: Dict[str, Any] = {} # these are controlled by the Target target = attr.ib() diff --git a/labgrid/environment.py b/labgrid/environment.py index 7e317bec4..c4aba1b98 100644 --- a/labgrid/environment.py +++ b/labgrid/environment.py @@ -1,4 +1,5 @@ import os +from typing import Optional import attr from .target import Target @@ -34,7 +35,7 @@ def __attrs_post_init__(self): module = importlib.import_module(user_import) sys.modules[module_name] = module - def get_target(self, role: str = 'main') -> Target: + def get_target(self, role: str = 'main') -> Optional[Target]: """Returns the specified target or None if not found. Each target is initialized as needed. diff --git a/labgrid/remote/exporter.py b/labgrid/remote/exporter.py index 5fbfc1116..3995f7ced 100755 --- a/labgrid/remote/exporter.py +++ b/labgrid/remote/exporter.py @@ -12,6 +12,7 @@ import shutil import subprocess import warnings +from typing import Dict, Type from socket import gethostname, getfqdn import attr from autobahn.asyncio.wamp import ApplicationRunner, ApplicationSession @@ -27,7 +28,7 @@ __version__ = "unknown" -exports = {} +exports: Dict[str, Type[ResourceEntry]] = {} reexec = False class ExporterError(Exception): diff --git a/labgrid/resource/common.py b/labgrid/resource/common.py index ebd485f21..1b6cbf671 100644 --- a/labgrid/resource/common.py +++ b/labgrid/resource/common.py @@ -1,3 +1,4 @@ +from typing import Dict, Type import attr from ..binding import BindingMixin @@ -81,10 +82,10 @@ def command_prefix(self): @attr.s(eq=False) class ResourceManager: - instances = {} + instances: 'Dict[Type[ResourceManager], ResourceManager]' = {} @classmethod - def get(cls): + def get(cls) -> 'ResourceManager': instance = ResourceManager.instances.get(cls) if instance is None: instance = cls() @@ -92,13 +93,13 @@ def get(cls): return instance def __attrs_post_init__(self): - self.resources = [] + self.resources: List[ManagedResource] = [] - def _add_resource(self, resource): + def _add_resource(self, resource: 'ManagedResource'): self.resources.append(resource) self.on_resource_added(resource) - def on_resource_added(self, resource): + def on_resource_added(self, resource: 'ManagedResource'): pass def poll(self): diff --git a/labgrid/util/ssh.py b/labgrid/util/ssh.py index 51000c6d8..a5da988fc 100644 --- a/labgrid/util/ssh.py +++ b/labgrid/util/ssh.py @@ -7,6 +7,7 @@ import os from select import select from functools import wraps +from typing import Dict import attr from ..driver.exception import ExecutionError @@ -22,7 +23,7 @@ class SSHConnectionManager: should not be directly instantiated, use the exported sshmanager from this module instead. """ - _connections = attr.ib( + _connections: 'Dict[str, SSHConnection]' = attr.ib( default=attr.Factory(dict), init=False, validator=attr.validators.optional(attr.validators.instance_of(dict)) diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 000000000..976ba0294 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,2 @@ +[mypy] +ignore_missing_imports = True