From 1d9d0bef2cf88b31153f770b03cee09edf9d6fbb Mon Sep 17 00:00:00 2001 From: kbaikov Date: Wed, 6 Aug 2025 14:46:49 +0200 Subject: [PATCH] Use modern python syntax for types and others Since the minium declared python version is 3.10 we don't need the Union and Option and many type imports should be done from collections.abc. so i ran fd --extension py --type f --exec pyupgrade --py310-plus on the repo. All changes are automated, i.e. no manual edit. --- pygit2/__init__.py | 6 ++---- pygit2/_pygit2.pyi | 5 ++--- pygit2/blame.py | 3 ++- pygit2/branches.py | 3 ++- pygit2/callbacks.py | 5 +++-- pygit2/config.py | 3 ++- pygit2/filter.py | 4 ++-- pygit2/index.py | 2 +- pygit2/references.py | 3 ++- pygit2/refspec.py | 2 +- pygit2/remotes.py | 3 ++- pygit2/repository.py | 3 ++- pygit2/submodules.py | 9 +++++---- pygit2/utils.py | 6 ++---- pyproject.toml | 2 +- test/conftest.py | 2 +- test/test_branch_empty.py | 2 +- test/test_commit_trailer.py | 2 +- test/test_config.py | 2 +- test/test_diff.py | 2 +- test/test_diff_binary.py | 2 +- test/test_filter.py | 2 +- test/test_odb.py | 2 +- test/test_odb_backend.py | 2 +- test/test_packbuilder.py | 2 +- test/test_refdb_backend.py | 2 +- test/test_remote_prune.py | 2 +- test/test_remote_utf8.py | 2 +- test/test_repository_custom.py | 2 +- test/test_submodule.py | 2 +- test/utils.py | 3 ++- 31 files changed, 48 insertions(+), 44 deletions(-) diff --git a/pygit2/__init__.py b/pygit2/__init__.py index 38ae2c38..7b01d37c 100644 --- a/pygit2/__init__.py +++ b/pygit2/__init__.py @@ -364,12 +364,10 @@ def init_repository( - path: typing.Union[str, bytes, os.PathLike, None], + path: str | bytes | os.PathLike | None, bare: bool = False, flags: enums.RepositoryInitFlag = enums.RepositoryInitFlag.MKPATH, - mode: typing.Union[ - int, enums.RepositoryInitMode - ] = enums.RepositoryInitMode.SHARED_UMASK, + mode: int | enums.RepositoryInitMode = enums.RepositoryInitMode.SHARED_UMASK, workdir_path: typing.Optional[str] = None, description: typing.Optional[str] = None, template_path: typing.Optional[str] = None, diff --git a/pygit2/_pygit2.pyi b/pygit2/_pygit2.pyi index 7ca7d269..105acc2a 100644 --- a/pygit2/_pygit2.pyi +++ b/pygit2/_pygit2.pyi @@ -1,13 +1,12 @@ +from collections.abc import Iterator, Sequence from io import DEFAULT_BUFFER_SIZE, IOBase from pathlib import Path from queue import Queue from threading import Event -from typing import ( +from typing import ( # noqa: UP035 Generic, - Iterator, Literal, Optional, - Sequence, Type, TypedDict, TypeVar, diff --git a/pygit2/blame.py b/pygit2/blame.py index 02685e12..9854f3af 100644 --- a/pygit2/blame.py +++ b/pygit2/blame.py @@ -23,7 +23,8 @@ # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. -from typing import TYPE_CHECKING, Iterator +from collections.abc import Iterator +from typing import TYPE_CHECKING from ._pygit2 import Oid, Repository, Signature diff --git a/pygit2/branches.py b/pygit2/branches.py index ca32e151..b729a21e 100644 --- a/pygit2/branches.py +++ b/pygit2/branches.py @@ -25,7 +25,8 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Iterator +from collections.abc import Iterator +from typing import TYPE_CHECKING from ._pygit2 import Branch, Commit, Oid from .enums import BranchType, ReferenceType diff --git a/pygit2/callbacks.py b/pygit2/callbacks.py index 3e365ebc..3423e2db 100644 --- a/pygit2/callbacks.py +++ b/pygit2/callbacks.py @@ -63,9 +63,10 @@ """ # Standard Library +from collections.abc import Callable, Generator from contextlib import contextmanager from functools import wraps -from typing import TYPE_CHECKING, Callable, Generator, Optional, Union +from typing import TYPE_CHECKING, Optional # pygit2 from ._pygit2 import DiffFile, Oid @@ -151,7 +152,7 @@ def sideband_progress(self, string: str) -> None: def credentials( self, url: str, - username_from_url: Union[str, None], + username_from_url: str | None, allowed_types: CredentialType, ) -> _Credentials: """ diff --git a/pygit2/config.py b/pygit2/config.py index ec97f543..76d114fe 100644 --- a/pygit2/config.py +++ b/pygit2/config.py @@ -23,9 +23,10 @@ # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. +from collections.abc import Callable, Iterator from os import PathLike from pathlib import Path -from typing import TYPE_CHECKING, Callable, Iterator +from typing import TYPE_CHECKING try: from functools import cached_property diff --git a/pygit2/filter.py b/pygit2/filter.py index abf3eb35..5c89a0d1 100644 --- a/pygit2/filter.py +++ b/pygit2/filter.py @@ -23,7 +23,7 @@ # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. -from typing import Callable, List, Optional +from collections.abc import Callable from ._pygit2 import FilterSource @@ -58,7 +58,7 @@ class Filter: def nattrs(cls) -> int: return len(cls.attributes.split()) - def check(self, src: FilterSource, attr_values: List[Optional[str]]) -> None: + def check(self, src: FilterSource, attr_values: list[str | None]) -> None: """ Check whether this filter should be applied to the given source. diff --git a/pygit2/index.py b/pygit2/index.py index 3fedf7c6..928a6222 100644 --- a/pygit2/index.py +++ b/pygit2/index.py @@ -392,7 +392,7 @@ class MergeFileResult: automergeable: bool 'True if the output was automerged, false if the output contains conflict markers' - path: typing.Union[str, None, PathLike[str]] + path: str | None | PathLike[str] 'The path that the resultant merge file should use, or None if a filename conflict would occur' mode: FileMode diff --git a/pygit2/references.py b/pygit2/references.py index 5cbee8fd..93c370ea 100644 --- a/pygit2/references.py +++ b/pygit2/references.py @@ -25,7 +25,8 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Iterator +from collections.abc import Iterator +from typing import TYPE_CHECKING from pygit2 import Oid diff --git a/pygit2/refspec.py b/pygit2/refspec.py index 968f7fdd..cb900e75 100644 --- a/pygit2/refspec.py +++ b/pygit2/refspec.py @@ -23,7 +23,7 @@ # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. -from typing import Callable +from collections.abc import Callable # Import from pygit2 from .errors import check_error diff --git a/pygit2/remotes.py b/pygit2/remotes.py index b3da7855..52974646 100644 --- a/pygit2/remotes.py +++ b/pygit2/remotes.py @@ -26,7 +26,8 @@ from __future__ import annotations import warnings -from typing import TYPE_CHECKING, Any, Generator, Iterator, Literal +from collections.abc import Generator, Iterator +from typing import TYPE_CHECKING, Any, Literal # Import from pygit2 from pygit2 import RemoteCallbacks diff --git a/pygit2/repository.py b/pygit2/repository.py index dfb96e32..509b4d0f 100644 --- a/pygit2/repository.py +++ b/pygit2/repository.py @@ -25,11 +25,12 @@ import tarfile import warnings +from collections.abc import Callable, Iterator from io import BytesIO from pathlib import Path from string import hexdigits from time import time -from typing import TYPE_CHECKING, Callable, Iterator, Optional, overload +from typing import TYPE_CHECKING, Optional, overload # Import from pygit2 from ._pygit2 import ( diff --git a/pygit2/submodules.py b/pygit2/submodules.py index 4cd036a8..4ff00980 100644 --- a/pygit2/submodules.py +++ b/pygit2/submodules.py @@ -25,8 +25,9 @@ from __future__ import annotations +from collections.abc import Iterable, Iterator from pathlib import Path -from typing import TYPE_CHECKING, Iterable, Iterator, Optional, Union +from typing import TYPE_CHECKING, Optional from ._pygit2 import Oid from .callbacks import RemoteCallbacks, git_fetch_options @@ -147,7 +148,7 @@ def path(self): return ffi.string(path).decode('utf-8') @property - def url(self) -> Union[str, None]: + def url(self) -> str | None: """URL of the submodule.""" url = C.git_submodule_url(self._subm) return maybe_string(url) @@ -167,7 +168,7 @@ def branch(self): return ffi.string(branch).decode('utf-8') @property - def head_id(self) -> Union[Oid, None]: + def head_id(self) -> Oid | None: """ The submodule's HEAD commit id (as recorded in the superproject's current HEAD tree). @@ -205,7 +206,7 @@ def __iter__(self) -> Iterator[Submodule]: for s in self._repository.listall_submodules(): yield self[s] - def get(self, name: str) -> Union[Submodule, None]: + def get(self, name: str) -> Submodule | None: """ Look up submodule information by name or path. Unlike __getitem__, this returns None if the submodule is not found. diff --git a/pygit2/utils.py b/pygit2/utils.py index d00b7626..b3d5d141 100644 --- a/pygit2/utils.py +++ b/pygit2/utils.py @@ -25,15 +25,13 @@ import contextlib import os +from collections.abc import Generator, Iterator, Sequence from types import TracebackType from typing import ( TYPE_CHECKING, - Generator, Generic, - Iterator, Optional, Protocol, - Sequence, TypeVar, Union, overload, @@ -55,7 +53,7 @@ def maybe_string(ptr: 'char_pointer | None') -> str | None: @overload def to_bytes( - s: Union[str, bytes, os.PathLike[str]], + s: str | bytes | os.PathLike[str], encoding: str = 'utf-8', errors: str = 'strict', ) -> bytes: ... diff --git a/pyproject.toml b/pyproject.toml index 219efcac..a9a25a5c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,7 @@ extend-exclude = [ ".cache", ".coverage", "build", "site-packages", "venv*"] target-version = "py310" # oldest supported Python version [tool.ruff.lint] -select = ["E4", "E7", "E9", "F", "I"] +select = ["E4", "E7", "E9", "F", "I", "UP035", "UP007"] [tool.ruff.format] quote-style = "single" diff --git a/test/conftest.py b/test/conftest.py index 4ea9b688..6052346f 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,6 +1,6 @@ import platform +from collections.abc import Generator from pathlib import Path -from typing import Generator import pytest diff --git a/test/test_branch_empty.py b/test/test_branch_empty.py index 9bfe0088..5b2beabd 100644 --- a/test/test_branch_empty.py +++ b/test/test_branch_empty.py @@ -23,7 +23,7 @@ # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. -from typing import Generator +from collections.abc import Generator import pytest diff --git a/test/test_commit_trailer.py b/test/test_commit_trailer.py index 51043ff8..efe5434f 100644 --- a/test/test_commit_trailer.py +++ b/test/test_commit_trailer.py @@ -23,8 +23,8 @@ # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. +from collections.abc import Generator from pathlib import Path -from typing import Generator import pytest diff --git a/test/test_config.py b/test/test_config.py index f7e63112..247d55c5 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -23,8 +23,8 @@ # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. +from collections.abc import Generator from pathlib import Path -from typing import Generator import pytest diff --git a/test/test_diff.py b/test/test_diff.py index 7c75dc4d..838d4dd0 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -26,8 +26,8 @@ """Tests for Diff objects.""" import textwrap +from collections.abc import Iterator from itertools import chain -from typing import Iterator import pytest diff --git a/test/test_diff_binary.py b/test/test_diff_binary.py index 9eb3a38c..2947e403 100644 --- a/test/test_diff_binary.py +++ b/test/test_diff_binary.py @@ -23,8 +23,8 @@ # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. +from collections.abc import Generator from pathlib import Path -from typing import Generator import pytest diff --git a/test/test_filter.py b/test/test_filter.py index d29b8b7e..26a45095 100644 --- a/test/test_filter.py +++ b/test/test_filter.py @@ -1,6 +1,6 @@ import codecs +from collections.abc import Callable, Generator from io import BytesIO -from typing import Callable, Generator import pytest diff --git a/test/test_odb.py b/test/test_odb.py index a9b33e18..4d023135 100644 --- a/test/test_odb.py +++ b/test/test_odb.py @@ -27,8 +27,8 @@ # Standard Library import binascii +from collections.abc import Generator from pathlib import Path -from typing import Generator import pytest diff --git a/test/test_odb_backend.py b/test/test_odb_backend.py index ab1ea588..89c21572 100644 --- a/test/test_odb_backend.py +++ b/test/test_odb_backend.py @@ -27,8 +27,8 @@ # Standard Library import binascii +from collections.abc import Generator, Iterator from pathlib import Path -from typing import Generator, Iterator import pytest diff --git a/test/test_packbuilder.py b/test/test_packbuilder.py index d6b6a8e1..5309f3f1 100644 --- a/test/test_packbuilder.py +++ b/test/test_packbuilder.py @@ -25,8 +25,8 @@ """Tests for Index files.""" +from collections.abc import Callable from pathlib import Path -from typing import Callable import pygit2 from pygit2 import Oid, PackBuilder, Repository diff --git a/test/test_refdb_backend.py b/test/test_refdb_backend.py index 903b9388..bfc37551 100644 --- a/test/test_refdb_backend.py +++ b/test/test_refdb_backend.py @@ -25,8 +25,8 @@ """Tests for Refdb objects.""" +from collections.abc import Generator, Iterator from pathlib import Path -from typing import Generator, Iterator import pytest diff --git a/test/test_remote_prune.py b/test/test_remote_prune.py index ea0b8489..2fdaab0d 100644 --- a/test/test_remote_prune.py +++ b/test/test_remote_prune.py @@ -23,8 +23,8 @@ # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. +from collections.abc import Generator from pathlib import Path -from typing import Generator import pytest diff --git a/test/test_remote_utf8.py b/test/test_remote_utf8.py index 4ed0bdd2..03edd6b4 100644 --- a/test/test_remote_utf8.py +++ b/test/test_remote_utf8.py @@ -23,8 +23,8 @@ # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. +from collections.abc import Generator from pathlib import Path -from typing import Generator import pytest diff --git a/test/test_repository_custom.py b/test/test_repository_custom.py index 336c20a8..40698df0 100644 --- a/test/test_repository_custom.py +++ b/test/test_repository_custom.py @@ -23,8 +23,8 @@ # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. +from collections.abc import Generator from pathlib import Path -from typing import Generator import pytest diff --git a/test/test_submodule.py b/test/test_submodule.py index 453a5abf..90bdc758 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -25,8 +25,8 @@ """Tests for Submodule objects.""" +from collections.abc import Generator from pathlib import Path -from typing import Generator import pytest diff --git a/test/utils.py b/test/utils.py index fe42a760..a3b14cfd 100644 --- a/test/utils.py +++ b/test/utils.py @@ -30,9 +30,10 @@ import stat import sys import zipfile +from collections.abc import Callable from pathlib import Path from types import TracebackType -from typing import Any, Callable, Optional, ParamSpec, TypeVar +from typing import Any, Optional, ParamSpec, TypeVar # Requirements import pytest