Skip to content

Commit ebee76a

Browse files
committed
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.
1 parent 2a57752 commit ebee76a

34 files changed

+108
-105
lines changed

pygit2/__init__.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,17 +364,15 @@
364364

365365

366366
def init_repository(
367-
path: typing.Union[str, bytes, os.PathLike, None],
367+
path: str | bytes | os.PathLike | None,
368368
bare: bool = False,
369369
flags: enums.RepositoryInitFlag = enums.RepositoryInitFlag.MKPATH,
370-
mode: typing.Union[
371-
int, enums.RepositoryInitMode
372-
] = enums.RepositoryInitMode.SHARED_UMASK,
373-
workdir_path: typing.Optional[str] = None,
374-
description: typing.Optional[str] = None,
375-
template_path: typing.Optional[str] = None,
376-
initial_head: typing.Optional[str] = None,
377-
origin_url: typing.Optional[str] = None,
370+
mode: (int | enums.RepositoryInitMode) = enums.RepositoryInitMode.SHARED_UMASK,
371+
workdir_path: str | None = None,
372+
description: str | None = None,
373+
template_path: str | None = None,
374+
initial_head: str | None = None,
375+
origin_url: str | None = None,
378376
) -> Repository:
379377
"""
380378
Creates a new Git repository in the given *path*.

pygit2/blame.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
2424
# Boston, MA 02110-1301, USA.
2525

26-
from typing import TYPE_CHECKING, Iterator
26+
from collections.abc import Iterator
27+
from typing import TYPE_CHECKING
2728

2829
from ._pygit2 import Oid, Repository, Signature
2930

pygit2/blob.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import time
44
from contextlib import AbstractContextManager
55
from queue import Queue
6-
from typing import Optional
76

87
from ._pygit2 import Blob, Oid
98
from .enums import BlobFilter
@@ -20,16 +19,16 @@ class _BlobIO(io.RawIOBase):
2019
def __init__(
2120
self,
2221
blob: Blob,
23-
as_path: Optional[str] = None,
22+
as_path: str | None = None,
2423
flags: BlobFilter = BlobFilter.CHECK_FOR_BINARY,
25-
commit_id: Optional[Oid] = None,
24+
commit_id: Oid | None = None,
2625
):
2726
super().__init__()
2827
self._blob = blob
29-
self._queue: Optional[Queue] = Queue(maxsize=1)
28+
self._queue: Queue | None = Queue(maxsize=1)
3029
self._ready = threading.Event()
3130
self._writer_closed = threading.Event()
32-
self._chunk: Optional[bytes] = None
31+
self._chunk: bytes | None = None
3332
self._thread = threading.Thread(
3433
target=self._blob._write_to_queue,
3534
args=(self._queue, self._ready, self._writer_closed),
@@ -127,9 +126,9 @@ class BlobIO(io.BufferedReader, AbstractContextManager):
127126
def __init__(
128127
self,
129128
blob: Blob,
130-
as_path: Optional[str] = None,
129+
as_path: str | None = None,
131130
flags: BlobFilter = BlobFilter.CHECK_FOR_BINARY,
132-
commit_id: Optional[Oid] = None,
131+
commit_id: Oid | None = None,
133132
):
134133
"""Wrap the specified blob.
135134

pygit2/branches.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626
from __future__ import annotations
2727

28-
from typing import TYPE_CHECKING, Iterator
28+
from collections.abc import Iterator
29+
from typing import TYPE_CHECKING
2930

3031
from ._pygit2 import Branch, Commit, Oid
3132
from .enums import BranchType, ReferenceType
@@ -36,8 +37,8 @@
3637

3738

3839
class Branches:
39-
local: 'Branches'
40-
remote: 'Branches'
40+
local: Branches
41+
remote: Branches
4142

4243
def __init__(
4344
self,
@@ -100,7 +101,7 @@ def _valid(self, branch: Branch) -> bool:
100101
or self._repository.descendant_of(branch_direct.target, self._commit)
101102
)
102103

103-
def with_commit(self, commit: Commit | Oid | str | None) -> 'Branches':
104+
def with_commit(self, commit: Commit | Oid | str | None) -> Branches:
104105
assert self._commit is None
105106
return Branches(self._repository, self._flag, commit)
106107

pygit2/callbacks.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@
6363
"""
6464

6565
# Standard Library
66+
from collections.abc import Callable, Generator
6667
from contextlib import contextmanager
6768
from functools import wraps
68-
from typing import TYPE_CHECKING, Callable, Generator, Optional, Union
69+
from typing import TYPE_CHECKING, Optional
6970

7071
# pygit2
7172
from ._pygit2 import DiffFile, Oid
@@ -151,7 +152,7 @@ def sideband_progress(self, string: str) -> None:
151152
def credentials(
152153
self,
153154
url: str,
154-
username_from_url: Union[str, None],
155+
username_from_url: str | None,
155156
allowed_types: CredentialType,
156157
) -> _Credentials:
157158
"""
@@ -298,9 +299,9 @@ def checkout_notify(
298299
self,
299300
why: CheckoutNotify,
300301
path: str,
301-
baseline: Optional[DiffFile],
302-
target: Optional[DiffFile],
303-
workdir: Optional[DiffFile],
302+
baseline: DiffFile | None,
303+
target: DiffFile | None,
304+
workdir: DiffFile | None,
304305
) -> None:
305306
"""
306307
Checkout will invoke an optional notification callback for

pygit2/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
2424
# Boston, MA 02110-1301, USA.
2525

26+
from collections.abc import Callable, Iterator
2627
from os import PathLike
2728
from pathlib import Path
28-
from typing import TYPE_CHECKING, Callable, Iterator
29+
from typing import TYPE_CHECKING
2930

3031
try:
3132
from functools import cached_property

pygit2/errors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from ._pygit2 import GitError
2828
from .ffi import C, ffi
2929

30-
value_errors = set([C.GIT_EEXISTS, C.GIT_EINVALIDSPEC, C.GIT_EAMBIGUOUS])
30+
value_errors = {C.GIT_EEXISTS, C.GIT_EINVALIDSPEC, C.GIT_EAMBIGUOUS}
3131

3232

3333
def check_error(err, io=False):
@@ -51,7 +51,7 @@ def check_error(err, io=False):
5151

5252
if err == C.GIT_ENOTFOUND:
5353
if io:
54-
raise IOError(message)
54+
raise OSError(message)
5555

5656
raise KeyError(message)
5757

pygit2/filter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
2424
# Boston, MA 02110-1301, USA.
2525

26-
from typing import Callable, List, Optional
26+
27+
from collections.abc import Callable
2728

2829
from ._pygit2 import FilterSource
2930

@@ -58,7 +59,7 @@ class Filter:
5859
def nattrs(cls) -> int:
5960
return len(cls.attributes.split())
6061

61-
def check(self, src: FilterSource, attr_values: List[Optional[str]]) -> None:
62+
def check(self, src: FilterSource, attr_values: list[str | None]) -> None:
6263
"""
6364
Check whether this filter should be applied to the given source.
6465

pygit2/index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ class MergeFileResult:
392392
automergeable: bool
393393
'True if the output was automerged, false if the output contains conflict markers'
394394

395-
path: typing.Union[str, None, PathLike[str]]
395+
path: str | None | PathLike[str]
396396
'The path that the resultant merge file should use, or None if a filename conflict would occur'
397397

398398
mode: FileMode

pygit2/references.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626
from __future__ import annotations
2727

28-
from typing import TYPE_CHECKING, Iterator
28+
from collections.abc import Iterator
29+
from typing import TYPE_CHECKING
2930

3031
from pygit2 import Oid
3132

@@ -41,10 +42,10 @@ class References:
4142
def __init__(self, repository: BaseRepository) -> None:
4243
self._repository = repository
4344

44-
def __getitem__(self, name: str) -> 'Reference':
45+
def __getitem__(self, name: str) -> Reference:
4546
return self._repository.lookup_reference(name)
4647

47-
def get(self, key: str) -> 'Reference' | None:
48+
def get(self, key: str) -> Reference | None:
4849
try:
4950
return self[key]
5051
except KeyError:
@@ -61,7 +62,7 @@ def __iter__(self) -> Iterator[str]:
6162

6263
def iterator(
6364
self, references_return_type: ReferenceFilter = ReferenceFilter.ALL
64-
) -> Iterator['Reference']:
65+
) -> Iterator[Reference]:
6566
"""Creates a new iterator and fetches references for a given repository.
6667
6768
Can also filter and pass all refs or only branches or only tags.
@@ -93,7 +94,7 @@ def iterator(
9394
else:
9495
return
9596

96-
def create(self, name: str, target: Oid | str, force: bool = False) -> 'Reference':
97+
def create(self, name: str, target: Oid | str, force: bool = False) -> Reference:
9798
return self._repository.create_reference(name, target, force)
9899

99100
def delete(self, name: str) -> None:
@@ -103,7 +104,7 @@ def __contains__(self, name: str) -> bool:
103104
return self.get(name) is not None
104105

105106
@property
106-
def objects(self) -> list['Reference']:
107+
def objects(self) -> list[Reference]:
107108
return self._repository.listall_reference_objects()
108109

109110
def compress(self) -> None:

0 commit comments

Comments
 (0)