Skip to content

Commit 568ea55

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 568ea55

37 files changed

+142
-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: 26 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,29 @@ 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: Literal[GIT_OBJ_COMMIT] | Literal[GIT_OBJ_TREE] | Literal[GIT_OBJ_TAG] | Literal[GIT_OBJ_BLOB]
304+
type_str: Literal['commit'] | Literal['tree'] | Literal['tag'] | Literal['blob']
308305
author: Signature
309306
committer: Signature
310307
tree: Tree
311308
@overload
312309
def peel(
313-
self, target_type: 'Literal[GIT_OBJ_COMMIT, ObjectType.COMMIT] | Type[Commit]'
314-
) -> 'Commit': ...
310+
self, target_type: Literal[GIT_OBJ_COMMIT, ObjectType.COMMIT] | type[Commit]
311+
) -> Commit: ...
315312
@overload
316313
def peel(
317-
self, target_type: 'Literal[GIT_OBJ_TREE, ObjectType.TREE] | Type[Tree]'
318-
) -> 'Tree': ...
314+
self, target_type: Literal[GIT_OBJ_TREE, ObjectType.TREE] | type[Tree]
315+
) -> Tree: ...
319316
@overload
320317
def peel(
321-
self, target_type: 'Literal[GIT_OBJ_TAG, ObjectType.TAG] | Type[Tag]'
322-
) -> 'Tag': ...
318+
self, target_type: Literal[GIT_OBJ_TAG, ObjectType.TAG] | type[Tag]
319+
) -> Tag: ...
323320
@overload
324321
def peel(
325-
self, target_type: 'Literal[GIT_OBJ_BLOB, ObjectType.BLOB] | Type[Blob]'
326-
) -> 'Blob': ...
322+
self, target_type: Literal[GIT_OBJ_BLOB, ObjectType.BLOB] | type[Blob]
323+
) -> Blob: ...
327324
@overload
328-
def peel(self, target_type: 'None') -> 'Commit|Tree|Tag|Blob': ...
325+
def peel(self, target_type: None) -> Commit|Tree|Tag|Blob: ...
329326
def read_raw(self) -> bytes: ...
330327
def __eq__(self, other) -> bool: ...
331328
def __ge__(self, other) -> bool: ...
@@ -350,15 +347,15 @@ class Reference:
350347
def delete(self) -> None: ...
351348
def log(self) -> Iterator[RefLogEntry]: ...
352349
@overload
353-
def peel(self, type: 'Literal[GIT_OBJ_COMMIT] | Type[Commit]') -> 'Commit': ...
350+
def peel(self, type: Literal[GIT_OBJ_COMMIT] | type[Commit]) -> Commit: ...
354351
@overload
355-
def peel(self, type: 'Literal[GIT_OBJ_TREE] | Type[Tree]') -> 'Tree': ...
352+
def peel(self, type: Literal[GIT_OBJ_TREE] | type[Tree]) -> Tree: ...
356353
@overload
357-
def peel(self, type: 'Literal[GIT_OBJ_TAG] | Type[Tag]') -> 'Tag': ...
354+
def peel(self, type: Literal[GIT_OBJ_TAG] | type[Tag]) -> Tag: ...
358355
@overload
359-
def peel(self, type: 'Literal[GIT_OBJ_BLOB] | Type[Blob]') -> 'Blob': ...
356+
def peel(self, type: Literal[GIT_OBJ_BLOB] | type[Blob]) -> Blob: ...
360357
@overload
361-
def peel(self, type: 'None' = None) -> 'Commit|Tree|Tag|Blob': ...
358+
def peel(self, type: None = None) -> Commit|Tree|Tag|Blob: ...
362359
def rename(self, new_name: str) -> None: ...
363360
def resolve(self) -> Reference: ...
364361
def set_target(self, target: _OidArg, message: str = ...) -> None: ...
@@ -384,7 +381,7 @@ class Blob(Object):
384381
) -> Patch: ...
385382
def diff_to_buffer(
386383
self,
387-
buffer: Optional[bytes | str] = None,
384+
buffer: bytes | str | None = None,
388385
flag: DiffOption = DiffOption.NORMAL,
389386
old_as_path: str = ...,
390387
buffer_as_path: str = ...,
@@ -395,9 +392,9 @@ class Blob(Object):
395392
ready: Event,
396393
done: Event,
397394
chunk_size: int = DEFAULT_BUFFER_SIZE,
398-
as_path: Optional[str] = None,
395+
as_path: str | None = None,
399396
flags: BlobFilter = BlobFilter.CHECK_FOR_BINARY,
400-
commit_id: Optional[Oid] = None,
397+
commit_id: Oid | None = None,
401398
) -> None: ...
402399
def __buffer__(self, flags: int) -> memoryview: ...
403400
def __release_buffer__(self, buffer: memoryview) -> None: ...
@@ -411,7 +408,7 @@ class Branch(Reference):
411408
def delete(self) -> None: ...
412409
def is_checked_out(self) -> bool: ...
413410
def is_head(self) -> bool: ...
414-
def rename(self, name: str, force: bool = False) -> 'Branch': ... # type: ignore[override]
411+
def rename(self, name: str, force: bool = False) -> Branch: ... # type: ignore[override]
415412

416413
class FetchOptions:
417414
# incomplete
@@ -693,7 +690,7 @@ class Repository:
693690
def create_branch(self, name: str, commit: Commit, force=False) -> Branch: ...
694691
def create_commit(
695692
self,
696-
reference_name: Optional[str],
693+
reference_name: str | None,
697694
author: Signature,
698695
committer: Signature,
699696
message: str | bytes,
@@ -711,7 +708,7 @@ class Repository:
711708
encoding: str = ...,
712709
) -> Oid: ...
713710
def create_commit_with_signature(
714-
self, content: str, signature: str, signature_field: Optional[str] = None
711+
self, content: str, signature: str, signature_field: str | None = None
715712
) -> Oid: ...
716713
def create_note(
717714
self,
@@ -723,10 +720,10 @@ class Repository:
723720
force: bool = False,
724721
) -> Oid: ...
725722
def create_reference_direct(
726-
self, name: str, target: _OidArg, force: bool, message: Optional[str] = None
723+
self, name: str, target: _OidArg, force: bool, message: str | None = None
727724
) -> Reference: ...
728725
def create_reference_symbolic(
729-
self, name: str, target: str, force: bool, message: Optional[str] = None
726+
self, name: str, target: str, force: bool, message: str | None = None
730727
) -> Reference: ...
731728
def create_tag(
732729
self, name: str, oid: _OidArg, type: ObjectType, tagger: Signature, message: str
@@ -801,7 +798,7 @@ class Signature:
801798
email: str,
802799
time: int = -1,
803800
offset: int = 0,
804-
encoding: Optional[str] = None,
801+
encoding: str | None = None,
805802
) -> None: ...
806803
def __eq__(self, other) -> bool: ...
807804
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)