Skip to content

Commit ea8d377

Browse files
authored
Merge pull request #1084 from onekey-sec/pyright-standard-type-checking-mode
Pyright enable standard type checking mode
2 parents 50b93c4 + a354a12 commit ea8d377

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ exclude = [
177177
".venv",
178178
"build",
179179
]
180-
typeCheckingMode = "basic"
180+
typeCheckingMode = "standard"
181181

182182
[build-system]
183183
build-backend = "hatchling.build"

unblob/extractors/command.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,15 @@ def get_dependencies(self) -> list[str]:
102102
return [self._executable]
103103

104104

105-
class MultiFileCommand(Command, DirectoryExtractor):
105+
class MultiFileCommand(DirectoryExtractor):
106+
def __init__(self, *args, **kwargs):
107+
self._command = Command(*args, **kwargs)
108+
106109
def extract(self, paths: list[Path], outdir: Path):
107-
return super().extract(paths[0], outdir)
110+
return self._command.extract(paths[0], outdir)
111+
112+
def get_dependencies(self) -> list[str]:
113+
return self._command.get_dependencies()
108114

109115

110116
class InvalidCommandTemplate(ValueError):

unblob/file_utils.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import re
99
import shutil
1010
import struct
11+
import sys
1112
import unicodedata
1213
from collections.abc import Iterable, Iterator
1314
from pathlib import Path
@@ -67,7 +68,7 @@ def from_path(cls, path: Path, access=mmap.ACCESS_READ):
6768
m.access = access
6869
return m
6970

70-
def seek(self, pos: int, whence: int = os.SEEK_SET) -> int:
71+
def seek(self, pos: int, whence: int = os.SEEK_SET) -> int: # pyright: ignore[reportIncompatibleMethodOverride]
7172
try:
7273
super().seek(pos, whence)
7374
except ValueError as e:
@@ -91,7 +92,7 @@ def size(self) -> int:
9192
def __enter__(self):
9293
return self
9394

94-
def __exit__(self, _exc_type, _exc_val, _exc_tb):
95+
def __exit__(self, *args):
9596
self.close()
9697

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

103-
def seekable(self) -> bool:
104-
return True # Memory-mapped files are always seekable
104+
if sys.version_info < (3, 13):
105+
106+
def seekable(self) -> Literal[True]:
107+
return True # Memory-mapped files are always seekable
105108

106109

107110
class OffsetFile:

unblob/handlers/compression/_gzip_reader.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def _add_read_data(self, data):
1515
self._crc = zlib.crc32(data, self._crc)
1616
self._stream_size = self._stream_size + len(data)
1717

18-
def read(self):
18+
def read_all(self):
1919
uncompress = b""
2020

2121
while True:
@@ -39,7 +39,7 @@ def read(self):
3939

4040
def read_until_eof(self):
4141
while not self._decompressor.eof:
42-
self.read()
42+
self.read_all()
4343

4444
@property
4545
def unused_data(self):

unblob/models.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from collections.abc import Iterable
55
from enum import Enum
66
from pathlib import Path
7-
from typing import Optional, TypeVar
7+
from typing import Generic, Optional, TypeVar, Union
88

99
import attrs
1010
from structlog import get_logger
@@ -255,7 +255,8 @@ def get_output_dir(self) -> Optional[Path]:
255255

256256

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

440441

441-
class Handler(abc.ABC):
442+
TExtractor = TypeVar("TExtractor", bound=Union[None, Extractor])
443+
444+
445+
class Handler(abc.ABC, Generic[TExtractor]):
442446
"""A file type handler is responsible for searching, validating and "unblobbing" files from Blobs."""
443447

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

450-
EXTRACTOR: Optional[Extractor]
454+
EXTRACTOR: TExtractor
451455

452456
@classmethod
453457
def get_dependencies(cls):
454458
"""Return external command dependencies needed for this handler to work."""
455-
if cls.EXTRACTOR:
459+
if cls.EXTRACTOR is not None:
456460
return cls.EXTRACTOR.get_dependencies()
457461
return []
458462

0 commit comments

Comments
 (0)