Skip to content

Commit 0ece131

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 0ece131

37 files changed

+147
-141
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/_build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def _get_libgit2_path():
4848

4949
# Default
5050
if os.name == 'nt':
51-
return Path(r'%s\libgit2' % os.getenv('ProgramFiles'))
51+
return Path(r'{}\libgit2'.format(os.getenv('ProgramFiles')))
5252
return Path('/usr/local')
5353

5454

pygit2/_pygit2.pyi

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1+
from collections.abc import Iterator, Sequence
12
from io import DEFAULT_BUFFER_SIZE, IOBase
23
from pathlib import Path
34
from queue import Queue
45
from threading import Event
56
from typing import (
67
Generic,
7-
Iterator,
88
Literal,
9-
Optional,
10-
Sequence,
11-
Type,
129
TypedDict,
1310
TypeVar,
1411
overload,
@@ -303,29 +300,34 @@ class _ObjectBase(Generic[T]):
303300
name: str | None
304301
raw_name: bytes | None
305302
short_id: str
306-
type: 'Literal[GIT_OBJ_COMMIT] | Literal[GIT_OBJ_TREE] | Literal[GIT_OBJ_TAG] | Literal[GIT_OBJ_BLOB]'
307-
type_str: "Literal['commit'] | Literal['tree'] | Literal['tag'] | Literal['blob']"
303+
type: (
304+
Literal[GIT_OBJ_COMMIT]
305+
| Literal[GIT_OBJ_TREE]
306+
| Literal[GIT_OBJ_TAG]
307+
| Literal[GIT_OBJ_BLOB]
308+
)
309+
type_str: Literal['commit'] | Literal['tree'] | Literal['tag'] | Literal['blob']
308310
author: Signature
309311
committer: Signature
310312
tree: Tree
311313
@overload
312314
def peel(
313-
self, target_type: 'Literal[GIT_OBJ_COMMIT, ObjectType.COMMIT] | Type[Commit]'
314-
) -> 'Commit': ...
315+
self, target_type: Literal[GIT_OBJ_COMMIT, ObjectType.COMMIT] | type[Commit]
316+
) -> Commit: ...
315317
@overload
316318
def peel(
317-
self, target_type: 'Literal[GIT_OBJ_TREE, ObjectType.TREE] | Type[Tree]'
318-
) -> 'Tree': ...
319+
self, target_type: Literal[GIT_OBJ_TREE, ObjectType.TREE] | type[Tree]
320+
) -> Tree: ...
319321
@overload
320322
def peel(
321-
self, target_type: 'Literal[GIT_OBJ_TAG, ObjectType.TAG] | Type[Tag]'
322-
) -> 'Tag': ...
323+
self, target_type: Literal[GIT_OBJ_TAG, ObjectType.TAG] | type[Tag]
324+
) -> Tag: ...
323325
@overload
324326
def peel(
325-
self, target_type: 'Literal[GIT_OBJ_BLOB, ObjectType.BLOB] | Type[Blob]'
326-
) -> 'Blob': ...
327+
self, target_type: Literal[GIT_OBJ_BLOB, ObjectType.BLOB] | type[Blob]
328+
) -> Blob: ...
327329
@overload
328-
def peel(self, target_type: 'None') -> 'Commit|Tree|Tag|Blob': ...
330+
def peel(self, target_type: None) -> Commit | Tree | Tag | Blob: ...
329331
def read_raw(self) -> bytes: ...
330332
def __eq__(self, other) -> bool: ...
331333
def __ge__(self, other) -> bool: ...
@@ -350,15 +352,15 @@ class Reference:
350352
def delete(self) -> None: ...
351353
def log(self) -> Iterator[RefLogEntry]: ...
352354
@overload
353-
def peel(self, type: 'Literal[GIT_OBJ_COMMIT] | Type[Commit]') -> 'Commit': ...
355+
def peel(self, type: Literal[GIT_OBJ_COMMIT] | type[Commit]) -> Commit: ...
354356
@overload
355-
def peel(self, type: 'Literal[GIT_OBJ_TREE] | Type[Tree]') -> 'Tree': ...
357+
def peel(self, type: Literal[GIT_OBJ_TREE] | type[Tree]) -> Tree: ...
356358
@overload
357-
def peel(self, type: 'Literal[GIT_OBJ_TAG] | Type[Tag]') -> 'Tag': ...
359+
def peel(self, type: Literal[GIT_OBJ_TAG] | type[Tag]) -> Tag: ...
358360
@overload
359-
def peel(self, type: 'Literal[GIT_OBJ_BLOB] | Type[Blob]') -> 'Blob': ...
361+
def peel(self, type: Literal[GIT_OBJ_BLOB] | type[Blob]) -> Blob: ...
360362
@overload
361-
def peel(self, type: 'None' = None) -> 'Commit|Tree|Tag|Blob': ...
363+
def peel(self, type: None = None) -> Commit | Tree | Tag | Blob: ...
362364
def rename(self, new_name: str) -> None: ...
363365
def resolve(self) -> Reference: ...
364366
def set_target(self, target: _OidArg, message: str = ...) -> None: ...
@@ -384,7 +386,7 @@ class Blob(Object):
384386
) -> Patch: ...
385387
def diff_to_buffer(
386388
self,
387-
buffer: Optional[bytes | str] = None,
389+
buffer: bytes | str | None = None,
388390
flag: DiffOption = DiffOption.NORMAL,
389391
old_as_path: str = ...,
390392
buffer_as_path: str = ...,
@@ -395,9 +397,9 @@ class Blob(Object):
395397
ready: Event,
396398
done: Event,
397399
chunk_size: int = DEFAULT_BUFFER_SIZE,
398-
as_path: Optional[str] = None,
400+
as_path: str | None = None,
399401
flags: BlobFilter = BlobFilter.CHECK_FOR_BINARY,
400-
commit_id: Optional[Oid] = None,
402+
commit_id: Oid | None = None,
401403
) -> None: ...
402404
def __buffer__(self, flags: int) -> memoryview: ...
403405
def __release_buffer__(self, buffer: memoryview) -> None: ...
@@ -411,7 +413,7 @@ class Branch(Reference):
411413
def delete(self) -> None: ...
412414
def is_checked_out(self) -> bool: ...
413415
def is_head(self) -> bool: ...
414-
def rename(self, name: str, force: bool = False) -> 'Branch': ... # type: ignore[override]
416+
def rename(self, name: str, force: bool = False) -> Branch: ... # type: ignore[override]
415417

416418
class FetchOptions:
417419
# incomplete
@@ -693,7 +695,7 @@ class Repository:
693695
def create_branch(self, name: str, commit: Commit, force=False) -> Branch: ...
694696
def create_commit(
695697
self,
696-
reference_name: Optional[str],
698+
reference_name: str | None,
697699
author: Signature,
698700
committer: Signature,
699701
message: str | bytes,
@@ -711,7 +713,7 @@ class Repository:
711713
encoding: str = ...,
712714
) -> Oid: ...
713715
def create_commit_with_signature(
714-
self, content: str, signature: str, signature_field: Optional[str] = None
716+
self, content: str, signature: str, signature_field: str | None = None
715717
) -> Oid: ...
716718
def create_note(
717719
self,
@@ -723,10 +725,10 @@ class Repository:
723725
force: bool = False,
724726
) -> Oid: ...
725727
def create_reference_direct(
726-
self, name: str, target: _OidArg, force: bool, message: Optional[str] = None
728+
self, name: str, target: _OidArg, force: bool, message: str | None = None
727729
) -> Reference: ...
728730
def create_reference_symbolic(
729-
self, name: str, target: str, force: bool, message: Optional[str] = None
731+
self, name: str, target: str, force: bool, message: str | None = None
730732
) -> Reference: ...
731733
def create_tag(
732734
self, name: str, oid: _OidArg, type: ObjectType, tagger: Signature, message: str
@@ -801,7 +803,7 @@ class Signature:
801803
email: str,
802804
time: int = -1,
803805
offset: int = 0,
804-
encoding: Optional[str] = None,
806+
encoding: str | None = None,
805807
) -> None: ...
806808
def __eq__(self, other) -> bool: ...
807809
def __ge__(self, other) -> bool: ...

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

0 commit comments

Comments
 (0)