diff --git a/rust2rpm/distgit.py b/rust2rpm/distgit.py index 1a5be83..bd4de08 100644 --- a/rust2rpm/distgit.py +++ b/rust2rpm/distgit.py @@ -1,3 +1,4 @@ +from typing import Optional from urllib.parse import urljoin import requests @@ -6,7 +7,7 @@ import requests DIST_GIT_API_URL = "https://src.fedoraproject.org/api/0/" -def get_package_info(package): +def get_package_info(package: str) -> Optional[dict]: """Download information about package from dist-git. Returns JSON with package metadata, or None if the package is diff --git a/rust2rpm/generator.py b/rust2rpm/generator.py index e0ca5ed..0363bb2 100644 --- a/rust2rpm/generator.py +++ b/rust2rpm/generator.py @@ -27,7 +27,7 @@ def spec_file_template(filename: str): def template_args_fedora(date: Optional[time.struct_time], packager: Optional[str], rpmautospec: bool) -> dict: - kwargs = dict() + kwargs: dict[str, str | bool] = dict() kwargs["include_build_requires"] = False kwargs["include_provides"] = False @@ -50,7 +50,7 @@ def template_args_fedora(date: Optional[time.struct_time], packager: Optional[st def template_args_mageia(date: Optional[time.struct_time], packager: Optional[str]) -> dict: - kwargs = dict() + kwargs: dict[str, str | bool] = dict() kwargs["include_build_requires"] = True kwargs["include_provides"] = False @@ -71,7 +71,7 @@ def template_args_mageia(date: Optional[time.struct_time], packager: Optional[st def template_args_opensuse(date: Optional[time.struct_time], packager: Optional[str]) -> dict: - kwargs = dict() + kwargs: dict[str, str | bool] = dict() kwargs["include_build_requires"] = True kwargs["include_provides"] = False @@ -93,7 +93,7 @@ def template_args_opensuse(date: Optional[time.struct_time], packager: Optional[ def template_args_plain(date: Optional[time.struct_time], packager: Optional[str]) -> dict: - kwargs = dict() + kwargs: dict[str, str | bool] = dict() kwargs["include_build_requires"] = True kwargs["include_provides"] = True @@ -326,7 +326,7 @@ def spec_render_workspace( else: cargo_args = "" - license_strs = {package.license for package in metadata.packages} + license_strs = {package.license for package in metadata.packages if package.license} if len(license_strs) == 1: rpm_license_tag, rpm_license_comments = translate_license(target, list(license_strs)[0]) else: diff --git a/rust2rpm/licensing.py b/rust2rpm/licensing.py index 20bc57d..f375387 100644 --- a/rust2rpm/licensing.py +++ b/rust2rpm/licensing.py @@ -1,13 +1,14 @@ import os import csv import functools +from typing import Optional from rust2rpm import log SPDX_TO_FEDORA_CSV = os.path.dirname(__file__) + "/spdx_to_fedora.csv" -def translate_slashes(license): +def translate_slashes(license: str) -> str: "Replace all slashes with OR, emit warning" split = [l.strip() for l in license.split("/")] if len(split) > 1: @@ -16,7 +17,7 @@ def translate_slashes(license): @functools.lru_cache() -def spdx_to_fedora_map(): +def spdx_to_fedora_map() -> dict[str, str]: with open(SPDX_TO_FEDORA_CSV, newline="") as f: reader = csv.DictReader(f) return { @@ -31,7 +32,7 @@ def dump_sdpx_to_fedora_map(file): print(f"{k} → {v}", file=file) -def translate_license_fedora(license): +def translate_license_fedora(license: str) -> tuple[str, Optional[str]]: comments = [] final = [] for tag in license.split(): @@ -64,7 +65,7 @@ def translate_license_fedora(license): return (" ".join(final), "\n".join(comments) or None) -def translate_license(target, license): +def translate_license(target: str, license: str) -> tuple[str, Optional[str]]: license = translate_slashes(license) if target in {"mageia"}: return translate_license_fedora(license) diff --git a/rust2rpm/log.py b/rust2rpm/log.py index 0cdf2d2..bc14eb2 100644 --- a/rust2rpm/log.py +++ b/rust2rpm/log.py @@ -10,25 +10,25 @@ import textwrap from termcolor import colored -def _eprint(message): +def _eprint(message: str): print(message, file=sys.stderr) -def _wrap(message, prefix): +def _wrap(message: str, prefix: str): return textwrap.wrap(message, 80, initial_indent=f"{prefix} ", subsequent_indent=" " * (len(prefix) + 1)) -def success(message): +def success(message: str): _eprint(colored("\n".join(_wrap(message, "•")), "green")) -def info(message): +def info(message: str): _eprint(colored("\n".join(_wrap(message, "•")), "grey")) -def warn(message): +def warn(message: str): _eprint(colored("\n".join(_wrap(message, "WARNING:")), "yellow")) -def error(message): +def error(message: str): _eprint(colored("\n".join(_wrap(message, "ERROR:")), "red", attrs=["dark"])) diff --git a/rust2rpm/utils.py b/rust2rpm/utils.py index 389501c..bc99dc7 100644 --- a/rust2rpm/utils.py +++ b/rust2rpm/utils.py @@ -1,6 +1,7 @@ import contextlib import glob import os +from pathlib import Path import re import sys import shlex @@ -40,7 +41,7 @@ def exit_on_common_errors(): sys.exit(str(e)) -def detect_editor(): +def detect_editor() -> str: terminal = os.getenv("TERM") terminal_is_dumb = not terminal or terminal == "dumb" editor = None @@ -55,7 +56,7 @@ def detect_editor(): return editor -def detect_packager(): +def detect_packager() -> Optional[str]: # If we're forcing the fallback... if os.getenv("RUST2RPM_NO_DETECT_PACKAGER"): return None @@ -77,7 +78,7 @@ def detect_packager(): return None -def guess_crate_name(): +def guess_crate_name() -> Optional[str]: """Guess crate name from directory name and/or spec file name If a spec file is present, we use the %crate variable. This is the best @@ -143,7 +144,7 @@ def package_name_compat(name: str, version: str): return package_name_suffixed(name, suffix) -def detect_rpmautospec(default_target, spec_file): +def detect_rpmautospec(default_target: str, spec_file: Path): """Guess whether %autorelease+%autochangelog should be used Returns False if we're not on Fedora or if the spec file exists and diff --git a/tox.ini b/tox.ini index 29ba8c4..c121073 100644 --- a/tox.ini +++ b/tox.ini @@ -33,6 +33,8 @@ setenv = deps = mypy pytest + types-requests + types-tqdm -rrequirements.txt commands = mypy -p rust2rpm {posargs}