Skip to content

Commit 68d8f9d

Browse files
authored
Update librt dependency to 0.5.0 (#20214)
This new version has breaking API and ABI changes (see #20194 for context). This is okay since we haven't made a public release with librt as a dependency yet. (Note that librt is part of the mypy project but distributed separately.)
1 parent ad2b72b commit 68d8f9d

File tree

9 files changed

+141
-144
lines changed

9 files changed

+141
-144
lines changed

mypy-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ typing_extensions>=4.6.0
44
mypy_extensions>=1.0.0
55
pathspec>=0.9.0
66
tomli>=1.1.0; python_version<'3.11'
7-
librt>=0.4.0
7+
librt>=0.5.0

mypy/build.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from librt.internal import cache_version
3232

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

15951595
# Serialize data and analyze interface
15961596
if manager.options.fixed_format_cache:
1597-
data_io = Buffer()
1597+
data_io = WriteBuffer()
15981598
tree.write(data_io)
15991599
data_bytes = data_io.getvalue()
16001600
else:
@@ -1678,7 +1678,7 @@ def write_cache_meta(meta: CacheMeta, manager: BuildManager, meta_file: str) ->
16781678
# Write meta cache file
16791679
metastore = manager.metastore
16801680
if manager.options.fixed_format_cache:
1681-
data_io = Buffer()
1681+
data_io = WriteBuffer()
16821682
meta.write(data_io)
16831683
# Prefix with both low- and high-level cache format versions for future validation.
16841684
# TODO: switch to something like librt.internal.write_byte() if this is slow.
@@ -2111,7 +2111,7 @@ def load_tree(self, temporary: bool = False) -> None:
21112111
t0 = time.time()
21122112
# TODO: Assert data file wasn't changed.
21132113
if isinstance(data, bytes):
2114-
data_io = Buffer(data)
2114+
data_io = ReadBuffer(data)
21152115
self.tree = MypyFile.read(data_io)
21162116
else:
21172117
self.tree = MypyFile.deserialize(data)
@@ -2484,7 +2484,7 @@ def write_cache(self) -> tuple[CacheMeta, str] | None:
24842484
if self.options.debug_serialize:
24852485
try:
24862486
if self.manager.options.fixed_format_cache:
2487-
data = Buffer()
2487+
data = WriteBuffer()
24882488
self.tree.write(data)
24892489
else:
24902490
self.tree.serialize()

mypy/cache.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
from typing_extensions import TypeAlias as _TypeAlias
5353

5454
from librt.internal import (
55-
Buffer as Buffer,
55+
ReadBuffer as ReadBuffer,
56+
WriteBuffer as WriteBuffer,
5657
read_bool as read_bool,
5758
read_bytes as read_bytes_bare,
5859
read_float as read_float_bare,
@@ -165,7 +166,7 @@ def deserialize(cls, meta: dict[str, Any], data_file: str) -> CacheMeta | None:
165166
except (KeyError, ValueError):
166167
return None
167168

168-
def write(self, data: Buffer) -> None:
169+
def write(self, data: WriteBuffer) -> None:
169170
write_str(data, self.id)
170171
write_str(data, self.path)
171172
write_int(data, self.mtime)
@@ -187,7 +188,7 @@ def write(self, data: Buffer) -> None:
187188
write_json_value(data, self.plugin_data)
188189

189190
@classmethod
190-
def read(cls, data: Buffer, data_file: str) -> CacheMeta | None:
191+
def read(cls, data: ReadBuffer, data_file: str) -> CacheMeta | None:
191192
try:
192193
return CacheMeta(
193194
id=read_str(data),
@@ -240,7 +241,7 @@ def read(cls, data: Buffer, data_file: str) -> CacheMeta | None:
240241
END_TAG: Final[Tag] = 255
241242

242243

243-
def read_literal(data: Buffer, tag: Tag) -> int | str | bool | float:
244+
def read_literal(data: ReadBuffer, tag: Tag) -> int | str | bool | float:
244245
if tag == LITERAL_INT:
245246
return read_int_bare(data)
246247
elif tag == LITERAL_STR:
@@ -256,7 +257,7 @@ def read_literal(data: Buffer, tag: Tag) -> int | str | bool | float:
256257

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

278279

279-
def read_int(data: Buffer) -> int:
280+
def read_int(data: ReadBuffer) -> int:
280281
assert read_tag(data) == LITERAL_INT
281282
return read_int_bare(data)
282283

283284

284-
def write_int(data: Buffer, value: int) -> None:
285+
def write_int(data: WriteBuffer, value: int) -> None:
285286
write_tag(data, LITERAL_INT)
286287
write_int_bare(data, value)
287288

288289

289-
def read_str(data: Buffer) -> str:
290+
def read_str(data: ReadBuffer) -> str:
290291
assert read_tag(data) == LITERAL_STR
291292
return read_str_bare(data)
292293

293294

294-
def write_str(data: Buffer, value: str) -> None:
295+
def write_str(data: WriteBuffer, value: str) -> None:
295296
write_tag(data, LITERAL_STR)
296297
write_str_bare(data, value)
297298

298299

299-
def read_bytes(data: Buffer) -> bytes:
300+
def read_bytes(data: ReadBuffer) -> bytes:
300301
assert read_tag(data) == LITERAL_BYTES
301302
return read_bytes_bare(data)
302303

303304

304-
def write_bytes(data: Buffer, value: bytes) -> None:
305+
def write_bytes(data: WriteBuffer, value: bytes) -> None:
305306
write_tag(data, LITERAL_BYTES)
306307
write_bytes_bare(data, value)
307308

308309

309-
def read_int_opt(data: Buffer) -> int | None:
310+
def read_int_opt(data: ReadBuffer) -> int | None:
310311
tag = read_tag(data)
311312
if tag == LITERAL_NONE:
312313
return None
313314
assert tag == LITERAL_INT
314315
return read_int_bare(data)
315316

316317

317-
def write_int_opt(data: Buffer, value: int | None) -> None:
318+
def write_int_opt(data: WriteBuffer, value: int | None) -> None:
318319
if value is not None:
319320
write_tag(data, LITERAL_INT)
320321
write_int_bare(data, value)
321322
else:
322323
write_tag(data, LITERAL_NONE)
323324

324325

325-
def read_str_opt(data: Buffer) -> str | None:
326+
def read_str_opt(data: ReadBuffer) -> str | None:
326327
tag = read_tag(data)
327328
if tag == LITERAL_NONE:
328329
return None
329330
assert tag == LITERAL_STR
330331
return read_str_bare(data)
331332

332333

333-
def write_str_opt(data: Buffer, value: str | None) -> None:
334+
def write_str_opt(data: WriteBuffer, value: str | None) -> None:
334335
if value is not None:
335336
write_tag(data, LITERAL_STR)
336337
write_str_bare(data, value)
337338
else:
338339
write_tag(data, LITERAL_NONE)
339340

340341

341-
def read_int_list(data: Buffer) -> list[int]:
342+
def read_int_list(data: ReadBuffer) -> list[int]:
342343
assert read_tag(data) == LIST_INT
343344
size = read_int_bare(data)
344345
return [read_int_bare(data) for _ in range(size)]
345346

346347

347-
def write_int_list(data: Buffer, value: list[int]) -> None:
348+
def write_int_list(data: WriteBuffer, value: list[int]) -> None:
348349
write_tag(data, LIST_INT)
349350
write_int_bare(data, len(value))
350351
for item in value:
351352
write_int_bare(data, item)
352353

353354

354-
def read_str_list(data: Buffer) -> list[str]:
355+
def read_str_list(data: ReadBuffer) -> list[str]:
355356
assert read_tag(data) == LIST_STR
356357
size = read_int_bare(data)
357358
return [read_str_bare(data) for _ in range(size)]
358359

359360

360-
def write_str_list(data: Buffer, value: Sequence[str]) -> None:
361+
def write_str_list(data: WriteBuffer, value: Sequence[str]) -> None:
361362
write_tag(data, LIST_STR)
362363
write_int_bare(data, len(value))
363364
for item in value:
364365
write_str_bare(data, item)
365366

366367

367-
def read_bytes_list(data: Buffer) -> list[bytes]:
368+
def read_bytes_list(data: ReadBuffer) -> list[bytes]:
368369
assert read_tag(data) == LIST_BYTES
369370
size = read_int_bare(data)
370371
return [read_bytes_bare(data) for _ in range(size)]
371372

372373

373-
def write_bytes_list(data: Buffer, value: Sequence[bytes]) -> None:
374+
def write_bytes_list(data: WriteBuffer, value: Sequence[bytes]) -> None:
374375
write_tag(data, LIST_BYTES)
375376
write_int_bare(data, len(value))
376377
for item in value:
377378
write_bytes_bare(data, item)
378379

379380

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

385386

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

395396

396-
def read_json_value(data: Buffer) -> JsonValue:
397+
def read_json_value(data: ReadBuffer) -> JsonValue:
397398
tag = read_tag(data)
398399
if tag == LITERAL_NONE:
399400
return None
@@ -416,7 +417,7 @@ def read_json_value(data: Buffer) -> JsonValue:
416417

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

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

452453

453-
def write_json(data: Buffer, value: dict[str, Any]) -> None:
454+
def write_json(data: WriteBuffer, value: dict[str, Any]) -> None:
454455
write_tag(data, DICT_STR_GEN)
455456
write_int_bare(data, len(value))
456457
for key in sorted(value):

mypy/exportjson.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from typing import Any, Union
2020
from typing_extensions import TypeAlias as _TypeAlias
2121

22-
from librt.internal import Buffer
22+
from librt.internal import ReadBuffer
2323

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

7979

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

8484

0 commit comments

Comments
 (0)