Skip to content

Commit

Permalink
ruff: enable upgrade checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mic92 committed Aug 25, 2023
1 parent 4f2d6c4 commit b4ea576
Show file tree
Hide file tree
Showing 18 changed files with 64 additions and 74 deletions.
4 changes: 2 additions & 2 deletions nix_update/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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
Expand Down Expand Up @@ -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:
Expand Down
36 changes: 18 additions & 18 deletions nix_update/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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
Expand Down Expand Up @@ -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)
Expand All @@ -90,7 +90,7 @@ def __post_init__(


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"

Expand Down
5 changes: 2 additions & 3 deletions nix_update/git.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import re
from typing import Optional

from .utils import run


def old_version_from_diff(
diff: str, linenumber: int, new_version: str
) -> Optional[str]:
) -> str | None:
current_line = 0
old_str = None
new_str = None
Expand Down Expand Up @@ -37,7 +36,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],
)
Expand Down
11 changes: 5 additions & 6 deletions nix_update/options.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import os
from dataclasses import dataclass, field
from typing import List, Optional

from .version.version import VersionPreference

Expand All @@ -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(".")))
Expand Down
9 changes: 4 additions & 5 deletions nix_update/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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 = {{")
Expand Down
9 changes: 5 additions & 4 deletions nix_update/utils.py
Original file line number Diff line number Diff line change
@@ -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__)))
Expand All @@ -23,12 +24,12 @@ def wrapper(text: str) -> None:


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()
Expand Down
19 changes: 10 additions & 9 deletions nix_update/version/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,11 +26,11 @@


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,
Expand All @@ -40,14 +41,14 @@ def __call__(self, url: ParseResult, branch: str) -> 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:
Expand All @@ -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]
Expand Down
3 changes: 1 addition & 2 deletions nix_update/version/crate.py
Original file line number Diff line number Diff line change
@@ -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("/")
Expand Down
5 changes: 2 additions & 3 deletions nix_update/version/gitea.py
Original file line number Diff line number Diff line change
@@ -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 []

Expand All @@ -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 []

Expand Down
5 changes: 2 additions & 3 deletions nix_update/version/github.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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("/")
Expand All @@ -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("/")
Expand Down
5 changes: 2 additions & 3 deletions nix_update/version/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import re
import urllib.request
from datetime import datetime
from typing import List
from urllib.parse import ParseResult, quote_plus

from ..errors import VersionError
Expand All @@ -14,7 +13,7 @@
)


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 []
Expand Down Expand Up @@ -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 []
Expand Down
3 changes: 1 addition & 2 deletions nix_update/version/pypi.py
Original file line number Diff line number Diff line change
@@ -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("/")
Expand Down
5 changes: 2 additions & 3 deletions nix_update/version/rubygems.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import json
import urllib.request
from typing import List
from urllib.parse import ParseResult

from ..errors import VersionError
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("/")
Expand All @@ -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)
Expand Down
Loading

0 comments on commit b4ea576

Please sign in to comment.