From 0424c07f9f453233a8bd7a7260e254c3a05b0f6e Mon Sep 17 00:00:00 2001 From: Patrick Ogenstad Date: Tue, 3 Oct 2023 02:38:31 +0200 Subject: [PATCH] Add additional type hints to core (#860) --- nornir/core/__init__.py | 32 +++++++++++++++++++------------- setup.cfg | 4 +++- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/nornir/core/__init__.py b/nornir/core/__init__.py index c30334fb..73ee926d 100644 --- a/nornir/core/__init__.py +++ b/nornir/core/__init__.py @@ -1,13 +1,14 @@ import logging import logging.config -from typing import List, Optional, TYPE_CHECKING +import types +from typing import Any, Dict, Callable, Generator, List, Optional, Type, TYPE_CHECKING from nornir.core.configuration import Config from nornir.core.inventory import Inventory from nornir.core.plugins.runners import RunnerPlugin from nornir.core.processor import Processor, Processors from nornir.core.state import GlobalState -from nornir.core.task import Task +from nornir.core.task import AggregatedResult, Task if TYPE_CHECKING: from nornir.core.inventory import Host # noqa: W0611 @@ -36,8 +37,8 @@ class Nornir(object): def __init__( self, inventory: Inventory, - config: Config = None, - data: GlobalState = None, + config: Optional[Config] = None, + data: Optional[GlobalState] = None, processors: Optional[Processors] = None, runner: Optional[RunnerPlugin] = None, ) -> None: @@ -47,10 +48,15 @@ def __init__( self.processors = processors or Processors() self.runner = runner - def __enter__(self): + def __enter__(self) -> "Nornir": return self - def __exit__(self, exc_type, exc_val, exc_tb): + def __exit__( + self, + exc_type: Type[BaseException], + exc_val: BaseException, + exc_tb: Optional[types.TracebackType] = None, + ) -> None: self.close_connections(on_good=True, on_failed=True) def with_processors(self, processors: List[Processor]) -> "Nornir": @@ -67,7 +73,7 @@ def with_runner(self, runner: RunnerPlugin) -> "Nornir": """ return Nornir(**{**self.__dict__, **{"runner": runner}}) - def filter(self, *args, **kwargs): + def filter(self, *args: Any, **kwargs: Any) -> "Nornir": """ See :py:meth:`nornir.core.inventory.Inventory.filter` @@ -85,8 +91,8 @@ def run( on_good=True, on_failed=False, name: Optional[str] = None, - **kwargs, - ): + **kwargs: Any, + ) -> AggregatedResult: """ Run task over all the hosts in the inventory. @@ -152,22 +158,22 @@ def run( return result - def dict(self): + def dict(self) -> Dict[str, Any]: """Return a dictionary representing the object.""" return {"data": self.data.dict(), "inventory": self.inventory.dict()} - def close_connections(self, on_good=True, on_failed=False): + def close_connections(self, on_good: bool = True, on_failed: bool = False) -> None: def close_connections_task(task): task.host.close_connections() self.run(task=close_connections_task, on_good=on_good, on_failed=on_failed) @classmethod - def get_validators(cls): + def get_validators(cls) -> Generator[Callable[["Nornir"], "Nornir"], None, None]: yield cls.validate @classmethod - def validate(cls, v): + def validate(cls, v: "Nornir") -> "Nornir": if not isinstance(v, cls): raise ValueError(f"Nornir: Nornir expected not {type(v)}") return v diff --git a/setup.cfg b/setup.cfg index b2d5a62c..bc69f979 100644 --- a/setup.cfg +++ b/setup.cfg @@ -46,7 +46,9 @@ warn_return_any = True warn_redundant_casts = True [mypy-nornir.core] -ignore_errors = True +disallow_untyped_defs = False +disallow_incomplete_defs = False +strict_optional = False [mypy-tests.*] ignore_errors = True