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

Typing #472

Closed
wants to merge 9 commits into from
Closed
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
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ jobs:
- name: Local integration tests
run: pytest -m local
if: ${{ matrix.os != 'windows-2019' }}

- name: Mypy typing checks
run: mypy pyinfra/ pyinfra_cli/
if: ${{ matrix.os == 'ubuntu-18.04' && matrix.python-version == '3.8' }}
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include README.md LICENSE.md CHANGELOG.md
include README.md LICENSE.md CHANGELOG.md pyinfra/py.typed
global-include *.pyi
21 changes: 11 additions & 10 deletions pyinfra/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
# Global pyinfra logger
logger = logging.getLogger('pyinfra')

# Setup package level version
from .version import __version__ # noqa
from . import pseudo_modules # noqa: E402
from .version import __version__ # noqa: E402, F401

# Trigger pseudo_* creation
from . import pseudo_modules # noqa

# Trigger fact index creation
from . import facts # noqa

# Trigger module imports
from . import operations # noqa # pragma: no cover
# Setup pseudo modules
state = pseudo_state = pseudo_modules.pseudo_state
host = pseudo_host = pseudo_modules.pseudo_host
inventory = pseudo_inventory = pseudo_modules.pseudo_inventory

# Initialise base classes - this sets the pseudo modules to point at the underlying
# class objects (Host, etc), which makes ipython/etc work as expected.
pseudo_modules.init_base_classes()

# TODO: remove this once we have explicit fact importing (break in v2)
# Trigger fact/operations index creation
from . import facts # noqa
from . import operations # noqa # pragma: no cover
81 changes: 81 additions & 0 deletions pyinfra/api/facts.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from typing import Any, Callable, Generic, Optional, Type, Union

FACTS: Any
FACT_LOCK: Any
SUDO_REGEX: str
SU_REGEXES: Any


def is_fact(name: str):
...


def get_fact_class(name: str):
...


def get_fact_names():
...


class FactMeta(type):
def __init__(cls, name: str, bases: Any, attrs: dict) -> None:
...


class FactBase(Generic, metaclass=FactMeta):
abstract: bool = ...
shell_executable: Optional[Union[str, Callable]] = ...
requires_command: Optional[Union[str, Callable]] = ...

@staticmethod
def default() -> Any:
...

@staticmethod
def process(output: list[str]) -> Any:
...

def process_pipeline(self, args: Any, output: Any):
...


class ShortFactBase(metaclass=FactMeta):
fact: Type[FactBase] = ...


def get_short_facts(state: Any, short_fact: Any, **kwargs: Any):
...


def get_facts(
state: Any,
name: Any,
args: Optional[Any] = ...,
ensure_hosts: Optional[Any] = ...,
apply_failed_hosts: bool = ...,
):
...


def get_host_fact(state: Any, host: Any, name: Any):
...


def create_host_fact(
state: Any,
host: Any,
name: Any,
data: Any,
args: Optional[Any] = ...,
) -> None:
...


def delete_host_fact(
state: Any,
host: Any,
name: Any,
args: Optional[Any] = ...,
) -> None:
...
32 changes: 32 additions & 0 deletions pyinfra/api/operation.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import Any, Callable, Optional

OPERATIONS: Any


def get_operation_names():
...


class OperationMeta:
commands: Any = ...
hash: Any = ...
changed: Any = ...

def __init__(self, hash: Optional[Any] = ..., commands: Optional[Any] = ...) -> None:
...


def add_op(state: Any, op_func: Any, *args: Any, **kwargs: Any):
...


def show_set_name_warning(call_location: Any) -> None:
...


def show_state_host_arguments_warning(call_location: Any) -> None:
...


def operation(func: Optional[Callable[..., Any]] = ..., pipeline_facts: Optional[Any] = ...):
...
4 changes: 2 additions & 2 deletions pyinfra/operations/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import six

from jinja2 import TemplateRuntimeError, TemplateSyntaxError, UndefinedError
from jinja2 import TemplateError, UndefinedError

from pyinfra import logger
from pyinfra.api import (
Expand Down Expand Up @@ -802,7 +802,7 @@ def template(
# Render and make file-like it's output
try:
output = get_template(src).render(data)
except (TemplateRuntimeError, TemplateSyntaxError, UndefinedError) as e:
except (TemplateError, UndefinedError) as e:
trace_frames = traceback.extract_tb(sys.exc_info()[2])
trace_frames = [
frame for frame in trace_frames
Expand Down
24 changes: 3 additions & 21 deletions pyinfra/pseudo_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
executing in CLI mode).
'''

import sys

import pyinfra


class PseudoModule(object):
_module = None
Expand Down Expand Up @@ -57,23 +53,9 @@ def isset(self):
return self._module is not None


# The current deploy state
pseudo_state = \
sys.modules['pyinfra.pseudo_state'] = sys.modules['pyinfra.state'] = \
pyinfra.pseudo_state = pyinfra.state = \
PseudoModule()

# The current deploy inventory
pseudo_inventory = \
sys.modules['pyinfra.pseudo_inventory'] = sys.modules['pyinfra.inventory'] = \
pyinfra.pseudo_inventory = pyinfra.inventory = \
PseudoModule()

# The current target host
pseudo_host = \
sys.modules['pyinfra.pseudo_host'] = sys.modules['pyinfra.host'] = \
pyinfra.pseudo_host = pyinfra.host = \
PseudoModule()
pseudo_state = PseudoModule()
pseudo_inventory = PseudoModule()
pseudo_host = PseudoModule()


def init_base_classes():
Expand Down
Empty file added pyinfra/py.typed
Empty file.
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ whitelist = words.txt
dictionaries=en_US,python,technical,django


[mypy]
ignore_missing_imports = true


[coverage:report]
show_missing = true
skip_covered = true
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
'mock==3.0.5',
'codecov==2.1.8',

# Type checking
'mypy==0.790 ; python_version >= "3"',

# Linting
'flake8==3.8.3',
'flake8-commas==2.0.0',
Expand Down