diff --git a/mypy-requirements.txt b/mypy-requirements.txt index 7c83178ae1eb..a69d31088e55 100644 --- a/mypy-requirements.txt +++ b/mypy-requirements.txt @@ -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 diff --git a/mypy/build.py b/mypy/build.py index e9c50ce6b224..853e54e445ac 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -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 @@ -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) @@ -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: @@ -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. @@ -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) @@ -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() diff --git a/mypy/cache.py b/mypy/cache.py index 900815b9f7e7..ad12fd96f1fa 100644 --- a/mypy/cache.py +++ b/mypy/cache.py @@ -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, @@ -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) @@ -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), @@ -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: @@ -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): @@ -276,37 +277,37 @@ 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 @@ -314,7 +315,7 @@ def read_int_opt(data: Buffer) -> int | None: 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) @@ -322,7 +323,7 @@ def write_int_opt(data: Buffer, value: int | None) -> None: 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 @@ -330,7 +331,7 @@ def read_str_opt(data: Buffer) -> str | None: 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) @@ -338,52 +339,52 @@ def write_str_opt(data: Buffer, value: str | None) -> None: 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: @@ -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 @@ -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): @@ -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): diff --git a/mypy/exportjson.py b/mypy/exportjson.py index 09945f0ef28f..dfc1cf5abbc6 100644 --- a/mypy/exportjson.py +++ b/mypy/exportjson.py @@ -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, @@ -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)) diff --git a/mypy/nodes.py b/mypy/nodes.py index 13ba011eebc0..e7d7e84d5ac2 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -28,8 +28,9 @@ LIST_STR, LITERAL_COMPLEX, LITERAL_NONE, - Buffer, + ReadBuffer, Tag, + WriteBuffer, read_bool, read_int, read_int_list, @@ -285,11 +286,11 @@ def deserialize(cls, data: JsonDict) -> SymbolNode: return method(data) raise NotImplementedError(f"unexpected .class {classname}") - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: raise NotImplementedError(f"Cannot serialize {self.__class__.__name__} instance") @classmethod - def read(cls, data: Buffer) -> SymbolNode: + def read(cls, data: ReadBuffer) -> SymbolNode: raise NotImplementedError(f"Cannot deserialize {cls.__name__} instance") @@ -441,7 +442,7 @@ def deserialize(cls, data: JsonDict) -> MypyFile: tree.future_import_flags = set(data["future_import_flags"]) return tree - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, MYPY_FILE) write_str(data, self._fullname) self.names.write(data, self._fullname) @@ -452,7 +453,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> MypyFile: + def read(cls, data: ReadBuffer) -> MypyFile: assert read_tag(data) == MYPY_FILE tree = MypyFile([], []) tree._fullname = read_str(data) @@ -737,7 +738,7 @@ def deserialize(cls, data: JsonDict) -> OverloadedFuncDef: # NOTE: res.info will be set in the fixup phase. return res - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, OVERLOADED_FUNC_DEF) write_tag(data, LIST_GEN) write_int_bare(data, len(self.items)) @@ -755,7 +756,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> OverloadedFuncDef: + def read(cls, data: ReadBuffer) -> OverloadedFuncDef: assert read_tag(data) == LIST_GEN res = OverloadedFuncDef([read_overload_part(data) for _ in range(read_int_bare(data))]) typ = mypy.types.read_type_opt(data) @@ -1052,7 +1053,7 @@ def deserialize(cls, data: JsonDict) -> FuncDef: del ret.min_args return ret - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, FUNC_DEF) write_str(data, self._name) mypy.types.write_type_opt(data, self.type) @@ -1070,7 +1071,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> FuncDef: + def read(cls, data: ReadBuffer) -> FuncDef: name = read_str(data) typ: mypy.types.FunctionLike | None = None tag = read_tag(data) @@ -1168,7 +1169,7 @@ def deserialize(cls, data: JsonDict) -> Decorator: dec.is_overload = data["is_overload"] return dec - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, DECORATOR) self.func.write(data) self.var.write(data) @@ -1176,7 +1177,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> Decorator: + def read(cls, data: ReadBuffer) -> Decorator: assert read_tag(data) == FUNC_DEF func = FuncDef.read(data) assert read_tag(data) == VAR @@ -1362,7 +1363,7 @@ def deserialize(cls, data: JsonDict) -> Var: v.final_value = data.get("final_value") return v - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, VAR) write_str(data, self._name) mypy.types.write_type_opt(data, self.type) @@ -1373,7 +1374,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> Var: + def read(cls, data: ReadBuffer) -> Var: name = read_str(data) typ = mypy.types.read_type_opt(data) v = Var(name, typ) @@ -1504,7 +1505,7 @@ def deserialize(cls, data: JsonDict) -> ClassDef: res.fullname = data["fullname"] return res - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, CLASS_DEF) write_str(data, self.name) mypy.types.write_type_list(data, self.type_vars) @@ -1512,7 +1513,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> ClassDef: + def read(cls, data: ReadBuffer) -> ClassDef: res = ClassDef(read_str(data), Block([]), mypy.types.read_type_var_likes(data)) res.fullname = read_str(data) assert read_tag(data) == END_TAG @@ -2975,7 +2976,7 @@ def deserialize(cls, data: JsonDict) -> TypeVarExpr: data["variance"], ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, TYPE_VAR_EXPR) write_str(data, self._name) write_str(data, self._fullname) @@ -2986,7 +2987,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> TypeVarExpr: + def read(cls, data: ReadBuffer) -> TypeVarExpr: ret = TypeVarExpr( read_str(data), read_str(data), @@ -3028,7 +3029,7 @@ def deserialize(cls, data: JsonDict) -> ParamSpecExpr: data["variance"], ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, PARAM_SPEC_EXPR) write_str(data, self._name) write_str(data, self._fullname) @@ -3038,7 +3039,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> ParamSpecExpr: + def read(cls, data: ReadBuffer) -> ParamSpecExpr: ret = ParamSpecExpr( read_str(data), read_str(data), @@ -3099,7 +3100,7 @@ def deserialize(cls, data: JsonDict) -> TypeVarTupleExpr: data["variance"], ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, TYPE_VAR_TUPLE_EXPR) self.tuple_fallback.write(data) write_str(data, self._name) @@ -3110,7 +3111,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> TypeVarTupleExpr: + def read(cls, data: ReadBuffer) -> TypeVarTupleExpr: assert read_tag(data) == mypy.types.INSTANCE fallback = mypy.types.Instance.read(data) ret = TypeVarTupleExpr( @@ -3994,7 +3995,7 @@ def deserialize(cls, data: JsonDict) -> TypeInfo: ti.deprecated = data.get("deprecated") return ti - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, TYPE_INFO) self.names.write(data, self.fullname) self.defn.write(data) @@ -4028,7 +4029,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> TypeInfo: + def read(cls, data: ReadBuffer) -> TypeInfo: names = SymbolTable.read(data) assert read_tag(data) == CLASS_DEF defn = ClassDef.read(data) @@ -4363,7 +4364,7 @@ def deserialize(cls, data: JsonDict) -> TypeAlias: python_3_12_type_alias=python_3_12_type_alias, ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, TYPE_ALIAS) write_str(data, self._fullname) write_str(data, self.module) @@ -4375,7 +4376,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> TypeAlias: + def read(cls, data: ReadBuffer) -> TypeAlias: fullname = read_str(data) module = read_str(data) target = mypy.types.read_type(data) @@ -4652,7 +4653,7 @@ def deserialize(cls, data: JsonDict) -> SymbolTableNode: stnode.plugin_generated = data["plugin_generated"] return stnode - def write(self, data: Buffer, prefix: str, name: str) -> None: + def write(self, data: WriteBuffer, prefix: str, name: str) -> None: write_tag(data, SYMBOL_TABLE_NODE) write_int(data, self.kind) write_bool(data, self.module_hidden) @@ -4684,7 +4685,7 @@ def write(self, data: Buffer, prefix: str, name: str) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> SymbolTableNode: + def read(cls, data: ReadBuffer) -> SymbolTableNode: assert read_tag(data) == SYMBOL_TABLE_NODE sym = SymbolTableNode(read_int(data), None) sym.module_hidden = read_bool(data) @@ -4750,7 +4751,7 @@ def deserialize(cls, data: JsonDict) -> SymbolTable: st[key] = SymbolTableNode.deserialize(value) return st - def write(self, data: Buffer, fullname: str) -> None: + def write(self, data: WriteBuffer, fullname: str) -> None: size = 0 for key, value in self.items(): # Skip __builtins__: it's a reference to the builtins @@ -4771,7 +4772,7 @@ def write(self, data: Buffer, fullname: str) -> None: value.write(data, fullname, key) @classmethod - def read(cls, data: Buffer) -> SymbolTable: + def read(cls, data: ReadBuffer) -> SymbolTable: assert read_tag(data) == DICT_STR_GEN size = read_int_bare(data) return SymbolTable( @@ -4828,7 +4829,7 @@ def deserialize(cls, data: JsonDict) -> DataclassTransformSpec: field_specifiers=tuple(data.get("field_specifiers", [])), ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, DT_SPEC) write_bool(data, self.eq_default) write_bool(data, self.order_default) @@ -4838,7 +4839,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> DataclassTransformSpec: + def read(cls, data: ReadBuffer) -> DataclassTransformSpec: ret = DataclassTransformSpec( eq_default=read_bool(data), order_default=read_bool(data), @@ -4859,12 +4860,12 @@ def set_flags(node: Node, flags: list[str]) -> None: setattr(node, name, True) -def write_flags(data: Buffer, node: SymbolNode, flags: list[str]) -> None: +def write_flags(data: WriteBuffer, node: SymbolNode, flags: list[str]) -> None: for flag in flags: write_bool(data, getattr(node, flag)) -def read_flags(data: Buffer, node: SymbolNode, flags: list[str]) -> None: +def read_flags(data: ReadBuffer, node: SymbolNode, flags: list[str]) -> None: for flag in flags: if read_bool(data): setattr(node, flag, True) @@ -5002,7 +5003,7 @@ def local_definitions( SYMBOL_TABLE_NODE: Final[Tag] = 61 -def read_symbol(data: Buffer) -> SymbolNode: +def read_symbol(data: ReadBuffer) -> SymbolNode: tag = read_tag(data) # The branches here are ordered manually by type "popularity". if tag == VAR: @@ -5026,7 +5027,7 @@ def read_symbol(data: Buffer) -> SymbolNode: assert False, f"Unknown symbol tag {tag}" -def read_overload_part(data: Buffer, tag: Tag | None = None) -> OverloadPart: +def read_overload_part(data: ReadBuffer, tag: Tag | None = None) -> OverloadPart: if tag is None: tag = read_tag(data) if tag == DECORATOR: diff --git a/mypy/types.py b/mypy/types.py index 7a8343097204..056b99cc3f91 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -23,8 +23,9 @@ EXTRA_ATTRS, LIST_GEN, LITERAL_NONE, - Buffer, + ReadBuffer, Tag, + WriteBuffer, read_bool, read_int, read_int_list, @@ -312,11 +313,11 @@ def serialize(self) -> JsonDict | str: def deserialize(cls, data: JsonDict) -> Type: raise NotImplementedError(f"Cannot deserialize {cls.__name__} instance") - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: raise NotImplementedError(f"Cannot serialize {self.__class__.__name__} instance") @classmethod - def read(cls, data: Buffer) -> Type: + def read(cls, data: ReadBuffer) -> Type: raise NotImplementedError(f"Cannot deserialize {cls.__name__} instance") def is_singleton_type(self) -> bool: @@ -449,7 +450,7 @@ def deserialize(cls, data: JsonDict) -> TypeAliasType: alias.type_ref = data["type_ref"] return alias - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, TYPE_ALIAS_TYPE) write_type_list(data, self.args) assert self.alias is not None @@ -457,7 +458,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> TypeAliasType: + def read(cls, data: ReadBuffer) -> TypeAliasType: alias = TypeAliasType(None, read_type_list(data)) alias.type_ref = read_str(data) assert read_tag(data) == END_TAG @@ -730,7 +731,7 @@ def deserialize(cls, data: JsonDict) -> TypeVarType: variance=data["variance"], ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, TYPE_VAR_TYPE) write_str(data, self.name) write_str(data, self.fullname) @@ -743,7 +744,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> TypeVarType: + def read(cls, data: ReadBuffer) -> TypeVarType: ret = TypeVarType( read_str(data), read_str(data), @@ -885,7 +886,7 @@ def deserialize(cls, data: JsonDict) -> ParamSpecType: prefix=Parameters.deserialize(data["prefix"]), ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, PARAM_SPEC_TYPE) self.prefix.write(data) write_str(data, self.name) @@ -898,7 +899,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> ParamSpecType: + def read(cls, data: ReadBuffer) -> ParamSpecType: assert read_tag(data) == PARAMETERS prefix = Parameters.read(data) ret = ParamSpecType( @@ -968,7 +969,7 @@ def deserialize(cls, data: JsonDict) -> TypeVarTupleType: min_len=data["min_len"], ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, TYPE_VAR_TUPLE_TYPE) self.tuple_fallback.write(data) write_str(data, self.name) @@ -981,7 +982,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> TypeVarTupleType: + def read(cls, data: ReadBuffer) -> TypeVarTupleType: assert read_tag(data) == INSTANCE fallback = Instance.read(data) ret = TypeVarTupleType( @@ -1127,7 +1128,7 @@ def deserialize(cls, data: JsonDict) -> UnboundType: original_str_fallback=data["expr_fallback"], ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, UNBOUND_TYPE) write_str(data, self.name) write_type_list(data, self.args) @@ -1136,7 +1137,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> UnboundType: + def read(cls, data: ReadBuffer) -> UnboundType: ret = UnboundType( read_str(data), read_type_list(data), @@ -1240,13 +1241,13 @@ def accept(self, visitor: TypeVisitor[T]) -> T: def serialize(self) -> JsonDict: return {".class": "UnpackType", "type": self.type.serialize()} - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, UNPACK_TYPE) self.type.write(data) write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> UnpackType: + def read(cls, data: ReadBuffer) -> UnpackType: ret = UnpackType(read_type(data)) assert read_tag(data) == END_TAG return ret @@ -1352,7 +1353,7 @@ def deserialize(cls, data: JsonDict) -> AnyType: data["missing_import_name"], ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, ANY_TYPE) write_type_opt(data, self.source_any) write_int(data, self.type_of_any) @@ -1360,7 +1361,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> AnyType: + def read(cls, data: ReadBuffer) -> AnyType: tag = read_tag(data) if tag != LITERAL_NONE: assert tag == ANY_TYPE @@ -1417,12 +1418,12 @@ def deserialize(cls, data: JsonDict) -> UninhabitedType: assert data[".class"] == "UninhabitedType" return UninhabitedType() - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, UNINHABITED_TYPE) write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> UninhabitedType: + def read(cls, data: ReadBuffer) -> UninhabitedType: assert read_tag(data) == END_TAG return UninhabitedType() @@ -1458,12 +1459,12 @@ def deserialize(cls, data: JsonDict) -> NoneType: assert data[".class"] == "NoneType" return NoneType() - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, NONE_TYPE) write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> NoneType: + def read(cls, data: ReadBuffer) -> NoneType: assert read_tag(data) == END_TAG return NoneType() @@ -1514,13 +1515,13 @@ def deserialize(cls, data: JsonDict) -> DeletedType: assert data[".class"] == "DeletedType" return DeletedType(data["source"]) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, DELETED_TYPE) write_str_opt(data, self.source) write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> DeletedType: + def read(cls, data: ReadBuffer) -> DeletedType: ret = DeletedType(read_str_opt(data)) assert read_tag(data) == END_TAG return ret @@ -1580,7 +1581,7 @@ def deserialize(cls, data: JsonDict) -> ExtraAttrs: data["mod_name"], ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, EXTRA_ATTRS) write_type_map(data, self.attrs) write_str_list(data, sorted(self.immutable)) @@ -1588,7 +1589,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> ExtraAttrs: + def read(cls, data: ReadBuffer) -> ExtraAttrs: ret = ExtraAttrs(read_type_map(data), set(read_str_list(data)), read_str_opt(data)) assert read_tag(data) == END_TAG return ret @@ -1729,7 +1730,7 @@ def deserialize(cls, data: JsonDict | str) -> Instance: inst.extra_attrs = ExtraAttrs.deserialize(data["extra_attrs"]) return inst - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, INSTANCE) if not self.args and not self.last_known_value and not self.extra_attrs: type_ref = self.type.fullname @@ -1758,7 +1759,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> Instance: + def read(cls, data: ReadBuffer) -> Instance: tag = read_tag(data) # This is quite verbose, but this is very hot code, so we are not # using dictionary lookups here. @@ -2100,7 +2101,7 @@ def deserialize(cls, data: JsonDict) -> Parameters: imprecise_arg_kinds=data["imprecise_arg_kinds"], ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, PARAMETERS) write_type_list(data, self.arg_types) write_int_list(data, [int(x.value) for x in self.arg_kinds]) @@ -2110,7 +2111,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> Parameters: + def read(cls, data: ReadBuffer) -> Parameters: ret = Parameters( read_type_list(data), # This is a micro-optimization until mypyc gets dedicated enum support. Otherwise, @@ -2629,7 +2630,7 @@ def deserialize(cls, data: JsonDict) -> CallableType: unpack_kwargs=data["unpack_kwargs"], ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, CALLABLE_TYPE) self.fallback.write(data) write_type_list(data, self.arg_types) @@ -2649,7 +2650,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> CallableType: + def read(cls, data: ReadBuffer) -> CallableType: assert read_tag(data) == INSTANCE fallback = Instance.read(data) ret = CallableType( @@ -2747,13 +2748,13 @@ def deserialize(cls, data: JsonDict) -> Overloaded: assert data[".class"] == "Overloaded" return Overloaded([CallableType.deserialize(t) for t in data["items"]]) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, OVERLOADED) write_type_list(data, self.items) write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> Overloaded: + def read(cls, data: ReadBuffer) -> Overloaded: items = [] assert read_tag(data) == LIST_GEN for _ in range(read_int_bare(data)): @@ -2858,7 +2859,7 @@ def deserialize(cls, data: JsonDict) -> TupleType: implicit=data["implicit"], ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, TUPLE_TYPE) self.partial_fallback.write(data) write_type_list(data, self.items) @@ -2866,7 +2867,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> TupleType: + def read(cls, data: ReadBuffer) -> TupleType: assert read_tag(data) == INSTANCE fallback = Instance.read(data) ret = TupleType(read_type_list(data), fallback, implicit=read_bool(data)) @@ -3043,7 +3044,7 @@ def deserialize(cls, data: JsonDict) -> TypedDictType: Instance.deserialize(data["fallback"]), ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, TYPED_DICT_TYPE) self.fallback.write(data) write_type_map(data, self.items) @@ -3052,7 +3053,7 @@ def write(self, data: Buffer) -> None: write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> TypedDictType: + def read(cls, data: ReadBuffer) -> TypedDictType: assert read_tag(data) == INSTANCE fallback = Instance.read(data) ret = TypedDictType( @@ -3309,14 +3310,14 @@ def deserialize(cls, data: JsonDict) -> LiteralType: assert data[".class"] == "LiteralType" return LiteralType(value=data["value"], fallback=Instance.deserialize(data["fallback"])) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, LITERAL_TYPE) self.fallback.write(data) write_literal(data, self.value) write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> LiteralType: + def read(cls, data: ReadBuffer) -> LiteralType: assert read_tag(data) == INSTANCE fallback = Instance.read(data) tag = read_tag(data) @@ -3425,14 +3426,14 @@ def deserialize(cls, data: JsonDict) -> UnionType: uses_pep604_syntax=data["uses_pep604_syntax"], ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, UNION_TYPE) write_type_list(data, self.items) write_bool(data, self.uses_pep604_syntax) write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> UnionType: + def read(cls, data: ReadBuffer) -> UnionType: ret = UnionType(read_type_list(data), uses_pep604_syntax=read_bool(data)) assert read_tag(data) == END_TAG return ret @@ -3594,13 +3595,13 @@ def deserialize(cls, data: JsonDict) -> Type: deserialize_type(data["item"]), is_type_form=data["is_type_form"] ) - def write(self, data: Buffer) -> None: + def write(self, data: WriteBuffer) -> None: write_tag(data, TYPE_TYPE) self.item.write(data) write_tag(data, END_TAG) @classmethod - def read(cls, data: Buffer) -> Type: + def read(cls, data: ReadBuffer) -> Type: ret = TypeType.make_normalized(read_type(data)) assert read_tag(data) == END_TAG return ret @@ -4303,7 +4304,7 @@ def type_vars_as_args(type_vars: Sequence[TypeVarLikeType]) -> tuple[Type, ...]: PARAMETERS: Final[Tag] = 117 -def read_type(data: Buffer, tag: Tag | None = None) -> Type: +def read_type(data: ReadBuffer, tag: Tag | None = None) -> Type: if tag is None: tag = read_tag(data) # The branches here are ordered manually by type "popularity". @@ -4348,7 +4349,7 @@ def read_type(data: Buffer, tag: Tag | None = None) -> Type: assert False, f"Unknown type tag {tag}" -def read_function_like(data: Buffer, tag: Tag) -> FunctionLike: +def read_function_like(data: ReadBuffer, tag: Tag) -> FunctionLike: if tag == CALLABLE_TYPE: return CallableType.read(data) if tag == OVERLOADED: @@ -4356,7 +4357,7 @@ def read_function_like(data: Buffer, tag: Tag) -> FunctionLike: assert False, f"Invalid type tag for FunctionLike {tag}" -def read_type_var_likes(data: Buffer) -> list[TypeVarLikeType]: +def read_type_var_likes(data: ReadBuffer) -> list[TypeVarLikeType]: """Specialized version of read_type_list() for lists of type variables.""" assert read_tag(data) == LIST_GEN ret: list[TypeVarLikeType] = [] @@ -4373,40 +4374,40 @@ def read_type_var_likes(data: Buffer) -> list[TypeVarLikeType]: return ret -def read_type_opt(data: Buffer) -> Type | None: +def read_type_opt(data: ReadBuffer) -> Type | None: tag = read_tag(data) if tag == LITERAL_NONE: return None return read_type(data, tag) -def write_type_opt(data: Buffer, value: Type | None) -> None: +def write_type_opt(data: WriteBuffer, value: Type | None) -> None: if value is not None: value.write(data) else: write_tag(data, LITERAL_NONE) -def read_type_list(data: Buffer) -> list[Type]: +def read_type_list(data: ReadBuffer) -> list[Type]: assert read_tag(data) == LIST_GEN size = read_int_bare(data) return [read_type(data) for _ in range(size)] -def write_type_list(data: Buffer, value: Sequence[Type]) -> None: +def write_type_list(data: WriteBuffer, value: Sequence[Type]) -> None: write_tag(data, LIST_GEN) write_int_bare(data, len(value)) for item in value: item.write(data) -def read_type_map(data: Buffer) -> dict[str, Type]: +def read_type_map(data: ReadBuffer) -> dict[str, Type]: assert read_tag(data) == DICT_STR_GEN size = read_int_bare(data) return {read_str_bare(data): read_type(data) for _ in range(size)} -def write_type_map(data: Buffer, value: dict[str, Type]) -> None: +def write_type_map(data: WriteBuffer, value: dict[str, Type]) -> None: write_tag(data, DICT_STR_GEN) write_int_bare(data, len(value)) for key in sorted(value): diff --git a/mypy/typeshed/stubs/librt/librt/internal.pyi b/mypy/typeshed/stubs/librt/librt/internal.pyi index 78e7f9caa117..2969ccfbadda 100644 --- a/mypy/typeshed/stubs/librt/librt/internal.pyi +++ b/mypy/typeshed/stubs/librt/librt/internal.pyi @@ -1,27 +1,21 @@ from mypy_extensions import u8 -# TODO: Remove Buffer -- right now we have hacky support for BOTH the old and new APIs - -class Buffer: - def __init__(self, source: bytes = ...) -> None: ... - def getvalue(self) -> bytes: ... - class ReadBuffer: def __init__(self, source: bytes) -> None: ... class WriteBuffer: def getvalue(self) -> bytes: ... -def write_bool(data: WriteBuffer | Buffer, value: bool) -> None: ... -def read_bool(data: ReadBuffer | Buffer) -> bool: ... -def write_str(data: WriteBuffer | Buffer, value: str) -> None: ... -def read_str(data: ReadBuffer | Buffer) -> str: ... -def write_bytes(data: WriteBuffer | Buffer, value: bytes) -> None: ... -def read_bytes(data: ReadBuffer | Buffer) -> bytes: ... -def write_float(data: WriteBuffer | Buffer, value: float) -> None: ... -def read_float(data: ReadBuffer | Buffer) -> float: ... -def write_int(data: WriteBuffer | Buffer, value: int) -> None: ... -def read_int(data: ReadBuffer | Buffer) -> int: ... -def write_tag(data: WriteBuffer | Buffer, value: u8) -> None: ... -def read_tag(data: ReadBuffer | Buffer) -> u8: ... +def write_bool(data: WriteBuffer, value: bool) -> None: ... +def read_bool(data: ReadBuffer) -> bool: ... +def write_str(data: WriteBuffer, value: str) -> None: ... +def read_str(data: ReadBuffer) -> str: ... +def write_bytes(data: WriteBuffer, value: bytes) -> None: ... +def read_bytes(data: ReadBuffer) -> bytes: ... +def write_float(data: WriteBuffer, value: float) -> None: ... +def read_float(data: ReadBuffer) -> float: ... +def write_int(data: WriteBuffer, value: int) -> None: ... +def read_int(data: ReadBuffer) -> int: ... +def write_tag(data: WriteBuffer, value: u8) -> None: ... +def read_tag(data: ReadBuffer) -> u8: ... def cache_version() -> u8: ... diff --git a/pyproject.toml b/pyproject.toml index 0de739be9b55..42ff3a6ca019 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ requires = [ "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", # the following is from build-requirements.txt "types-psutil", "types-setuptools", @@ -54,7 +54,7 @@ dependencies = [ "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", ] dynamic = ["version"] diff --git a/test-requirements.txt b/test-requirements.txt index 126abd7149e6..b65b658844d2 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -22,7 +22,7 @@ identify==2.6.15 # via pre-commit iniconfig==2.1.0 # via pytest -librt==0.4.0 +librt==0.5.0 # via -r mypy-requirements.txt lxml==6.0.2 ; python_version < "3.15" # via -r test-requirements.in