Enable flake8-annotations linting rules (#267)

See https://docs.astral.sh/ruff/rules/#flake8-annotations-ann.
This commit is contained in:
Marc Wrobel
2023-12-30 10:38:17 +01:00
parent 0e8fe135e4
commit f49e3dff15
12 changed files with 51 additions and 47 deletions

View File

@@ -2,25 +2,25 @@ import calendar
from datetime import datetime, timezone
def parse_date(text, formats=frozenset([
"%B %d %Y", # January 1 2020
"%b %d %Y", # Jan 1 2020
"%d %B %Y", # 1 January 2020
"%d %b %Y", # 1 Jan 2020
"%d-%b-%Y", # 1-Jan-2020
"%d-%B-%Y", # 1-January-2020
"%Y-%m-%d", # 2020-01-01
"%m/%d/%Y", # 01/25/2020
def parse_date(text: str, formats: list[str] = frozenset([
"%B %d %Y", # January 1 2020
"%b %d %Y", # Jan 1 2020
"%d %B %Y", # 1 January 2020
"%d %b %Y", # 1 Jan 2020
"%d-%b-%Y", # 1-Jan-2020
"%d-%B-%Y", # 1-January-2020
"%Y-%m-%d", # 2020-01-01
"%m/%d/%Y", # 01/25/2020
])) -> datetime:
"""Parse a given text representing a date using a list of formats.
"""
return parse_datetime(text, formats, to_utc=False)
def parse_month_year_date(text, formats=frozenset([
"%B %Y", # January 2020
"%b %Y", # Jan 2020
])) -> datetime:
def parse_month_year_date(text: str, formats: list[str] = frozenset([
"%B %Y", # January 2020
"%b %Y", # Jan 2020
])) -> datetime:
"""Parse a given text representing a partial date using a list of formats,
adjusting it to the last day of the month.
"""
@@ -29,14 +29,14 @@ def parse_month_year_date(text, formats=frozenset([
return date.replace(day=last_day)
def parse_datetime(text, formats=frozenset([
def parse_datetime(text: str, formats: list[str] = frozenset([
"%Y-%m-%d %H:%M:%S", # 2023-05-01 08:32:34
"%Y-%m-%dT%H:%M:%S", # 2023-05-01T08:32:34
"%Y-%m-%d %H:%M:%S %z", # 2023-05-01 08:32:34 +0900
"%Y-%m-%dT%H:%M:%S%z", # 2023-05-01T08:32:34+0900
"%Y-%m-%dT%H:%M:%S.%f%z", # 2023-05-01T08:32:34.123456Z
"%a %d %b %Y %H:%M:%S %Z", # Wed, 01 Jan 2020 00:00:00 GMT
]), to_utc=True) -> datetime:
]), to_utc: bool = True) -> datetime:
"""Parse a given text representing a datetime using a list of formats,
optionally converting it to UTC.
"""

View File

@@ -21,7 +21,7 @@ VERSIONS_PATH = os.environ.get("VERSIONS_PATH", "releases")
class AutoConfig:
def __init__(self, method: str, config: dict):
def __init__(self, method: str, config: dict) -> None:
self.method = method
self.url = config[method]
self.version_template = Template(config.get("template", DEFAULT_VERSION_TEMPLATE))
@@ -41,7 +41,7 @@ class AutoConfig:
class ProductFrontmatter:
def __init__(self, name: str):
def __init__(self, name: str) -> None:
self.name: str = name
self.path: str = f"{PRODUCTS_PATH}/{name}.md"
@@ -72,13 +72,13 @@ class ProductFrontmatter:
class Product:
def __init__(self, name: str):
def __init__(self, name: str) -> None:
self.name: str = name
self.versions_path: str = f"{VERSIONS_PATH}/{name}.json"
self.versions = {}
@staticmethod
def from_file(name: str):
def from_file(name: str) -> "Product":
product = Product(name)
if not os.path.isfile(product.versions_path):
@@ -137,7 +137,7 @@ class Product:
return f"<{self.name}>"
def list_products(method, products_filter=None) -> list[str]:
def list_products(method: str, products_filter: str = None) -> list[str]:
"""Return a list of products that are using the same given update method.
"""
products = []

View File

@@ -8,7 +8,7 @@ class Git:
"""Git cli wrapper
"""
def __init__(self, url: str):
def __init__(self, url: str) -> None:
self.url: str = url
self.repo_dir: Path = Path(f"~/.cache/git/{sha1(url.encode()).hexdigest()}").expanduser()
@@ -22,7 +22,7 @@ class Git:
except ChildProcessError as ex:
raise RuntimeError(f"Failed to run '{cmd}': {ex}") from ex
def setup(self, bare: bool = False):
def setup(self, bare: bool = False) -> None:
"""Creates the repository path and runs:
git init
git remote add origin $url
@@ -34,7 +34,7 @@ class Git:
self._run(f"remote add origin {self.url}")
# See https://stackoverflow.com/a/65746233/374236
def list_tags(self):
def list_tags(self) -> list[tuple[str, str]]:
"""Fetch and return tags matching the given`pattern`"""
# See https://stackoverflow.com/a/65746233/374236
self._run("config --local extensions.partialClone true")
@@ -44,7 +44,7 @@ class Git:
tags_with_date = self._run("tag --list --format='%(refname:strip=2) %(creatordate:short)'")
return [tag_with_date.split(" ") for tag_with_date in tags_with_date]
def list_branches(self, pattern: str):
def list_branches(self, pattern: str) -> list[str]:
"""Uses ls-remote to fetch the branch names
`pattern` uses fnmatch style globbing
"""
@@ -56,7 +56,7 @@ class Git:
return [line.split("\t")[1][11:] for line in lines if "\t" in line]
def checkout(self, branch: str, file_list: list[str] = None):
def checkout(self, branch: str, file_list: list[str] = None) -> None:
"""Checks out a branch
If `file_list` is given, sparse-checkout is used to save bandwidth
and only download the given files

View File

@@ -38,6 +38,6 @@ def fetch_urls(urls: list[str], data: any = None, headers: dict[str, str] = None
return fetch_urls(urls, data, headers, next_max_retries, backoff_factor, timeout)
def fetch_url(url, data: any = None, headers: dict[str, str] = None,
def fetch_url(url: str, data: any = None, headers: dict[str, str] = None,
max_retries: int = 10, backoff_factor: float = 0.5, timeout: int = 30) -> Response:
return fetch_urls([url], data, headers, max_retries, backoff_factor, timeout)[0]