Skip to content

Commit

Permalink
MAINT: upgrade Python syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Dec 3, 2023
1 parent f0dbd9b commit ffc6e30
Show file tree
Hide file tree
Showing 29 changed files with 256 additions and 191 deletions.
5 changes: 3 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
documentation: https://www.sphinx-doc.org/en/master/usage/configuration.html
"""

from __future__ import annotations

import contextlib
import os
import shutil
import subprocess
import sys
from typing import Optional

import requests

Expand Down Expand Up @@ -46,7 +47,7 @@ def generate_api(package: str) -> None:
)


def get_html_logo_path() -> Optional[str]:
def get_html_logo_path() -> str | None:
logo_path = "_static/logo.svg"
os.makedirs(os.path.dirname(logo_path), exist_ok=True)
with contextlib.suppress(requests.exceptions.ConnectionError):
Expand Down
8 changes: 5 additions & 3 deletions src/repoma/check_dev_files/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""A collection of scripts that check the file structure of a repository."""

from __future__ import annotations

import sys
from argparse import ArgumentParser
from typing import List, Optional, Sequence
from typing import Sequence

from repoma.check_dev_files.deprecated import remove_deprecated_tools
from repoma.utilities.executor import Executor
Expand Down Expand Up @@ -33,7 +35,7 @@
)


def main(argv: Optional[Sequence[str]] = None) -> int:
def main(argv: Sequence[str] | None = None) -> int:
parser = _create_argparse()
args = parser.parse_args(argv)
is_python_repo = not args.no_python
Expand Down Expand Up @@ -243,7 +245,7 @@ def _create_argparse() -> ArgumentParser:
return parser


def _to_list(arg: str) -> List[str]:
def _to_list(arg: str) -> list[str]:
"""Create a comma-separated list from a string argument.
>>> _to_list('a c , test,b')
Expand Down
13 changes: 7 additions & 6 deletions src/repoma/check_dev_files/citation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Check citation files."""

from __future__ import annotations

import json
import os
from textwrap import dedent
from typing import Dict, List, Optional, Set

from html2text import HTML2Text
from ruamel.yaml import YAML
Expand Down Expand Up @@ -112,8 +113,8 @@ def _write_citation_cff(citation_cff: CommentedMap) -> None:
yaml.dump(citation_cff, stream)


def _get_authors(zenodo: dict) -> Optional[List[Dict[str, str]]]:
creators: Optional[List[Dict[str, str]]] = zenodo.get("creators")
def _get_authors(zenodo: dict) -> list[dict[str, str]] | None:
creators: list[dict[str, str]] | None = zenodo.get("creators")

Check warning on line 117 in src/repoma/check_dev_files/citation.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/citation.py#L117

Added line #L117 was not covered by tests
if creators is None:
return None
return [__convert_author(item) for item in creators]
Expand All @@ -132,10 +133,10 @@ def __convert_author(creator: dict) -> dict:
"family-names": family_name.strip(),
"given-names": given_names.strip(),
}
affiliation: Optional[str] = creator.get("affiliation")
affiliation: str | None = creator.get("affiliation")

Check warning on line 136 in src/repoma/check_dev_files/citation.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/citation.py#L136

Added line #L136 was not covered by tests
if affiliation is not None:
author_info["affiliation"] = affiliation
orcid: Optional[str] = creator.get("orcid")
orcid: str | None = creator.get("orcid")

Check warning on line 139 in src/repoma/check_dev_files/citation.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/citation.py#L139

Added line #L139 was not covered by tests
if orcid is not None:
author_info["orcid"] = f"https://orcid.org/{orcid}"
return author_info
Expand All @@ -160,7 +161,7 @@ def check_citation_keys() -> None:
if not citation_cff:
msg = f"{CONFIG_PATH.citation} is empty"
raise PrecommitError(msg)
existing: Set[str] = set(citation_cff)
existing: set[str] = set(citation_cff)

Check warning on line 164 in src/repoma/check_dev_files/citation.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/citation.py#L164

Added line #L164 was not covered by tests
missing_keys = expected - existing
if missing_keys:
sorted_keys = sorted(missing_keys)
Expand Down
12 changes: 8 additions & 4 deletions src/repoma/check_dev_files/cspell.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
<https://github.com/streetsidesoftware/cspell/tree/master/packages/cspell>`_.
"""

from __future__ import annotations

import json
import os
from glob import glob
from pathlib import Path
from typing import Any, Iterable, List, Sequence, Union
from typing import TYPE_CHECKING, Any, Iterable, Sequence

from ruamel.yaml.comments import CommentedMap

Expand All @@ -23,6 +24,9 @@
from repoma.utilities.readme import add_badge, remove_badge
from repoma.utilities.vscode import sort_case_insensitive

if TYPE_CHECKING:
from pathlib import Path

Check warning on line 28 in src/repoma/check_dev_files/cspell.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/cspell.py#L28

Added line #L28 was not covered by tests

__VSCODE_EXTENSION_NAME = "streetsidesoftware.code-spell-checker"

# cspell:ignore pelling
Expand Down Expand Up @@ -202,7 +206,7 @@ def __express_list_of_sections(sections: Sequence[str]) -> str:
return sentence


def __get_config(path: Union[str, Path]) -> dict:
def __get_config(path: str | Path) -> dict:
with open(path) as stream:
return json.load(stream)

Expand All @@ -213,7 +217,7 @@ def __write_config(config: dict) -> None:
stream.write("\n")


def __sort_section(content: Iterable[Any], section_name: str) -> List[str]:
def __sort_section(content: Iterable[Any], section_name: str) -> list[str]:
"""Sort a list section.
>>> __sort_section({"one", "Two"}, section_name="words")
Expand Down
5 changes: 3 additions & 2 deletions src/repoma/check_dev_files/github_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
https://github.com/ComPWA/repo-maintenance.
"""

from __future__ import annotations

import os
import pathlib
from typing import List

from repoma.errors import PrecommitError
from repoma.utilities import CONFIG_PATH
Expand Down Expand Up @@ -48,7 +49,7 @@ def _check_has_labels_requirement(path: pathlib.Path) -> bool:
return False


def _get_requirement_files() -> List[pathlib.Path]:
def _get_requirement_files() -> list[pathlib.Path]:
return [
*pathlib.Path(".").glob("**/requirements*.in"),
*pathlib.Path(".").glob("**/requirements*.txt"),
Expand Down
39 changes: 22 additions & 17 deletions src/repoma/check_dev_files/github_workflows.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""Check :file:`.github/workflows` folder content."""

from __future__ import annotations

import os
import re
import shutil
from pathlib import Path
from typing import List, Tuple
from typing import TYPE_CHECKING

from ruamel.yaml.comments import CommentedMap
from ruamel.yaml.main import YAML
from ruamel.yaml.scalarstring import DoubleQuotedScalarString

from repoma.errors import PrecommitError
Expand All @@ -22,16 +21,22 @@
)
from repoma.utilities.yaml import create_prettier_round_trip_yaml

if TYPE_CHECKING:
from pathlib import Path

Check warning on line 25 in src/repoma/check_dev_files/github_workflows.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/github_workflows.py#L25

Added line #L25 was not covered by tests

from ruamel.yaml.comments import CommentedMap
from ruamel.yaml.main import YAML

Check warning on line 28 in src/repoma/check_dev_files/github_workflows.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/github_workflows.py#L27-L28

Added lines #L27 - L28 were not covered by tests


def main(
allow_deprecated: bool,
doc_apt_packages: List[str],
doc_apt_packages: list[str],
no_macos: bool,
no_pypi: bool,
no_version_branches: bool,
single_threaded: bool,
skip_tests: List[str],
test_extras: List[str],
skip_tests: list[str],
test_extras: list[str],
) -> None:
executor = Executor()
executor(_update_cd_workflow, no_pypi, no_version_branches)
Expand Down Expand Up @@ -86,11 +91,11 @@ def _update_pr_linting() -> None:

def _update_ci_workflow(
allow_deprecated: bool,
doc_apt_packages: List[str],
doc_apt_packages: list[str],
no_macos: bool,
single_threaded: bool,
skip_tests: List[str],
test_extras: List[str],
skip_tests: list[str],
test_extras: list[str],
) -> None:
def update() -> None:
yaml, expected_data = _get_ci_workflow(
Expand Down Expand Up @@ -128,12 +133,12 @@ def update() -> None:

def _get_ci_workflow(
path: Path,
doc_apt_packages: List[str],
doc_apt_packages: list[str],
no_macos: bool,
single_threaded: bool,
skip_tests: List[str],
test_extras: List[str],
) -> Tuple[YAML, dict]:
skip_tests: list[str],
test_extras: list[str],
) -> tuple[YAML, dict]:
yaml = create_prettier_round_trip_yaml()
config = yaml.load(path)
__update_doc_section(config, doc_apt_packages)
Expand All @@ -142,7 +147,7 @@ def _get_ci_workflow(
return yaml, config


def __update_doc_section(config: CommentedMap, apt_packages: List[str]) -> None:
def __update_doc_section(config: CommentedMap, apt_packages: list[str]) -> None:
if not os.path.exists("docs/"):
del config["jobs"]["doc"]
else:
Expand All @@ -167,8 +172,8 @@ def __update_pytest_section(
config: CommentedMap,
no_macos: bool,
single_threaded: bool,
skip_tests: List[str],
test_extras: List[str],
skip_tests: list[str],
test_extras: list[str],
) -> None:
test_dir = "tests"
if not os.path.exists(test_dir):
Expand Down
18 changes: 10 additions & 8 deletions src/repoma/check_dev_files/precommit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Check content of :code:`.pre-commit-config.yaml` and related files."""

from __future__ import annotations

from pathlib import Path
from typing import Iterable, List, Optional, Set, Tuple
from typing import Iterable

from ruamel.yaml.comments import CommentedMap, CommentedSeq
from ruamel.yaml.scalarstring import DoubleQuotedScalarString
Expand All @@ -25,18 +27,18 @@ def main() -> None:
def _sort_hooks() -> None:
yaml = create_prettier_round_trip_yaml()
contents: CommentedMap = yaml.load(CONFIG_PATH.precommit)
repos: Optional[CommentedSeq] = contents.get("repos")
repos: CommentedSeq | None = contents.get("repos")

Check warning on line 30 in src/repoma/check_dev_files/precommit.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/precommit.py#L30

Added line #L30 was not covered by tests
if repos is None:
return
sorted_repos: List[CommentedMap] = sorted(repos, key=__repo_def_sorting)
sorted_repos: list[CommentedMap] = sorted(repos, key=__repo_def_sorting)

Check warning on line 33 in src/repoma/check_dev_files/precommit.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/precommit.py#L33

Added line #L33 was not covered by tests
contents["repos"] = sorted_repos
if sorted_repos != repos:
yaml.dump(contents, CONFIG_PATH.precommit)
msg = f"Sorted pre-commit hooks in {CONFIG_PATH.precommit}"
raise PrecommitError(msg)


def __repo_def_sorting(repo_def: CommentedMap) -> Tuple[int, str]:
def __repo_def_sorting(repo_def: CommentedMap) -> tuple[int, str]:
if repo_def["repo"] == "meta":
return (0, "meta")
hooks: CommentedSeq = repo_def["hooks"]
Expand Down Expand Up @@ -80,7 +82,7 @@ def __update_precommit_ci_skip(expected_skips: Iterable[str]) -> None:
raise PrecommitError(msg)


def __get_precommit_ci_skips(config: PrecommitConfig) -> Set[str]:
def __get_precommit_ci_skips(config: PrecommitConfig) -> set[str]:
if config.ci is None:
msg = "Pre-commit config does not contain a ci section"
raise ValueError(msg)
Expand All @@ -89,11 +91,11 @@ def __get_precommit_ci_skips(config: PrecommitConfig) -> Set[str]:
return set(config.ci.skip)


def get_local_hooks(config: PrecommitConfig) -> List[str]:
def get_local_hooks(config: PrecommitConfig) -> list[str]:
return [h.id for r in config.repos for h in r.hooks if r.repo == "local"]


def get_non_functional_hooks(config: PrecommitConfig) -> List[str]:
def get_non_functional_hooks(config: PrecommitConfig) -> list[str]:
return [
hook.id
for repo in config.repos
Expand Down Expand Up @@ -131,7 +133,7 @@ def _update_conda_environment(precommit_config: PrecommitConfig) -> None:
raise PrecommitError(msg)


def __get_skipped_hooks(config: PrecommitConfig) -> Set[str]:
def __get_skipped_hooks(config: PrecommitConfig) -> set[str]:
skipped_hooks = {
"check-jsonschema",
"pyright",
Expand Down
6 changes: 4 additions & 2 deletions src/repoma/check_dev_files/prettier.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Check the configuration for `Prettier <https://prettier.io>`_."""

from __future__ import annotations

import os
from typing import Iterable, List
from typing import Iterable

from repoma.errors import PrecommitError
from repoma.utilities import CONFIG_PATH, REPOMA_DIR, vscode
Expand Down Expand Up @@ -125,7 +127,7 @@ def __insert_expected_paths() -> None:
raise PrecommitError(msg)


def __get_existing_lines() -> List[str]:
def __get_existing_lines() -> list[str]:
if not os.path.exists(CONFIG_PATH.prettier_ignore):
return [""]
with open(CONFIG_PATH.prettier_ignore) as f:
Expand Down
6 changes: 4 additions & 2 deletions src/repoma/check_dev_files/pyright.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Check and update :code:`mypy` settings."""

from __future__ import annotations

import json
import os
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING

from repoma.errors import PrecommitError
from repoma.utilities import CONFIG_PATH
Expand Down Expand Up @@ -46,7 +48,7 @@ def _merge_config_into_pyproject() -> None:

def _update_settings() -> None:
pyproject = load_pyproject()
pyright_settings: Optional[Table] = pyproject.get("tool", {}).get("pyright")
pyright_settings: Table | None = pyproject.get("tool", {}).get("pyright")

Check warning on line 51 in src/repoma/check_dev_files/pyright.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/pyright.py#L51

Added line #L51 was not covered by tests
if pyright_settings is None:
return
minimal_settings = {
Expand Down
5 changes: 3 additions & 2 deletions src/repoma/check_dev_files/pytest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Check and update :code:`pytest` settings."""

from __future__ import annotations

import os
from typing import Dict

import tomlkit
from ini2toml.api import Translator
Expand Down Expand Up @@ -30,7 +31,7 @@ def _merge_coverage_into_pyproject() -> None:
section_name = "coverage:run"
if not pytest_ini.has_section(section_name):
return
coverage_config: Dict = dict(pytest_ini[section_name])
coverage_config: dict = dict(pytest_ini[section_name])

Check warning on line 34 in src/repoma/check_dev_files/pytest.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/pytest.py#L34

Added line #L34 was not covered by tests
for key, value in coverage_config.items():
if value in {"False", "True"}:
coverage_config[key] = bool(value)
Expand Down
Loading

0 comments on commit ffc6e30

Please sign in to comment.