Skip to content

Commit 42bda74

Browse files
ArEnScCrossPr0duct
authored andcommitted
refactor: optimize file handling to avoid redundant buffer conversions
- Change _get_file_details() to return Buffer instead of bytes - Add special handling for memoryview inputs to avoid unnecessary conversion - Support all buffer protocol objects (bytes, bytearray, memoryview, array.array, etc.) - Update type annotations throughout to use Buffer type from typing_extensions - Improve error messages to mention "buffer" instead of just "bytes" This change reduces memory overhead when working with buffer protocol objects by preserving memoryview objects instead of converting them to bytes unnecessarily.
1 parent f0b093e commit 42bda74

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/lmstudio/history.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# Native in 3.13+
3030
TypeIs,
3131
# Native in Python 3.12+
32-
Buffer
32+
Buffer,
3333
)
3434

3535
from msgspec import to_builtins
@@ -555,10 +555,10 @@ def _get_file_details(src: LocalFileInput) -> Tuple[str, Buffer]:
555555
except TypeError:
556556
# Not a buffer protocol object, fall through to other checks
557557
pass
558-
558+
559559
if hasattr(src, "read"):
560560
try:
561-
data: Buffer = src.read()
561+
data = src.read()
562562
except OSError as exc:
563563
# Note: OSError details remain available via raised_exc.__context__
564564
err_msg = f"Error while reading {src!r} ({exc!r})"

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ commands =
5252
ruff check {posargs} src/ tests/ examples/plugins
5353

5454
[testenv:typecheck]
55+
groups = dev
5556
allowlist_externals = mypy
5657
commands =
5758
mypy --strict {posargs} src/ tests/

0 commit comments

Comments
 (0)