Skip to content

Use modern python syntax for types and others #1399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions pygit2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions pygit2/_pygit2.pyi
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 2 additions & 1 deletion pygit2/blame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion pygit2/branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions pygit2/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
"""
Expand Down
3 changes: 2 additions & 1 deletion pygit2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pygit2/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion pygit2/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pygit2/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pygit2/refspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pygit2/remotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pygit2/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
9 changes: 5 additions & 4 deletions pygit2/submodules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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).
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 2 additions & 4 deletions pygit2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: ...
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import platform
from collections.abc import Generator
from pathlib import Path
from typing import Generator

import pytest

Expand Down
2 changes: 1 addition & 1 deletion test/test_branch_empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test_commit_trailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test_diff_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test_filter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import codecs
from collections.abc import Callable, Generator
from io import BytesIO
from typing import Callable, Generator

import pytest

Expand Down
2 changes: 1 addition & 1 deletion test/test_odb.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

# Standard Library
import binascii
from collections.abc import Generator
from pathlib import Path
from typing import Generator

import pytest

Expand Down
2 changes: 1 addition & 1 deletion test/test_odb_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test_packbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/test_refdb_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test_remote_prune.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test_remote_utf8.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test_repository_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test_submodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

"""Tests for Submodule objects."""

from collections.abc import Generator
from pathlib import Path
from typing import Generator

import pytest

Expand Down
3 changes: 2 additions & 1 deletion test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading