Skip to content

Commit 720881e

Browse files
committed
add typing to blob.py and fix _pygit2.pyi
1 parent 5f2d9ae commit 720881e

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

Diff for: pygit2/_pygit2.pyi

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
from io import IOBase
1+
import queue
2+
import threading
3+
from io import DEFAULT_BUFFER_SIZE, IOBase
24
from typing import Any, Callable, Iterator, Literal, Optional, TypeAlias, overload
35

46
from pygit2.filter import Filter
57

68
from . import Index
79
from .enums import (
810
ApplyLocation,
11+
BlobFilter,
912
BranchType,
1013
DeltaStatus,
1114
DiffFind,
@@ -363,6 +366,16 @@ class Blob(Object):
363366
old_as_path: str = ...,
364367
buffer_as_path: str = ...,
365368
) -> Patch: ...
369+
def _write_to_queue(
370+
self,
371+
queue: queue.Queue[bytes | None],
372+
closed: threading.Event,
373+
chunk_size: int = DEFAULT_BUFFER_SIZE,
374+
*,
375+
as_path: str | None = None,
376+
flags: BlobFilter = BlobFilter.CHECK_FOR_BINARY,
377+
commit_id: Oid | None = None,
378+
) -> None: ...
366379

367380
class Branch(Reference):
368381
branch_name: str

Diff for: pygit2/blob.py

+30-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
from __future__ import annotations
2+
13
import io
24
import threading
35
import time
46
from contextlib import AbstractContextManager
57
from queue import Queue
6-
from typing import Optional
8+
from types import TracebackType
9+
from typing import TYPE_CHECKING, Any, Optional, overload
710

811
from ._pygit2 import Blob, Oid
912
from .enums import BlobFilter
1013

14+
if TYPE_CHECKING:
15+
from _typeshed import WriteableBuffer
16+
1117

1218
class _BlobIO(io.RawIOBase):
1319
"""Low-level wrapper for streaming blob content.
@@ -26,7 +32,7 @@ def __init__(
2632
):
2733
super().__init__()
2834
self._blob = blob
29-
self._queue = Queue(maxsize=1)
35+
self._queue: Queue[bytes | None] | None = Queue(maxsize=1)
3036
self._ready = threading.Event()
3137
self._writer_closed = threading.Event()
3238
self._chunk: Optional[bytes] = None
@@ -42,10 +48,15 @@ def __init__(
4248
)
4349
self._thread.start()
4450

45-
def __exit__(self, exc_type, exc_value, traceback):
51+
def __exit__(
52+
self,
53+
exc_type: type[BaseException] | None,
54+
exc_value: BaseException | None,
55+
traceback: TracebackType | None,
56+
) -> None:
4657
self.close()
4758

48-
def isatty():
59+
def isatty(self):
4960
return False
5061

5162
def readable(self):
@@ -57,7 +68,14 @@ def writable(self):
5768
def seekable(self):
5869
return False
5970

60-
def readinto(self, b, /):
71+
# workaround for type checking
72+
if TYPE_CHECKING:
73+
@overload
74+
def readinto(self, b: WriteableBuffer, /) -> int: ... # type: ignore
75+
76+
def readinto(self, b: Any, /) -> int:
77+
if not self._queue:
78+
raise RuntimeError('Blob I/O is closed')
6179
try:
6280
while self._chunk is None:
6381
self._ready.wait()
@@ -96,7 +114,7 @@ def close(self):
96114
self._queue = None
97115

98116

99-
class BlobIO(io.BufferedReader, AbstractContextManager):
117+
class BlobIO(io.BufferedReader, AbstractContextManager['BlobIO']):
100118
"""Read-only wrapper for streaming blob content.
101119
102120
Supports reading both raw and filtered blob content.
@@ -147,7 +165,12 @@ def __init__(
147165
raw = _BlobIO(blob, as_path=as_path, flags=flags, commit_id=commit_id)
148166
super().__init__(raw)
149167

150-
def __exit__(self, exc_type, exc_value, traceback):
168+
def __exit__(
169+
self,
170+
exc_type: type[BaseException] | None,
171+
exc_value: BaseException | None,
172+
traceback: TracebackType | None,
173+
) -> None:
151174
self.close()
152175

153176

0 commit comments

Comments
 (0)