From e0239251cb77205f7364ae691134f28f0dce4f0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 25 Aug 2023 08:53:31 +0200 Subject: [PATCH] ruff: enable upgrade checks --- nix_update/__init__.py | 4 ++-- nix_update/eval.py | 36 ++++++++++++++++----------------- nix_update/git.py | 7 ++----- nix_update/options.py | 11 +++++----- nix_update/update.py | 9 ++++----- nix_update/utils.py | 9 +++++---- nix_update/version/__init__.py | 19 ++++++++--------- nix_update/version/crate.py | 3 +-- nix_update/version/gitea.py | 5 ++--- nix_update/version/github.py | 5 ++--- nix_update/version/gitlab.py | 5 ++--- nix_update/version/pypi.py | 3 +-- nix_update/version/rubygems.py | 5 ++--- nix_update/version/savannah.py | 5 ++--- nix_update/version/sourcehut.py | 3 +-- nix_update/version/version.py | 5 ++--- pyproject.toml | 2 +- tests/conftest.py | 4 ++-- 18 files changed, 64 insertions(+), 76 deletions(-) diff --git a/nix_update/__init__.py b/nix_update/__init__.py index a52b607..0e85689 100644 --- a/nix_update/__init__.py +++ b/nix_update/__init__.py @@ -3,7 +3,7 @@ import os import shutil import sys import tempfile -from typing import NoReturn, Optional +from typing import NoReturn from .eval import CargoLockInSource, Package, eval_attr from .options import Options @@ -186,7 +186,7 @@ def write_commit_message(path: str, package: Package) -> None: f.write("\n") -def find_git_root(path: str) -> Optional[str]: +def find_git_root(path: str) -> str | None: prefix = [path] release_nix = [".git"] while True: diff --git a/nix_update/eval.py b/nix_update/eval.py index b673da2..1503a4e 100644 --- a/nix_update/eval.py +++ b/nix_update/eval.py @@ -2,7 +2,7 @@ import json import os from dataclasses import InitVar, dataclass, field from textwrap import dedent, indent -from typing import Any, Dict, List, Literal, Optional +from typing import Any, Literal from urllib.parse import ParseResult, urlparse from .errors import UpdateError @@ -43,32 +43,32 @@ class Package: old_version: str filename: str line: int - urls: Optional[List[str]] - url: Optional[str] - src_homepage: Optional[str] - changelog: Optional[str] + urls: list[str] | None + url: str | None + src_homepage: str | None + changelog: str | None rev: str - hash: Optional[str] - go_modules: Optional[str] - go_modules_old: Optional[str] - cargo_deps: Optional[str] - npm_deps: Optional[str] - tests: List[str] + hash: str | None + go_modules: str | None + go_modules_old: str | None + cargo_deps: str | None + npm_deps: str | None + tests: list[str] has_update_script: bool - raw_version_position: InitVar[Optional[Dict[str, Any]]] + raw_version_position: InitVar[dict[str, Any] | None] raw_cargo_lock: InitVar[Literal[False] | str | None] - parsed_url: Optional[ParseResult] = None - new_version: Optional[Version] = None - version_position: Optional[Position] = field(init=False) + parsed_url: ParseResult | None = None + new_version: Version | None = None + version_position: Position | None = field(init=False) cargo_lock: CargoLock = field(init=False) - diff_url: Optional[str] = None + diff_url: str | None = None def __post_init__( self, import_path: str, - raw_version_position: Optional[Dict[str, Any]], + raw_version_position: dict[str, Any] | None, raw_cargo_lock: Literal[False] | str | None, ) -> None: url = self.url or (self.urls[0] if self.urls else None) @@ -90,7 +90,7 @@ class Package: def eval_expression( - escaped_import_path: str, attr: str, flake: bool, system: Optional[str] + escaped_import_path: str, attr: str, flake: bool, system: str | None ) -> str: system = f'"{system}"' if system else "builtins.currentSystem" diff --git a/nix_update/git.py b/nix_update/git.py index 694fb70..4c02f79 100644 --- a/nix_update/git.py +++ b/nix_update/git.py @@ -1,12 +1,9 @@ import re -from typing import Optional from .utils import run -def old_version_from_diff( - diff: str, linenumber: int, new_version: str -) -> Optional[str]: +def old_version_from_diff(diff: str, linenumber: int, new_version: str) -> str | None: current_line = 0 old_str = None new_str = None @@ -37,7 +34,7 @@ def old_version_from_diff( def old_version_from_git( filename: str, linenumber: int, new_version: str -) -> Optional[str]: +) -> str | None: proc = run( ["git", "diff", "--color=never", "--word-diff=porcelain", "--", filename], ) diff --git a/nix_update/options.py b/nix_update/options.py index a1d2066..e1be59f 100644 --- a/nix_update/options.py +++ b/nix_update/options.py @@ -1,7 +1,6 @@ import json import os from dataclasses import dataclass, field -from typing import List, Optional from .version.version import VersionPreference @@ -14,19 +13,19 @@ class Options: version_preference: VersionPreference = VersionPreference.STABLE version_regex: str = "(.*)" import_path: str = os.getcwd() - override_filename: Optional[str] = None - url: Optional[str] = None + override_filename: str | None = None + url: str | None = None commit: bool = False use_update_script: bool = False - write_commit_message: Optional[str] = None + write_commit_message: str | None = None shell: bool = False run: bool = False build: bool = False test: bool = False review: bool = False format: bool = False - system: Optional[str] = None - extra_flags: List[str] = field(default_factory=list) + system: str | None = None + extra_flags: list[str] = field(default_factory=list) def __post_init__(self) -> None: self.escaped_attribute = ".".join(map(json.dumps, self.attribute.split("."))) diff --git a/nix_update/update.py b/nix_update/update.py index 72811ac..41e1dd2 100644 --- a/nix_update/update.py +++ b/nix_update/update.py @@ -9,7 +9,6 @@ import tomllib from concurrent.futures import ThreadPoolExecutor from os import path from pathlib import Path -from typing import Dict, Optional, Tuple from .errors import UpdateError from .eval import CargoLockInSource, CargoLockInStore, Package, eval_attr @@ -93,8 +92,8 @@ def get_package(opts: Options) -> str: def nix_prefetch(opts: Options, attr: str) -> str: expr = f"{get_package(opts)}.{attr}" - extra_env: Dict[str, str] = {} - tempdir: Optional[tempfile.TemporaryDirectory[str]] = None + extra_env: dict[str, str] = {} + tempdir: tempfile.TemporaryDirectory[str] | None = None stderr = "" if extra_env.get("XDG_RUNTIME_DIR") is None: tempdir = tempfile.TemporaryDirectory() @@ -135,7 +134,7 @@ def disable_check_meta(opts: Options) -> str: return f'(if (builtins.hasAttr "config" (builtins.functionArgs (import {opts.escaped_import_path}))) then {{ config.checkMeta = false; overlays = []; }} else {{ }})' -def git_prefetch(x: Tuple[str, Tuple[str, str]]) -> Tuple[str, str]: +def git_prefetch(x: tuple[str, tuple[str, str]]) -> tuple[str, str]: rev, (key, url) = x res = run(["nix-prefetch-git", url, rev, "--fetch-submodules"]) return key, to_sri(json.loads(res.stdout)["sha256"]) @@ -244,7 +243,7 @@ def update_cargo_lock( print(line, end="") -def print_hashes(hashes: Dict[str, str], indent: str) -> None: +def print_hashes(hashes: dict[str, str], indent: str) -> None: if not hashes: return print(f"{indent}outputHashes = {{") diff --git a/nix_update/utils.py b/nix_update/utils.py index ba8bbe9..eab82e2 100644 --- a/nix_update/utils.py +++ b/nix_update/utils.py @@ -1,8 +1,9 @@ import os import subprocess import sys +from collections.abc import Callable from pathlib import Path -from typing import IO, Any, Callable, Dict, List, Optional +from typing import IO, Any HAS_TTY = sys.stdout.isatty() ROOT = Path(os.path.dirname(os.path.realpath(__file__))) @@ -23,12 +24,12 @@ info = color_text(32) def run( - command: List[str], - cwd: Optional[Path | str] = None, + command: list[str], + cwd: Path | str | None = None, stdout: None | int | IO[Any] = subprocess.PIPE, stderr: None | int | IO[Any] = None, check: bool = True, - extra_env: Dict[str, str] = {}, + extra_env: dict[str, str] = {}, ) -> "subprocess.CompletedProcess[str]": info("$ " + " ".join(command)) env = os.environ.copy() diff --git a/nix_update/version/__init__.py b/nix_update/version/__init__.py index 3e93276..458ffec 100644 --- a/nix_update/version/__init__.py +++ b/nix_update/version/__init__.py @@ -1,6 +1,7 @@ import re +from collections.abc import Callable from functools import partial -from typing import Callable, List, Optional, Protocol +from typing import Protocol from urllib.parse import ParseResult from ..errors import VersionError @@ -25,11 +26,11 @@ from .version import Version, VersionPreference class SnapshotFetcher(Protocol): - def __call__(self, url: ParseResult, branch: str) -> List[Version]: + def __call__(self, url: ParseResult, branch: str) -> list[Version]: ... -fetchers: List[Callable[[ParseResult], List[Version]]] = [ +fetchers: list[Callable[[ParseResult], list[Version]]] = [ fetch_crate_versions, fetch_pypi_versions, fetch_gitea_versions, @@ -40,14 +41,14 @@ fetchers: List[Callable[[ParseResult], List[Version]]] = [ fetch_sourcehut_versions, ] -branch_snapshots_fetchers: List[SnapshotFetcher] = [ +branch_snapshots_fetchers: list[SnapshotFetcher] = [ fetch_gitea_snapshots, fetch_github_snapshots, fetch_gitlab_snapshots, ] -def extract_version(version: Version, version_regex: str) -> Optional[Version]: +def extract_version(version: Version, version_regex: str) -> Version | None: pattern = re.compile(version_regex) match = re.match(pattern, version.number) if match is not None: @@ -73,12 +74,12 @@ def fetch_latest_version( url: ParseResult, preference: VersionPreference, version_regex: str, - branch: Optional[str] = None, - old_rev: Optional[str] = None, + branch: str | None = None, + old_rev: str | None = None, version_prefix: str = "", ) -> Version: - unstable: List[str] = [] - filtered: List[str] = [] + unstable: list[str] = [] + filtered: list[str] = [] used_fetchers = fetchers if preference == VersionPreference.BRANCH: used_fetchers = [partial(f, branch=branch) for f in branch_snapshots_fetchers] diff --git a/nix_update/version/crate.py b/nix_update/version/crate.py index e02b3f9..1a312ea 100644 --- a/nix_update/version/crate.py +++ b/nix_update/version/crate.py @@ -1,13 +1,12 @@ import json import urllib.request -from typing import List from urllib.parse import ParseResult from ..utils import info from .version import Version -def fetch_crate_versions(url: ParseResult) -> List[Version]: +def fetch_crate_versions(url: ParseResult) -> list[Version]: if url.netloc != "crates.io": return [] parts = url.path.split("/") diff --git a/nix_update/version/gitea.py b/nix_update/version/gitea.py index 70b20f9..c826cef 100644 --- a/nix_update/version/gitea.py +++ b/nix_update/version/gitea.py @@ -1,12 +1,11 @@ import json -from typing import List from urllib.parse import ParseResult from urllib.request import urlopen from .version import Version -def fetch_gitea_versions(url: ParseResult) -> List[Version]: +def fetch_gitea_versions(url: ParseResult) -> list[Version]: if url.netloc not in ["codeberg.org", "gitea.com", "notabug.org"]: return [] @@ -17,7 +16,7 @@ def fetch_gitea_versions(url: ParseResult) -> List[Version]: return [Version(tag["name"]) for tag in tags] -def fetch_gitea_snapshots(url: ParseResult, branch: str) -> List[Version]: +def fetch_gitea_snapshots(url: ParseResult, branch: str) -> list[Version]: if url.netloc not in ["codeberg.org", "gitea.com", "notabug.org"]: return [] diff --git a/nix_update/version/github.py b/nix_update/version/github.py index d1143de..fd94f97 100644 --- a/nix_update/version/github.py +++ b/nix_update/version/github.py @@ -1,7 +1,6 @@ import re import urllib.request import xml.etree.ElementTree as ET -from typing import List from urllib.parse import ParseResult, urlparse from xml.etree.ElementTree import Element @@ -21,7 +20,7 @@ def version_from_entry(entry: Element) -> Version: return Version(url.path.split("/")[-1]) -def fetch_github_versions(url: ParseResult) -> List[Version]: +def fetch_github_versions(url: ParseResult) -> list[Version]: if url.netloc != "github.com": return [] parts = url.path.split("/") @@ -36,7 +35,7 @@ def fetch_github_versions(url: ParseResult) -> List[Version]: return [version_from_entry(x) for x in releases] -def fetch_github_snapshots(url: ParseResult, branch: str) -> List[Version]: +def fetch_github_snapshots(url: ParseResult, branch: str) -> list[Version]: if url.netloc != "github.com": return [] parts = url.path.split("/") diff --git a/nix_update/version/gitlab.py b/nix_update/version/gitlab.py index e80d718..9ad55a9 100644 --- a/nix_update/version/gitlab.py +++ b/nix_update/version/gitlab.py @@ -2,7 +2,6 @@ import json import re import urllib.request from datetime import datetime -from typing import List from urllib.parse import ParseResult, quote_plus from ..errors import VersionError @@ -14,7 +13,7 @@ GITLAB_API = re.compile( ) -def fetch_gitlab_versions(url: ParseResult) -> List[Version]: +def fetch_gitlab_versions(url: ParseResult) -> list[Version]: match = GITLAB_API.match(url.geturl()) if not match: return [] @@ -42,7 +41,7 @@ def fetch_gitlab_versions(url: ParseResult) -> List[Version]: return releases -def fetch_gitlab_snapshots(url: ParseResult, branch: str) -> List[Version]: +def fetch_gitlab_snapshots(url: ParseResult, branch: str) -> list[Version]: match = GITLAB_API.match(url.geturl()) if not match: return [] diff --git a/nix_update/version/pypi.py b/nix_update/version/pypi.py index 0138409..3463833 100644 --- a/nix_update/version/pypi.py +++ b/nix_update/version/pypi.py @@ -1,13 +1,12 @@ import json import urllib.request -from typing import List from urllib.parse import ParseResult from ..utils import info from .version import Version -def fetch_pypi_versions(url: ParseResult) -> List[Version]: +def fetch_pypi_versions(url: ParseResult) -> list[Version]: if url.netloc != "pypi": return [] parts = url.path.split("/") diff --git a/nix_update/version/rubygems.py b/nix_update/version/rubygems.py index 5812e7f..9da358e 100644 --- a/nix_update/version/rubygems.py +++ b/nix_update/version/rubygems.py @@ -1,6 +1,5 @@ import json import urllib.request -from typing import List from urllib.parse import ParseResult from ..errors import VersionError @@ -8,7 +7,7 @@ from ..utils import info from .version import Version -def fetch_rubygem_versions(url: ParseResult) -> List[Version]: +def fetch_rubygem_versions(url: ParseResult) -> list[Version]: if url.netloc != "rubygems.org": return [] parts = url.path.split("/") @@ -21,7 +20,7 @@ def fetch_rubygem_versions(url: ParseResult) -> List[Version]: if len(json_versions) == 0: raise VersionError("No versions found") - versions: List[Version] = [] + versions: list[Version] = [] for version in json_versions: number = version["number"] assert isinstance(number, str) diff --git a/nix_update/version/savannah.py b/nix_update/version/savannah.py index 5b364da..8aa330f 100644 --- a/nix_update/version/savannah.py +++ b/nix_update/version/savannah.py @@ -1,7 +1,6 @@ import re import urllib.request import xml.etree.ElementTree as ET -from typing import List, Optional from urllib.parse import ParseResult, urljoin, urlparse from xml.etree.ElementTree import Element @@ -11,7 +10,7 @@ from .version import Version filename_regex = re.compile(r"-(\d+(?:\.\d+)*(?:-[^-.]+)?)\.tar\.[^.]+$") -def version_from_link(a: Element, baseurl: str) -> Optional[Version]: +def version_from_link(a: Element, baseurl: str) -> Version | None: try: href = a.attrib["href"] except KeyError: @@ -23,7 +22,7 @@ def version_from_link(a: Element, baseurl: str) -> Optional[Version]: return Version(m[1]) -def fetch_savannah_versions(url: ParseResult) -> List[Version]: +def fetch_savannah_versions(url: ParseResult) -> list[Version]: if url.scheme != "mirror" or url.netloc != "savannah": return [] pname = url.path.split("/", 2)[1] diff --git a/nix_update/version/sourcehut.py b/nix_update/version/sourcehut.py index 1fb367e..ceeb023 100644 --- a/nix_update/version/sourcehut.py +++ b/nix_update/version/sourcehut.py @@ -1,6 +1,5 @@ import urllib.request import xml.etree.ElementTree as ET -from typing import List from urllib.parse import ParseResult, urlparse from xml.etree.ElementTree import Element @@ -18,7 +17,7 @@ def version_from_entry(entry: Element) -> Version: return Version(url.path.split("/")[-1]) -def fetch_sourcehut_versions(url: ParseResult) -> List[Version]: +def fetch_sourcehut_versions(url: ParseResult) -> list[Version]: if url.netloc != "git.sr.ht": return [] parts = url.path.split("/") diff --git a/nix_update/version/version.py b/nix_update/version/version.py index 8136948..74eff09 100644 --- a/nix_update/version/version.py +++ b/nix_update/version/version.py @@ -1,13 +1,12 @@ from dataclasses import dataclass from enum import Enum, auto -from typing import Optional @dataclass class Version: number: str - prerelease: Optional[bool] = None - rev: Optional[str] = None + prerelease: bool | None = None + rev: str | None = None class VersionPreference(Enum): diff --git a/pyproject.toml b/pyproject.toml index b013714..321e975 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ target-version = "py311" line-length = 88 -select = ["E", "F", "I"] +select = ["E", "F", "I", "U"] ignore = [ "E501" ] [tool.mypy] diff --git a/tests/conftest.py b/tests/conftest.py index e2c11a6..0229bb0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,9 +3,9 @@ import shutil import subprocess import sys import tempfile +from collections.abc import Iterator from contextlib import contextmanager from pathlib import Path -from typing import Iterator, Type import pytest @@ -41,5 +41,5 @@ class Helpers: @pytest.fixture # type: ignore -def helpers() -> Type[Helpers]: +def helpers() -> type[Helpers]: return Helpers