add even more type annotations and start checking with mypy

This commit is contained in:
Fabio Valentini 2023-10-02 01:05:42 +02:00
parent eb448953a1
commit 11554c5053
No known key found for this signature in database
GPG key ID: 5AC5F572E5D410AF
6 changed files with 25 additions and 20 deletions

View file

@ -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

View file

@ -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:

View file

@ -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)

View file

@ -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"]))

View file

@ -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

View file

@ -33,6 +33,8 @@ setenv =
deps =
mypy
pytest
types-requests
types-tqdm
-rrequirements.txt
commands =
mypy -p rust2rpm {posargs}