From 8da1e8c31d03cd16bd231631ed82b7f35ca7b938 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 24 Nov 2022 22:54:09 -0800 Subject: [PATCH] marshal: specify allowed types (#9165) --- stdlib/marshal.pyi | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/stdlib/marshal.pyi b/stdlib/marshal.pyi index d68cdd143..d46d9c104 100644 --- a/stdlib/marshal.pyi +++ b/stdlib/marshal.pyi @@ -1,9 +1,33 @@ +import builtins +import types from _typeshed import ReadableBuffer, SupportsRead, SupportsWrite -from typing import Any +from typing import Any, Union +from typing_extensions import TypeAlias version: int -def dump(__value: Any, __file: SupportsWrite[bytes], __version: int = ...) -> None: ... +_Marshallable: TypeAlias = Union[ + # handled in w_object() in marshal.c + None, + type[StopIteration], + builtins.ellipsis, + bool, + # handled in w_complex_object() in marshal.c + int, + float, + complex, + bytes, + str, + tuple[_Marshallable, ...], + list[Any], + dict[Any, Any], + set[Any], + frozenset[_Marshallable], + types.CodeType, + ReadableBuffer, +] + +def dump(__value: _Marshallable, __file: SupportsWrite[bytes], __version: int = ...) -> None: ... def load(__file: SupportsRead[bytes]) -> Any: ... -def dumps(__value: Any, __version: int = ...) -> bytes: ... +def dumps(__value: _Marshallable, __version: int = ...) -> bytes: ... def loads(__bytes: ReadableBuffer) -> Any: ...