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

V3: type hints for operations #1082

Merged
merged 17 commits into from
Jun 8, 2024
Merged
8 changes: 5 additions & 3 deletions pyinfra/api/command.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import shlex
from inspect import getfullargspec
from string import Formatter
from typing import TYPE_CHECKING, Callable, Union
from typing import IO, TYPE_CHECKING, Callable, Union

import gevent
from typing_extensions import Unpack
Expand Down Expand Up @@ -143,7 +145,7 @@ def execute(self, state: "State", host: "Host", connector_arguments: ConnectorAr
class FileUploadCommand(PyinfraCommand):
def __init__(
self,
src: str,
src: str | IO,
dest: str,
remote_temp_filename=None,
**kwargs: Unpack[ConnectorArguments],
Expand Down Expand Up @@ -173,7 +175,7 @@ class FileDownloadCommand(PyinfraCommand):
def __init__(
self,
src: str,
dest: str,
dest: str | IO,
remote_temp_filename=None,
**kwargs: Unpack[ConnectorArguments],
):
Expand Down
6 changes: 3 additions & 3 deletions pyinfra/api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,16 @@ def get_caller_frameinfo(frame_offset: int = 0):


def get_operation_order_from_stack(state: "State"):

stack_items = list(reversed(stack()))

i = 0
# Find the *first* occurrence of our deploy file in the reversed stack
if state.current_deploy_filename:
for i, stack_item in enumerate(stack_items):
frame = getframeinfo(stack_item[0])
if frame.filename == state.current_deploy_filename:
break
else:
i = 0

# Now generate a list of line numbers *following that file*
line_numbers = []
Expand All @@ -139,7 +139,7 @@ def get_operation_order_from_stack(state: "State"):
return line_numbers


def get_template(filename_or_io: str):
def get_template(filename_or_io: str | IO):
"""
Gets a jinja2 ``Template`` object for the input filename or string, with caching
based on the filename of the template, or the SHA1 of the input string.
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/apk.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pyinfra.api import FactBase

from .util.packaging import parse_packages
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/apt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re

from pyinfra.api import FactBase
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/brew.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re

from pyinfra import logger
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/bsdinit.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from .sysvinit import InitdStatus


Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/cargo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# encoding: utf8

from __future__ import annotations

from pyinfra.api import FactBase

from .util.packaging import parse_packages
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/choco.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pyinfra.api import FactBase

from .util.packaging import parse_packages
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/deb.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re
import shlex

Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/dnf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pyinfra.api import FactBase

from .util import make_cat_files_command
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/docker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json

from pyinfra.api import FactBase
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
The files facts provide information about the filesystem and it's contents on the target host.
"""

from __future__ import annotations

import re
import stat
from datetime import datetime
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/gem.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pyinfra.api import FactBase

from .util.packaging import parse_packages
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/gpg.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from urllib.parse import urlparse

from pyinfra.api import FactBase
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/launchd.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pyinfra.api import FactBase


Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/lxd.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json

from pyinfra.api import FactBase
Expand Down
18 changes: 12 additions & 6 deletions pyinfra/facts/mysql.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re
from collections import defaultdict

Expand All @@ -8,11 +10,11 @@


def make_mysql_command(
database=None,
user=None,
password=None,
host=None,
port=None,
database: str | None = None,
user: str | None = None,
password: str | None = None,
host: str | None = None,
port: int | None = None,
executable="mysql",
):
target_bits = [executable]
Expand All @@ -37,7 +39,11 @@ def make_mysql_command(
return StringCommand(*target_bits)


def make_execute_mysql_command(command, ignore_errors=False, **mysql_kwargs):
def make_execute_mysql_command(
command: str | StringCommand,
ignore_errors=False,
**mysql_kwargs,
):
commands_bits = [
make_mysql_command(**mysql_kwargs),
"-Be",
Expand Down
1 change: 1 addition & 0 deletions pyinfra/facts/npm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# encoding: utf8
from __future__ import annotations

from pyinfra.api import FactBase

Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/openrc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re

from pyinfra.api import FactBase
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/pacman.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import shlex

from pyinfra.api import FactBase
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/pip.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pyinfra.api import FactBase

from .util.packaging import parse_packages
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/pkg.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pyinfra.api import FactBase

from .util.packaging import parse_packages
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/pkgin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pyinfra.api import FactBase

from .util.packaging import parse_packages
Expand Down
12 changes: 6 additions & 6 deletions pyinfra/facts/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@


def make_psql_command(
database=None,
user=None,
password=None,
host=None,
port=None,
database: str | None = None,
user: str | None = None,
password: str | None = None,
host: str | None = None,
port: str | int | None = None,
executable="psql",
):
) -> StringCommand:
target_bits: list[str] = []

if password:
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/postgresql.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from .postgres import PostgresDatabases, PostgresRoles


Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/rpm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re
import shlex

Expand Down
15 changes: 5 additions & 10 deletions pyinfra/facts/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import shutil
from datetime import datetime
from tempfile import mkdtemp
from typing import Dict, List, NewType, Optional, Union
from typing import Dict, List, Optional, Union

from dateutil.parser import parse as parse_date
from distro import distro
Expand Down Expand Up @@ -350,8 +350,7 @@ class Groups(FactBase[List[str]]):
command = "cat /etc/group"
default = list

@staticmethod
def process(output) -> list[str]:
def process(self, output) -> list[str]:
groups: list[str] = []

for line in output:
Expand All @@ -361,9 +360,6 @@ def process(output) -> list[str]:
return groups


CrontabCommand = NewType("CrontabCommand", int)


class CrontabDict(TypedDict):
minute: NotRequired[Union[int, str]]
hour: NotRequired[Union[int, str]]
Expand All @@ -374,7 +370,7 @@ class CrontabDict(TypedDict):
special_time: NotRequired[str]


class Crontab(FactBase[Dict[CrontabCommand, CrontabDict]]):
class Crontab(FactBase[Dict[str, CrontabDict]]):
"""
Returns a dictionary of cron command -> execution time.

Expand Down Expand Up @@ -404,9 +400,8 @@ def command(user=None):
return "crontab -l -u {0} || true".format(user)
return "crontab -l || true"

@staticmethod
def process(output):
crons: dict[Command, CrontabDict] = {}
def process(self, output):
crons: dict[str, CrontabDict] = {}
current_comments = []

for line in output:
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/snap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re

from pyinfra.api import FactBase
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/systemd.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re
from typing import Dict, Iterable

Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/upstart.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re

from pyinfra.api import FactBase
Expand Down
3 changes: 2 additions & 1 deletion pyinfra/facts/util/packaging.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations

import re
from typing import Iterable


def parse_packages(regex, output):
def parse_packages(regex: str, output: Iterable[str]) -> dict[str, set[str]]:
packages: dict[str, set[str]] = {}

for line in output:
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/vzctl.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json

from pyinfra.api import FactBase
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/xbps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pyinfra.api import FactBase

from .util.packaging import parse_packages
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/yum.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pyinfra.api import FactBase

from .util import make_cat_files_command
Expand Down
2 changes: 2 additions & 0 deletions pyinfra/facts/zypper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pyinfra.api import FactBase

from .util import make_cat_files_command
Expand Down
4 changes: 3 additions & 1 deletion pyinfra/operations/apk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Manage apk packages.
"""

from __future__ import annotations

from pyinfra import host
from pyinfra.api import operation
from pyinfra.facts.apk import ApkPackages
Expand Down Expand Up @@ -40,7 +42,7 @@ def update():

@operation()
def packages(
packages=None,
packages: str | list[str] | None = None,
present=True,
latest=False,
update=False,
Expand Down
Loading
Loading