Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional type hints to core #860

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions nornir/core/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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":
Expand All @@ -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`

Expand All @@ -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.

Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading