Skip to content
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 mypy-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ typing_extensions>=4.6.0
mypy_extensions>=1.0.0
pathspec>=0.9.0
tomli>=1.1.0; python_version<'3.11'
librt>=0.4.0
librt>=0.5.0
12 changes: 6 additions & 6 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from librt.internal import cache_version

import mypy.semanal_main
from mypy.cache import CACHE_VERSION, Buffer, CacheMeta
from mypy.cache import CACHE_VERSION, CacheMeta, ReadBuffer, WriteBuffer
from mypy.checker import TypeChecker
from mypy.error_formatter import OUTPUT_CHOICES, ErrorFormatter
from mypy.errors import CompileError, ErrorInfo, Errors, report_internal_error
Expand Down Expand Up @@ -1343,7 +1343,7 @@ def find_cache_meta(id: str, path: str, manager: BuildManager) -> CacheMeta | No
if meta[0] != cache_version() or meta[1] != CACHE_VERSION:
manager.log(f"Metadata abandoned for {id}: incompatible cache format")
return None
data_io = Buffer(meta[2:])
data_io = ReadBuffer(meta[2:])
m = CacheMeta.read(data_io, data_file)
else:
m = CacheMeta.deserialize(meta, data_file)
Expand Down Expand Up @@ -1594,7 +1594,7 @@ def write_cache(

# Serialize data and analyze interface
if manager.options.fixed_format_cache:
data_io = Buffer()
data_io = WriteBuffer()
tree.write(data_io)
data_bytes = data_io.getvalue()
else:
Expand Down Expand Up @@ -1678,7 +1678,7 @@ def write_cache_meta(meta: CacheMeta, manager: BuildManager, meta_file: str) ->
# Write meta cache file
metastore = manager.metastore
if manager.options.fixed_format_cache:
data_io = Buffer()
data_io = WriteBuffer()
meta.write(data_io)
# Prefix with both low- and high-level cache format versions for future validation.
# TODO: switch to something like librt.internal.write_byte() if this is slow.
Expand Down Expand Up @@ -2111,7 +2111,7 @@ def load_tree(self, temporary: bool = False) -> None:
t0 = time.time()
# TODO: Assert data file wasn't changed.
if isinstance(data, bytes):
data_io = Buffer(data)
data_io = ReadBuffer(data)
self.tree = MypyFile.read(data_io)
else:
self.tree = MypyFile.deserialize(data)
Expand Down Expand Up @@ -2484,7 +2484,7 @@ def write_cache(self) -> tuple[CacheMeta, str] | None:
if self.options.debug_serialize:
try:
if self.manager.options.fixed_format_cache:
data = Buffer()
data = WriteBuffer()
self.tree.write(data)
else:
self.tree.serialize()
Expand Down
55 changes: 28 additions & 27 deletions mypy/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
from typing_extensions import TypeAlias as _TypeAlias

from librt.internal import (
Buffer as Buffer,
ReadBuffer as ReadBuffer,
WriteBuffer as WriteBuffer,
read_bool as read_bool,
read_bytes as read_bytes_bare,
read_float as read_float_bare,
Expand Down Expand Up @@ -165,7 +166,7 @@ def deserialize(cls, meta: dict[str, Any], data_file: str) -> CacheMeta | None:
except (KeyError, ValueError):
return None

def write(self, data: Buffer) -> None:
def write(self, data: WriteBuffer) -> None:
write_str(data, self.id)
write_str(data, self.path)
write_int(data, self.mtime)
Expand All @@ -187,7 +188,7 @@ def write(self, data: Buffer) -> None:
write_json_value(data, self.plugin_data)

@classmethod
def read(cls, data: Buffer, data_file: str) -> CacheMeta | None:
def read(cls, data: ReadBuffer, data_file: str) -> CacheMeta | None:
try:
return CacheMeta(
id=read_str(data),
Expand Down Expand Up @@ -240,7 +241,7 @@ def read(cls, data: Buffer, data_file: str) -> CacheMeta | None:
END_TAG: Final[Tag] = 255


def read_literal(data: Buffer, tag: Tag) -> int | str | bool | float:
def read_literal(data: ReadBuffer, tag: Tag) -> int | str | bool | float:
if tag == LITERAL_INT:
return read_int_bare(data)
elif tag == LITERAL_STR:
Expand All @@ -256,7 +257,7 @@ def read_literal(data: Buffer, tag: Tag) -> int | str | bool | float:

# There is an intentional asymmetry between read and write for literals because
# None and/or complex values are only allowed in some contexts but not in others.
def write_literal(data: Buffer, value: int | str | bool | float | complex | None) -> None:
def write_literal(data: WriteBuffer, value: int | str | bool | float | complex | None) -> None:
if isinstance(value, bool):
write_bool(data, value)
elif isinstance(value, int):
Expand All @@ -276,114 +277,114 @@ def write_literal(data: Buffer, value: int | str | bool | float | complex | None
write_tag(data, LITERAL_NONE)


def read_int(data: Buffer) -> int:
def read_int(data: ReadBuffer) -> int:
assert read_tag(data) == LITERAL_INT
return read_int_bare(data)


def write_int(data: Buffer, value: int) -> None:
def write_int(data: WriteBuffer, value: int) -> None:
write_tag(data, LITERAL_INT)
write_int_bare(data, value)


def read_str(data: Buffer) -> str:
def read_str(data: ReadBuffer) -> str:
assert read_tag(data) == LITERAL_STR
return read_str_bare(data)


def write_str(data: Buffer, value: str) -> None:
def write_str(data: WriteBuffer, value: str) -> None:
write_tag(data, LITERAL_STR)
write_str_bare(data, value)


def read_bytes(data: Buffer) -> bytes:
def read_bytes(data: ReadBuffer) -> bytes:
assert read_tag(data) == LITERAL_BYTES
return read_bytes_bare(data)


def write_bytes(data: Buffer, value: bytes) -> None:
def write_bytes(data: WriteBuffer, value: bytes) -> None:
write_tag(data, LITERAL_BYTES)
write_bytes_bare(data, value)


def read_int_opt(data: Buffer) -> int | None:
def read_int_opt(data: ReadBuffer) -> int | None:
tag = read_tag(data)
if tag == LITERAL_NONE:
return None
assert tag == LITERAL_INT
return read_int_bare(data)


def write_int_opt(data: Buffer, value: int | None) -> None:
def write_int_opt(data: WriteBuffer, value: int | None) -> None:
if value is not None:
write_tag(data, LITERAL_INT)
write_int_bare(data, value)
else:
write_tag(data, LITERAL_NONE)


def read_str_opt(data: Buffer) -> str | None:
def read_str_opt(data: ReadBuffer) -> str | None:
tag = read_tag(data)
if tag == LITERAL_NONE:
return None
assert tag == LITERAL_STR
return read_str_bare(data)


def write_str_opt(data: Buffer, value: str | None) -> None:
def write_str_opt(data: WriteBuffer, value: str | None) -> None:
if value is not None:
write_tag(data, LITERAL_STR)
write_str_bare(data, value)
else:
write_tag(data, LITERAL_NONE)


def read_int_list(data: Buffer) -> list[int]:
def read_int_list(data: ReadBuffer) -> list[int]:
assert read_tag(data) == LIST_INT
size = read_int_bare(data)
return [read_int_bare(data) for _ in range(size)]


def write_int_list(data: Buffer, value: list[int]) -> None:
def write_int_list(data: WriteBuffer, value: list[int]) -> None:
write_tag(data, LIST_INT)
write_int_bare(data, len(value))
for item in value:
write_int_bare(data, item)


def read_str_list(data: Buffer) -> list[str]:
def read_str_list(data: ReadBuffer) -> list[str]:
assert read_tag(data) == LIST_STR
size = read_int_bare(data)
return [read_str_bare(data) for _ in range(size)]


def write_str_list(data: Buffer, value: Sequence[str]) -> None:
def write_str_list(data: WriteBuffer, value: Sequence[str]) -> None:
write_tag(data, LIST_STR)
write_int_bare(data, len(value))
for item in value:
write_str_bare(data, item)


def read_bytes_list(data: Buffer) -> list[bytes]:
def read_bytes_list(data: ReadBuffer) -> list[bytes]:
assert read_tag(data) == LIST_BYTES
size = read_int_bare(data)
return [read_bytes_bare(data) for _ in range(size)]


def write_bytes_list(data: Buffer, value: Sequence[bytes]) -> None:
def write_bytes_list(data: WriteBuffer, value: Sequence[bytes]) -> None:
write_tag(data, LIST_BYTES)
write_int_bare(data, len(value))
for item in value:
write_bytes_bare(data, item)


def read_str_opt_list(data: Buffer) -> list[str | None]:
def read_str_opt_list(data: ReadBuffer) -> list[str | None]:
assert read_tag(data) == LIST_GEN
size = read_int_bare(data)
return [read_str_opt(data) for _ in range(size)]


def write_str_opt_list(data: Buffer, value: list[str | None]) -> None:
def write_str_opt_list(data: WriteBuffer, value: list[str | None]) -> None:
write_tag(data, LIST_GEN)
write_int_bare(data, len(value))
for item in value:
Expand All @@ -393,7 +394,7 @@ def write_str_opt_list(data: Buffer, value: list[str | None]) -> None:
JsonValue: _TypeAlias = Union[None, int, str, bool, list["JsonValue"], dict[str, "JsonValue"]]


def read_json_value(data: Buffer) -> JsonValue:
def read_json_value(data: ReadBuffer) -> JsonValue:
tag = read_tag(data)
if tag == LITERAL_NONE:
return None
Expand All @@ -416,7 +417,7 @@ def read_json_value(data: Buffer) -> JsonValue:

# Currently tuples are used by mypyc plugin. They will be normalized to
# JSON lists after a roundtrip.
def write_json_value(data: Buffer, value: JsonValue | tuple[JsonValue, ...]) -> None:
def write_json_value(data: WriteBuffer, value: JsonValue | tuple[JsonValue, ...]) -> None:
if value is None:
write_tag(data, LITERAL_NONE)
elif isinstance(value, bool):
Expand Down Expand Up @@ -444,13 +445,13 @@ def write_json_value(data: Buffer, value: JsonValue | tuple[JsonValue, ...]) ->

# These are functions for JSON *dictionaries* specifically. Unfortunately, we
# must use imprecise types here, because the callers use imprecise types.
def read_json(data: Buffer) -> dict[str, Any]:
def read_json(data: ReadBuffer) -> dict[str, Any]:
assert read_tag(data) == DICT_STR_GEN
size = read_int_bare(data)
return {read_str_bare(data): read_json_value(data) for _ in range(size)}


def write_json(data: Buffer, value: dict[str, Any]) -> None:
def write_json(data: WriteBuffer, value: dict[str, Any]) -> None:
write_tag(data, DICT_STR_GEN)
write_int_bare(data, len(value))
for key in sorted(value):
Expand Down
4 changes: 2 additions & 2 deletions mypy/exportjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from typing import Any, Union
from typing_extensions import TypeAlias as _TypeAlias

from librt.internal import Buffer
from librt.internal import ReadBuffer

from mypy.nodes import (
FUNCBASE_FLAGS,
Expand Down Expand Up @@ -78,7 +78,7 @@ def __init__(self, *, implicit_names: bool = True) -> None:


def convert_binary_cache_to_json(data: bytes, *, implicit_names: bool = True) -> Json:
tree = MypyFile.read(Buffer(data))
tree = MypyFile.read(ReadBuffer(data))
return convert_mypy_file_to_json(tree, Config(implicit_names=implicit_names))


Expand Down
Loading
Loading