From d1a7029cd4148a30a6dea3bb4adcdba6f7becec2 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Mon, 4 Nov 2024 07:01:22 -0500 Subject: [PATCH] chore: pyupgrade --py39-plus --- src/scriv/config.py | 2 +- src/scriv/format.py | 2 +- src/scriv/github.py | 13 +++++++------ src/scriv/gitinfo.py | 2 +- src/scriv/linkcheck.py | 2 +- src/scriv/literals.py | 3 ++- src/scriv/scriv.py | 7 ++++--- src/scriv/shell.py | 6 +++--- src/scriv/util.py | 11 ++++++----- tests/conftest.py | 2 +- tests/faker.py | 15 ++++++++------- tests/helpers.py | 2 +- tests/test_ghrel.py | 4 ++-- 13 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/scriv/config.py b/src/scriv/config.py index 0416c56..fa9e421 100644 --- a/src/scriv/config.py +++ b/src/scriv/config.py @@ -434,7 +434,7 @@ def read_file_value(self, file_name: str) -> str: return value -def convert_list(val: str) -> List[str]: +def convert_list(val: str) -> list[str]: """ Convert a string value from a config into a list of strings. diff --git a/src/scriv/format.py b/src/scriv/format.py index 714a0c1..cc23a72 100644 --- a/src/scriv/format.py +++ b/src/scriv/format.py @@ -8,7 +8,7 @@ # When collecting changelog fragments, we group them by their category into # Sections. A SectionDict maps category names to a list of the paragraphs in # that section. For projects not using categories, the key will be None. -SectionDict = Dict[Optional[str], List[str]] +SectionDict = dict[Optional[str], list[str]] class FormatTools(abc.ABC): diff --git a/src/scriv/github.py b/src/scriv/github.py index d76ac95..7499234 100644 --- a/src/scriv/github.py +++ b/src/scriv/github.py @@ -2,7 +2,8 @@ import logging import os -from typing import Any, Dict, Iterable +from typing import Any, Dict +from collections.abc import Iterable import requests @@ -24,7 +25,7 @@ def check_ok(resp): resp.raise_for_status() -def auth_headers() -> Dict[str, str]: +def auth_headers() -> dict[str, str]: """ Get the authorization headers needed for GitHub. @@ -37,7 +38,7 @@ def auth_headers() -> Dict[str, str]: return headers -def github_paginated(url: str) -> Iterable[Dict[str, Any]]: +def github_paginated(url: str) -> Iterable[dict[str, Any]]: """ Get all the results from a paginated GitHub url. """ @@ -54,7 +55,7 @@ def github_paginated(url: str) -> Iterable[Dict[str, Any]]: RELEASES_URL = "https://api.github.com/repos/{repo}/releases" -def get_releases(repo: str) -> Dict[str, Dict[str, Any]]: +def get_releases(repo: str) -> dict[str, dict[str, Any]]: """ Get all the releases from a name/project repo. @@ -66,7 +67,7 @@ def get_releases(repo: str) -> Dict[str, Dict[str, Any]]: return releases -def create_release(repo: str, release_data: Dict[str, Any]) -> None: +def create_release(repo: str, release_data: dict[str, Any]) -> None: """ Create a GitHub release. @@ -89,7 +90,7 @@ def create_release(repo: str, release_data: Dict[str, Any]) -> None: def update_release( - release: Dict[str, Any], release_data: Dict[str, Any] + release: dict[str, Any], release_data: dict[str, Any] ) -> None: """ Update a GitHub release. diff --git a/src/scriv/gitinfo.py b/src/scriv/gitinfo.py index 74bce54..93496e1 100644 --- a/src/scriv/gitinfo.py +++ b/src/scriv/gitinfo.py @@ -82,7 +82,7 @@ def git_rm(filename: Path) -> None: sys.exit(ret) -def get_github_repos() -> Set[str]: +def get_github_repos() -> set[str]: """ Find the GitHub name/repos for this project. diff --git a/src/scriv/linkcheck.py b/src/scriv/linkcheck.py index 4cd12a9..6d82524 100644 --- a/src/scriv/linkcheck.py +++ b/src/scriv/linkcheck.py @@ -2,7 +2,7 @@ import concurrent.futures import logging -from typing import Iterable +from collections.abc import Iterable import markdown_it import requests diff --git a/src/scriv/literals.py b/src/scriv/literals.py index ac4289d..09e55a1 100644 --- a/src/scriv/literals.py +++ b/src/scriv/literals.py @@ -5,7 +5,8 @@ import ast import configparser import os.path -from typing import Any, MutableMapping, Optional +from typing import Any, Optional +from collections.abc import MutableMapping from .exceptions import ScrivException from .optional import tomllib, yaml diff --git a/src/scriv/scriv.py b/src/scriv/scriv.py index 5665cf4..023a9d4 100644 --- a/src/scriv/scriv.py +++ b/src/scriv/scriv.py @@ -6,7 +6,8 @@ import re import textwrap from pathlib import Path -from typing import Iterable, List, Optional +from typing import List, Optional +from collections.abc import Iterable import jinja2 @@ -37,7 +38,7 @@ def new_fragment(self) -> Fragment: content=_new_fragment_content(self.config), ) - def fragments_to_combine(self) -> List[Fragment]: + def fragments_to_combine(self) -> list[Fragment]: """Get the list of fragments to combine.""" return [Fragment(path=path) for path in _files_to_combine(self.config)] @@ -94,7 +95,7 @@ def _new_fragment_content(config: Config) -> str: ).render(config=config) -def _files_to_combine(config: Config) -> List[Path]: +def _files_to_combine(config: Config) -> list[Path]: """ Find all the fragment file paths to be combined. diff --git a/src/scriv/shell.py b/src/scriv/shell.py index 7edd605..d54aec2 100644 --- a/src/scriv/shell.py +++ b/src/scriv/shell.py @@ -6,12 +6,12 @@ from typing import List, Tuple, Union # The return value of run_command. -CmdResult = Tuple[bool, str] +CmdResult = tuple[bool, str] logger = logging.getLogger(__name__) -def run_command(cmd: Union[str, List[str]]) -> CmdResult: +def run_command(cmd: Union[str, list[str]]) -> CmdResult: """ Run a command line (with no shell). @@ -38,7 +38,7 @@ def run_command(cmd: Union[str, List[str]]) -> CmdResult: return proc.returncode == 0, output -def run_simple_command(cmd: Union[str, List[str]]) -> str: +def run_simple_command(cmd: Union[str, list[str]]) -> str: """ Run a command and return its output, or "" if it fails. """ diff --git a/src/scriv/util.py b/src/scriv/util.py index 89d2251..c65720d 100644 --- a/src/scriv/util.py +++ b/src/scriv/util.py @@ -7,7 +7,8 @@ import logging import re import sys -from typing import Dict, Optional, Sequence, Tuple, TypeVar +from typing import Dict, Optional, Tuple, TypeVar +from collections.abc import Sequence import click_log @@ -18,8 +19,8 @@ def order_dict( - d: Dict[Optional[K], T], keys: Sequence[Optional[K]] -) -> Dict[Optional[K], T]: + d: dict[K | None, T], keys: Sequence[K | None] +) -> dict[K | None, T]: """ Produce an OrderedDict of `d`, but with the keys in `keys` order. @@ -40,7 +41,7 @@ def order_dict( return with_order -def partition_lines(text: str, marker: str) -> Tuple[str, str, str]: +def partition_lines(text: str, marker: str) -> tuple[str, str, str]: """ Split `text` by lines, similar to str.partition. @@ -102,7 +103,7 @@ def __hash__(self): return hash(self.vtext.lstrip("v")) @classmethod - def from_text(cls, text: str) -> Optional[Version]: + def from_text(cls, text: str) -> Version | None: """Find a version number in a text string.""" m = re.search(VERSION_REGEX, text) if m: diff --git a/tests/conftest.py b/tests/conftest.py index 18f3830..a9ae872 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,7 +4,7 @@ import sys import traceback from pathlib import Path -from typing import Iterable +from collections.abc import Iterable import pytest import responses diff --git a/tests/faker.py b/tests/faker.py index b0657a1..df12ed0 100644 --- a/tests/faker.py +++ b/tests/faker.py @@ -1,12 +1,13 @@ """Fake implementations of some of our external information sources.""" import shlex -from typing import Callable, Dict, Iterable, List, Optional, Set, Tuple +from typing import Callable, Dict, List, Optional, Set, Tuple +from collections.abc import Iterable from scriv.shell import CmdResult # A function that simulates run_command. -CmdHandler = Callable[[List[str]], CmdResult] +CmdHandler = Callable[[list[str]], CmdResult] class FakeRunCommand: @@ -18,7 +19,7 @@ class FakeRunCommand: def __init__(self, mocker): """Make the faker.""" - self.handlers: Dict[str, CmdHandler] = {} + self.handlers: dict[str, CmdHandler] = {} self.mocker = mocker self.patch_module("scriv.shell") @@ -50,19 +51,19 @@ class FakeGit: def __init__(self, frc: FakeRunCommand) -> None: """Make a FakeGit from a FakeRunCommand.""" # Initialize with basic defaults. - self.config: Dict[str, str] = { + self.config: dict[str, str] = { "core.bare": "false", "core.repositoryformatversion": "0", } self.branch = "main" self.editor = "vi" - self.tags: Set[str] = set() - self.remotes: Dict[str, Tuple[str, str]] = {} + self.tags: set[str] = set() + self.remotes: dict[str, tuple[str, str]] = {} # Hook up our run_command handler. frc.add_handler("git", self.run_command) - def run_command(self, argv: List[str]) -> CmdResult: + def run_command(self, argv: list[str]) -> CmdResult: """Simulate git commands.""" # todo: match/case someday if argv[1] == "config": diff --git a/tests/helpers.py b/tests/helpers.py index 6499cbb..83b7344 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -27,6 +27,6 @@ def check_logs(caplog, expected): Only caplog records from a logger mentioned in expected are considered. """ - logger_names = set(r[0] for r in expected) + logger_names = {r[0] for r in expected} records = [r for r in caplog.record_tuples if r[0] in logger_names] assert records == expected diff --git a/tests/test_ghrel.py b/tests/test_ghrel.py index 965e485..7417f24 100644 --- a/tests/test_ghrel.py +++ b/tests/test_ghrel.py @@ -98,7 +98,7 @@ def scenario1(temp_dir, fake_git, mocker): def mock_create_release(mocker): """Create a mock create_release that checks arguments.""" - def _create_release(repo: str, release_data: Dict[str, Any]) -> None: + def _create_release(repo: str, release_data: dict[str, Any]) -> None: assert repo assert release_data["name"] assert json.dumps(release_data)[0] == "{" @@ -113,7 +113,7 @@ def mock_update_release(mocker): """Create a mock update_release that checks arguments.""" def _update_release( - release: Dict[str, Any], release_data: Dict[str, Any] + release: dict[str, Any], release_data: dict[str, Any] ) -> None: assert release_data["name"] assert release["url"]