Skip to content

Commit

Permalink
Fix type hints
Browse files Browse the repository at this point in the history
This contains the following changes:

- `SizeLimitedFile` uses common conventions, types and defaults.
- `_fileobj_name` -> `_obj_name`, as it's not related to files.
- `rohmufile.write_file` only requires `HasRead` for the input file.
- `HasRead`, `HasWrite` use Python conventions, types and defaults.
- `FileLike` uses Python conventions, types and defaults.
- New `HasName`.

Signed-off-by: Samuel Giffard <[email protected]>
  • Loading branch information
Samuel Giffard committed Oct 11, 2023
1 parent 7b575b8 commit 2f6cf3f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
14 changes: 8 additions & 6 deletions rohmu/delta/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,23 @@ def __init__(self, *, path: AnyPath, file_size: int) -> None:
def __enter__(self) -> SizeLimitedFile:
return self

def __exit__(self, t: Optional[Type[BaseException]], v: Optional[BaseException], tb: Optional[TracebackType]) -> None:
def __exit__(
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
) -> None:
self._f.close()

def read(self, n: Optional[int] = None) -> bytes:
def read(self, n: int = -1, /) -> bytes:
can_read = max(0, self._file_size - self._f.tell())
if n is None:
if n == -1:
n = can_read
n = min(can_read, n)
return self._f.read(n)

def seek(self, ofs: int, whence: int = 0) -> int:
def seek(self, offset: int, whence: int = 0, /) -> int:
if whence == os.SEEK_END:
ofs += self._file_size
offset += self._file_size
whence = os.SEEK_SET
return self._f.seek(ofs, whence)
return self._f.seek(offset, whence)


class SnapshotHash(DeltaModel):
Expand Down
4 changes: 2 additions & 2 deletions rohmu/encryptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .common.constants import IO_BLOCK_SIZE
from .errors import UninitializedError
from .filewrap import FileWrap, Sink, Stream
from .typing import BinaryData, FileLike, HasRead, HasWrite
from .typing import BinaryData, FileLike, HasRead, HasSeek, HasWrite
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
Expand Down Expand Up @@ -258,7 +258,7 @@ def _reset(self) -> None:
self.state = "OPEN"

@classmethod
def _file_size(cls, file: FileLike) -> int:
def _file_size(cls, file: HasSeek) -> int:
current_offset = file.seek(0, os.SEEK_SET)
file_end_offset = file.seek(0, os.SEEK_END)
file.seek(current_offset, os.SEEK_SET)
Expand Down
12 changes: 6 additions & 6 deletions rohmu/rohmufile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
from .encryptor import DecryptorFile, DecryptSink, EncryptorFile
from .errors import InvalidConfigurationError
from .filewrap import ThrottleSink
from .typing import FileLike, HasWrite, Metadata
from .typing import FileLike, HasRead, HasWrite, Metadata
from contextlib import suppress
from inspect import signature
from rohmu.object_storage.base import IncrementalProgressCallbackType
from typing import Callable, Optional, Union
from typing import Any, Callable, Optional, Union

import time


def _fileobj_name(input_obj: FileLike) -> str:
def _obj_name(input_obj: Any) -> str:
if hasattr(input_obj, "name"):
name = getattr(input_obj, "name")
return f"open file {repr(name)}"
Expand Down Expand Up @@ -133,7 +133,7 @@ def read_file(
action,
original_size,
result_size,
_fileobj_name(output_obj),
_obj_name(output_obj),
time.monotonic() - start_time,
)

Expand All @@ -159,7 +159,7 @@ def file_writer(

def write_file(
*,
input_obj: FileLike,
input_obj: HasRead,
output_obj: FileLike,
progress_callback: IncrementalProgressCallbackType = None,
compression_algorithm: Optional[str] = None,
Expand Down Expand Up @@ -208,7 +208,7 @@ def write_file(
log_func=log_func,
original_size=original_size,
result_size=result_size,
source_name=_fileobj_name(input_obj),
source_name=_obj_name(input_obj),
)

return original_size, result_size
Expand Down
15 changes: 12 additions & 3 deletions rohmu/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def fileno(self) -> int:


class HasRead(Protocol):
def read(self, n: Optional[int] = -1) -> bytes:
def read(self, n: int = -1, /) -> bytes:
...


Expand All @@ -48,6 +48,15 @@ def write(self, data: BinaryData) -> int:
...


class HasSeek(Protocol):
def seek(self, offset: int, whence: int = 0, /) -> int:
...


class HasName(Protocol):
name: str


class FileLike(Protocol):
def __enter__(self) -> FileLike:
...
Expand All @@ -57,7 +66,7 @@ def __exit__(
) -> None:
...

def read(self, n: Optional[int] = -1) -> bytes:
def read(self, n: int = -1, /) -> bytes:
...

def flush(self) -> None:
Expand All @@ -75,7 +84,7 @@ def fileno(self) -> int:
def tell(self) -> int:
...

def seek(self, offset: int, whence: int) -> int:
def seek(self, offset: int, whence: int = 0, /) -> int:
...


Expand Down
3 changes: 1 addition & 2 deletions rohmu/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,10 @@ def isatty(self) -> bool:
def tell(self) -> int:
return self.raw_stream.tell()

def seek(self, offset: int, whence: int = 0) -> int:
def seek(self, offset: int, whence: int = 0, /) -> int:
"""Seek the underlying file if this operation is supported.
NOTE: Calling this method will reset the bytes_read field!
"""
result = self.raw_stream.seek(offset, whence)

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

def test_fileobj_name(tmpdir: Any) -> None:
with NamedTemporaryFile(dir=tmpdir, suffix="foo") as raw_output_obj:
result = rohmufile._fileobj_name(raw_output_obj) # type: ignore# pylint: disable=protected-access
result = rohmufile._obj_name(raw_output_obj) # type: ignore# pylint: disable=protected-access
assert result.startswith("open file ")
assert "foo" in result

Expand Down

0 comments on commit 2f6cf3f

Please sign in to comment.