Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pyright enable standard type checking mode #1084

Merged
merged 6 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ exclude = [
".venv",
"build",
]
typeCheckingMode = "basic"
typeCheckingMode = "standard"

[build-system]
build-backend = "hatchling.build"
Expand Down
10 changes: 8 additions & 2 deletions unblob/extractors/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,15 @@ def get_dependencies(self) -> list[str]:
return [self._executable]


class MultiFileCommand(Command, DirectoryExtractor):
class MultiFileCommand(DirectoryExtractor):
def __init__(self, *args, **kwargs):
self._command = Command(*args, **kwargs)

def extract(self, paths: list[Path], outdir: Path):
return super().extract(paths[0], outdir)
return self._command.extract(paths[0], outdir)

def get_dependencies(self) -> list[str]:
return self._command.get_dependencies()


class InvalidCommandTemplate(ValueError):
Expand Down
11 changes: 7 additions & 4 deletions unblob/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import re
import shutil
import struct
import sys
import unicodedata
from collections.abc import Iterable, Iterator
from pathlib import Path
Expand Down Expand Up @@ -67,7 +68,7 @@ def from_path(cls, path: Path, access=mmap.ACCESS_READ):
m.access = access
return m

def seek(self, pos: int, whence: int = os.SEEK_SET) -> int:
def seek(self, pos: int, whence: int = os.SEEK_SET) -> int: # pyright: ignore[reportIncompatibleMethodOverride]
try:
super().seek(pos, whence)
except ValueError as e:
Expand All @@ -91,7 +92,7 @@ def size(self) -> int:
def __enter__(self):
return self

def __exit__(self, _exc_type, _exc_val, _exc_tb):
def __exit__(self, *args):
self.close()

def readable(self) -> bool:
Expand All @@ -100,8 +101,10 @@ def readable(self) -> bool:
def writable(self) -> bool:
return self.access in (mmap.ACCESS_WRITE, mmap.ACCESS_COPY)

def seekable(self) -> bool:
return True # Memory-mapped files are always seekable
if sys.version_info < (3, 13):

def seekable(self) -> Literal[True]:
return True # Memory-mapped files are always seekable


class OffsetFile:
Expand Down
4 changes: 2 additions & 2 deletions unblob/handlers/compression/_gzip_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def _add_read_data(self, data):
self._crc = zlib.crc32(data, self._crc)
self._stream_size = self._stream_size + len(data)

def read(self):
def read_all(self):
e3krisztian marked this conversation as resolved.
Show resolved Hide resolved
uncompress = b""

while True:
Expand All @@ -39,7 +39,7 @@ def read(self):

def read_until_eof(self):
while not self._decompressor.eof:
self.read()
self.read_all()

@property
def unused_data(self):
Expand Down
14 changes: 9 additions & 5 deletions unblob/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections.abc import Iterable
from enum import Enum
from pathlib import Path
from typing import Optional, TypeVar
from typing import Generic, Optional, TypeVar, Union

import attrs
from structlog import get_logger
Expand Down Expand Up @@ -255,7 +255,8 @@ def get_output_dir(self) -> Optional[Path]:


class _JSONEncoder(json.JSONEncoder):
def default(self, obj):
def default(self, o):
obj = o
if attrs.has(type(obj)):
extend_attr_output = True
attr_output = attrs.asdict(obj, recurse=not extend_attr_output)
Expand Down Expand Up @@ -438,7 +439,10 @@ def extract(self, paths: list[Path], outdir: Path) -> Optional[ExtractResult]:
return self.EXTRACTOR.extract(paths, outdir)


class Handler(abc.ABC):
TExtractor = TypeVar("TExtractor", bound=Union[None, Extractor])


class Handler(abc.ABC, Generic[TExtractor]):
"""A file type handler is responsible for searching, validating and "unblobbing" files from Blobs."""

NAME: str
Expand All @@ -447,12 +451,12 @@ class Handler(abc.ABC):
# (e.g. tar magic is in the middle of the header)
PATTERN_MATCH_OFFSET: int = 0

EXTRACTOR: Optional[Extractor]
EXTRACTOR: TExtractor

@classmethod
def get_dependencies(cls):
"""Return external command dependencies needed for this handler to work."""
if cls.EXTRACTOR:
if cls.EXTRACTOR is not None:
return cls.EXTRACTOR.get_dependencies()
return []

Expand Down
Loading