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

Type hints for operations #1081

Closed
12 changes: 6 additions & 6 deletions pyinfra/facts/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@


def make_mysql_command(
database=None,
user=None,
password=None,
host=None,
port=None,
database: str = None,
user: str = None,
password: str = None,
host: str = None,
port: int = None,
executable="mysql",
):
target_bits = [executable]
Expand All @@ -37,7 +37,7 @@ 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, ignore_errors=False, **mysql_kwargs):
commands_bits = [
make_mysql_command(**mysql_kwargs),
"-Be",
Expand Down
3 changes: 2 additions & 1 deletion pyinfra/facts/util/packaging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import re
from typing import Iterable


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

for line in output:
Expand Down
2 changes: 1 addition & 1 deletion pyinfra/operations/apk.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def update():

@operation
def packages(
packages=None,
packages: str | list[str] = None,
present=True,
latest=False,
update=False,
Expand Down
20 changes: 10 additions & 10 deletions pyinfra/operations/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
APT_UPDATE_FILENAME = "/var/lib/apt/periodic/update-success-stamp"


def noninteractive_apt(command, force=False):
def noninteractive_apt(command: str, force=False):
args = ["DEBIAN_FRONTEND=noninteractive apt-get -y"]

if force:
Expand All @@ -37,7 +37,7 @@ def noninteractive_apt(command, force=False):


@operation
def key(src=None, keyserver=None, keyid=None):
def key(src: str = None, keyserver: str = None, keyid: str = None):
"""
Add apt gpg keys with ``apt-key``.

Expand Down Expand Up @@ -109,7 +109,7 @@ def key(src=None, keyserver=None, keyid=None):


@operation
def repo(src, present=True, filename=None):
def repo(src: str, present=True, filename: str = None):
"""
Add/remove apt repositories.

Expand Down Expand Up @@ -172,7 +172,7 @@ def repo(src, present=True, filename=None):


@operation(is_idempotent=False)
def ppa(src, present=True):
def ppa(src: str, present=True):
"""
Add/remove Ubuntu ppa repositories.

Expand Down Expand Up @@ -202,7 +202,7 @@ def ppa(src, present=True):


@operation
def deb(src, present=True, force=False):
def deb(src: str, present=True, force=False):
"""
Add/remove ``.deb`` file packages.

Expand Down Expand Up @@ -291,7 +291,7 @@ def deb(src, present=True, force=False):
"unless the ``cache_time`` argument is provided."
),
)
def update(cache_time=None):
def update(cache_time: int = None):
"""
Updates apt repositories.

Expand Down Expand Up @@ -392,17 +392,17 @@ def dist_upgrade():

@operation
def packages(
packages=None,
packages: str | list[str] = None,
present=True,
latest=False,
update=False,
cache_time=None,
cache_time: int = None,
upgrade=False,
force=False,
no_recommends=False,
allow_downgrades=False,
extra_install_args=None,
extra_uninstall_args=None,
extra_install_args: str = None,
extra_uninstall_args: str = None,
):
"""
Install/remove/update packages & update apt.
Expand Down
12 changes: 6 additions & 6 deletions pyinfra/operations/brew.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def upgrade():

@operation
def packages(
packages=None,
packages: str | list[str] = None,
present=True,
latest=False,
update=False,
Expand Down Expand Up @@ -93,7 +93,7 @@ def packages(
)


def cask_args(host):
def cask_args():
return ("", " --cask") if new_cask_cli(host.get_fact(BrewVersion)) else ("cask ", "")


Expand All @@ -106,14 +106,14 @@ def cask_upgrade():
Upgrades all brew casks.
"""

yield "brew %supgrade%s" % cask_args(host)
yield "brew %supgrade%s" % cask_args()


@operation(
pipeline_facts={"brew_version": ""},
)
def casks(
casks=None,
casks: str | list[str] = None,
present=True,
latest=False,
upgrade=False,
Expand Down Expand Up @@ -145,7 +145,7 @@ def casks(
if upgrade:
yield from cask_upgrade()

args = cask_args(host)
args = cask_args()

yield from ensure_packages(
host,
Expand All @@ -161,7 +161,7 @@ def casks(


@operation
def tap(src=None, present=True, url=None):
def tap(src: str = None, present=True, url: str = None):
"""
Add/remove brew taps.

Expand Down
6 changes: 3 additions & 3 deletions pyinfra/operations/bsdinit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

@operation
def service(
service,
service: str,
running=True,
restarted=False,
reloaded=False,
command=None,
enabled=None,
command: str = None,
enabled: bool = None,
):
"""
Manage the state of BSD init services.
Expand Down
2 changes: 1 addition & 1 deletion pyinfra/operations/cargo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@operation
def packages(packages=None, present=True, latest=False):
def packages(packages: str | list[str] = None, present=True, latest=False):
"""
Install/remove/update cargo packages.

Expand Down
2 changes: 1 addition & 1 deletion pyinfra/operations/choco.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@operation
def packages(packages=None, present=True, latest=False):
def packages(packages: str | list[str] = None, present=True, latest=False):
"""
Add/remove/update ``choco`` packages.

Expand Down
27 changes: 12 additions & 15 deletions pyinfra/operations/dnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
from pyinfra.api import operation
from pyinfra.facts.rpm import RpmPackageProvides, RpmPackages

from . import files
from .util.packaging import ensure_packages, ensure_rpm, ensure_yum_repo


@operation(is_idempotent=False)
def key(src):
def key(src: str):
"""
Add dnf gpg keys with ``rpm``.

Expand All @@ -37,19 +36,19 @@ def key(src):

@operation
def repo(
src,
src: str,
present=True,
baseurl=None,
description=None,
baseurl: str = None,
description: str = None,
enabled=True,
gpgcheck=True,
gpgkey=None,
gpgkey: str = None,
):
# NOTE: if updating this docstring also update `yum.repo`
"""
Add/remove/update dnf repositories.

+ name: URL or name for the ``.repo`` file
+ src: URL or name for the ``.repo`` file
+ present: whether the ``.repo`` file should be present
+ baseurl: the baseurl of the repo (if ``name`` is not a URL)
+ description: optional verbose description
Expand Down Expand Up @@ -81,9 +80,7 @@ def repo(
"""

yield from ensure_yum_repo(
state,
host,
files,
src,
baseurl,
present,
Expand All @@ -95,7 +92,7 @@ def repo(


@operation
def rpm(src, present=True):
def rpm(src: str, present=True):
# NOTE: if updating this docstring also update `yum.rpm`
"""
Add/remove ``.rpm`` file packages.
Expand All @@ -118,7 +115,7 @@ def rpm(src, present=True):
)
"""

yield from ensure_rpm(state, host, files, src, present, "dnf")
yield from ensure_rpm(state, host, src, present, "dnf")


@operation(is_idempotent=False)
Expand All @@ -135,19 +132,19 @@ def update():

@operation
def packages(
packages=None,
packages: str | list[str] = None,
present=True,
latest=False,
update=False,
clean=False,
nobest=False,
extra_install_args=None,
extra_uninstall_args=None,
extra_install_args: str = None,
extra_uninstall_args: str = None,
):
"""
Install/remove/update dnf packages & updates.

+ packages: list of packages to ensure
+ packages: packages to ensure
+ present: whether the packages should be installed
+ latest: whether to upgrade packages without a specified version
+ update: run ``dnf update`` before installing packages
Expand Down
Loading
Loading