From 3fa6374c9aeedcda7020f16953b29753ddc53a3c Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Sun, 18 Feb 2024 01:50:46 +0300 Subject: [PATCH] Mark simplejson as completed (#9211) Co-authored-by: Jelle Zijlstra Co-authored-by: Alex Waygood --- .../simplejson/@tests/stubtest_allowlist.txt | 25 ++- .../@tests/test_cases/check_simplejson.py | 16 ++ stubs/simplejson/METADATA.toml | 4 - stubs/simplejson/simplejson/__init__.pyi | 164 +++++++++++++++++- stubs/simplejson/simplejson/decoder.pyi | 31 +++- stubs/simplejson/simplejson/encoder.pyi | 56 +++++- stubs/simplejson/simplejson/scanner.pyi | 6 + 7 files changed, 273 insertions(+), 29 deletions(-) create mode 100644 stubs/simplejson/@tests/test_cases/check_simplejson.py diff --git a/stubs/simplejson/@tests/stubtest_allowlist.txt b/stubs/simplejson/@tests/stubtest_allowlist.txt index d3000e8eb..ab8e30af4 100644 --- a/stubs/simplejson/@tests/stubtest_allowlist.txt +++ b/stubs/simplejson/@tests/stubtest_allowlist.txt @@ -1,14 +1,13 @@ -simplejson.JSONDecodeError.__init__ -simplejson.JSONDecoder.__init__ -simplejson.JSONDecoder.decode -simplejson.JSONDecoder.raw_decode -simplejson.JSONEncoder.__init__ -simplejson.decoder.JSONDecoder.__init__ -simplejson.decoder.JSONDecoder.decode -simplejson.decoder.JSONDecoder.raw_decode -simplejson.dump -simplejson.dumps -simplejson.encoder.JSONEncoder.__init__ -simplejson.load -simplejson.loads +# Speedups (C vs Python inconsistency): +simplejson.scanner.make_scanner simplejson.scanner.JSONDecodeError.__init__ +simplejson.JSONDecodeError.__init__ + +# Tests are not included: +simplejson.tests.* + +# Internal and compat tools: +simplejson.compat +simplejson.ordered_dict +simplejson.tool +simplejson.OrderedDict diff --git a/stubs/simplejson/@tests/test_cases/check_simplejson.py b/stubs/simplejson/@tests/test_cases/check_simplejson.py new file mode 100644 index 000000000..c62fd73da --- /dev/null +++ b/stubs/simplejson/@tests/test_cases/check_simplejson.py @@ -0,0 +1,16 @@ +from __future__ import annotations + +from typing_extensions import assert_type + +from simplejson import JSONEncoder, dumps + + +class CustomEncoder(JSONEncoder): # eventhough it does not have `extra_kw` arg. + ... + + +# We are only testing `dumps` here, because they are all the same: +dumps([], extra_kw=True) # type: ignore + +# Ok: +assert_type(dumps([], cls=CustomEncoder, extra_kw=True), str) diff --git a/stubs/simplejson/METADATA.toml b/stubs/simplejson/METADATA.toml index 23fe62d9e..e41e9f31a 100644 --- a/stubs/simplejson/METADATA.toml +++ b/stubs/simplejson/METADATA.toml @@ -1,6 +1,2 @@ version = "3.19.*" upstream_repository = "https://github.com/simplejson/simplejson" -partial_stub = true - -[tool.stubtest] -ignore_missing_stub = true diff --git a/stubs/simplejson/simplejson/__init__.pyi b/stubs/simplejson/simplejson/__init__.pyi index 5cfcc6d4c..dcba31704 100644 --- a/stubs/simplejson/simplejson/__init__.pyi +++ b/stubs/simplejson/simplejson/__init__.pyi @@ -1,4 +1,6 @@ -from typing import IO, Any +from _typeshed import SupportsRichComparison +from collections.abc import Callable +from typing import IO, Any, TypeVar, overload from typing_extensions import TypeAlias from simplejson.decoder import JSONDecoder as JSONDecoder @@ -7,8 +9,160 @@ from simplejson.raw_json import RawJSON as RawJSON from simplejson.scanner import JSONDecodeError as JSONDecodeError _LoadsString: TypeAlias = str | bytes | bytearray +_T = TypeVar("_T") -def dumps(obj: Any, *args: Any, **kwds: Any) -> str: ... -def dump(obj: Any, fp: IO[str], *args: Any, **kwds: Any) -> None: ... -def loads(s: _LoadsString, **kwds: Any) -> Any: ... -def load(fp: IO[str], **kwds: Any) -> Any: ... +@overload +def dumps( + obj: Any, + skipkeys: bool = ..., + ensure_ascii: bool = ..., + check_circular: bool = ..., + allow_nan: bool = ..., + *, + cls: type[JSONEncoder], + indent: str | int | None = ..., + separators: tuple[str, str] | None = ..., + encoding: str = ..., + default: Callable[[Any], Any] | None = ..., + use_decimal: bool = ..., + namedtuple_as_object: bool = ..., + tuple_as_array: bool = ..., + bigint_as_string: bool = ..., + sort_keys: bool = ..., + item_sort_key: Callable[[Any], SupportsRichComparison] | None = ..., + for_json: bool = ..., + ignore_nan: bool = ..., + int_as_string_bitcount: int | None = ..., + iterable_as_array: bool = ..., + **kw: Any, +) -> str: ... +@overload +def dumps( + obj: Any, + skipkeys: bool = ..., + ensure_ascii: bool = ..., + check_circular: bool = ..., + allow_nan: bool = ..., + cls: type[JSONEncoder] | None = ..., + indent: str | int | None = ..., + separators: tuple[str, str] | None = ..., + encoding: str = ..., + default: Callable[[Any], Any] | None = ..., + use_decimal: bool = ..., + namedtuple_as_object: bool = ..., + tuple_as_array: bool = ..., + bigint_as_string: bool = ..., + sort_keys: bool = ..., + item_sort_key: Callable[[Any], SupportsRichComparison] | None = ..., + for_json: bool = ..., + ignore_nan: bool = ..., + int_as_string_bitcount: int | None = ..., + iterable_as_array: bool = ..., +) -> str: ... +@overload +def dump( + obj: Any, + fp: IO[str], + skipkeys: bool = ..., + ensure_ascii: bool = ..., + check_circular: bool = ..., + allow_nan: bool = ..., + *, + cls: type[JSONEncoder], + indent: str | int | None = ..., + separators: tuple[str, str] | None = ..., + encoding: str = ..., + default: Callable[[Any], Any] | None = ..., + use_decimal: bool = ..., + namedtuple_as_object: bool = ..., + tuple_as_array: bool = ..., + bigint_as_string: bool = ..., + sort_keys: bool = ..., + item_sort_key: Callable[[Any], SupportsRichComparison] | None = ..., + for_json: bool = ..., + ignore_nan: bool = ..., + int_as_string_bitcount: int | None = ..., + iterable_as_array: bool = ..., + **kw: Any, +) -> None: ... +@overload +def dump( + obj: Any, + fp: IO[str], + skipkeys: bool = ..., + ensure_ascii: bool = ..., + check_circular: bool = ..., + allow_nan: bool = ..., + cls: type[JSONEncoder] | None = ..., + indent: str | int | None = ..., + separators: tuple[str, str] | None = ..., + encoding: str = ..., + default: Callable[[Any], Any] | None = ..., + use_decimal: bool = ..., + namedtuple_as_object: bool = ..., + tuple_as_array: bool = ..., + bigint_as_string: bool = ..., + sort_keys: bool = ..., + item_sort_key: Callable[[Any], SupportsRichComparison] | None = ..., + for_json: bool = ..., + ignore_nan: bool = ..., + int_as_string_bitcount: int | None = ..., + iterable_as_array: bool = ..., +) -> None: ... +@overload +def loads( + s: _LoadsString, + encoding: str | None = ..., + *, + cls: type[JSONDecoder], + object_hook: Callable[[dict[Any, Any]], Any] | None = ..., + parse_float: Callable[[str], Any] | None = ..., + parse_int: Callable[[str], Any] | None = ..., + parse_constant: Callable[[str], Any] | None = ..., + object_pairs_hook: Callable[[list[tuple[Any, Any]]], Any] | None = ..., + use_decimal: bool = ..., + allow_nan: bool = ..., + **kw: Any, +) -> Any: ... +@overload +def loads( + s: _LoadsString, + encoding: str | None = ..., + cls: type[JSONDecoder] | None = ..., + object_hook: Callable[[dict[Any, Any]], Any] | None = ..., + parse_float: Callable[[str], Any] | None = ..., + parse_int: Callable[[str], Any] | None = ..., + parse_constant: Callable[[str], Any] | None = ..., + object_pairs_hook: Callable[[list[tuple[Any, Any]]], Any] | None = ..., + use_decimal: bool = ..., + allow_nan: bool = ..., +) -> Any: ... +@overload +def load( + fp: IO[str], + encoding: str | None = ..., + *, + cls: type[JSONDecoder], + object_hook: Callable[[dict[Any, Any]], Any] | None = ..., + parse_float: Callable[[str], Any] | None = ..., + parse_int: Callable[[str], Any] | None = ..., + parse_constant: Callable[[str], Any] | None = ..., + object_pairs_hook: Callable[[list[tuple[Any, Any]]], Any] | None = ..., + use_decimal: bool = ..., + allow_nan: bool = ..., + **kw: Any, +) -> Any: ... +@overload +def load( + fp: IO[str], + encoding: str | None = ..., + cls: type[JSONDecoder] | None = ..., + object_hook: Callable[[dict[Any, Any]], Any] | None = ..., + parse_float: Callable[[str], Any] | None = ..., + parse_int: Callable[[str], Any] | None = ..., + parse_constant: Callable[[str], Any] | None = ..., + object_pairs_hook: Callable[[list[tuple[Any, Any]]], Any] | None = ..., + use_decimal: bool = ..., + allow_nan: bool = ..., +) -> Any: ... +def simple_first(kv: tuple[_T, object]) -> tuple[bool, _T]: ... diff --git a/stubs/simplejson/simplejson/decoder.pyi b/stubs/simplejson/simplejson/decoder.pyi index d2b1ac14a..ff1ef5646 100644 --- a/stubs/simplejson/simplejson/decoder.pyi +++ b/stubs/simplejson/simplejson/decoder.pyi @@ -1,7 +1,30 @@ +from collections.abc import Callable from re import Match -from typing import Any +from typing import Any, Literal class JSONDecoder: - def __init__(self, **kwargs: Any) -> None: ... - def decode(self, s: str, _w: Match[str], _PY3: bool) -> Any: ... - def raw_decode(self, s: str, idx: int, _w: Match[str], _PY3: bool) -> tuple[Any, int]: ... + encoding: str + object_hook: Callable[[dict[Any, Any]], Any] | None + parse_float: Callable[[str], Any] | None + parse_int: Callable[[str], Any] | None + parse_constant: Callable[[str], Any] | None + strict: bool + object_pairs_hook: Callable[[list[tuple[Any, Any]]], Any] | None + memo: dict[Any, Any] + scan_once: Callable[[str, int], tuple[bool, int]] + + def __init__( + self, + encoding: str | None = ..., + object_hook: Callable[[dict[Any, Any]], Any] | None = ..., + parse_float: Callable[[str], Any] | None = ..., + parse_int: Callable[[str], Any] | None = ..., + parse_constant: Callable[[str], Any] | None = ..., + strict: bool = ..., + object_pairs_hook: Callable[[list[tuple[Any, Any]]], Any] | None = ..., + allow_nan: bool = ..., + ) -> None: ... + def decode(self, s: str, _w: Callable[[str, int], Match[str]] = ..., _PY3: Literal[True] = ...) -> Any: ... + def raw_decode( + self, s: str, idx: int = ..., _w: Callable[[str, int], Match[str]] = ..., _PY3: Literal[True] = ... + ) -> tuple[Any, int]: ... diff --git a/stubs/simplejson/simplejson/encoder.pyi b/stubs/simplejson/simplejson/encoder.pyi index e69a066df..b19ec33c3 100644 --- a/stubs/simplejson/simplejson/encoder.pyi +++ b/stubs/simplejson/simplejson/encoder.pyi @@ -1,10 +1,60 @@ -from collections.abc import Iterator -from typing import Any, NoReturn +import re +from _typeshed import SupportsRichComparison +from collections.abc import Callable, Iterator +from typing import Any, Literal, NoReturn + +ESCAPE: re.Pattern[str] +ESCAPE_ASCII: re.Pattern[str] +HAS_UTF8: re.Pattern[str] +ESCAPE_DCT: dict[str, str] +FLOAT_REPR: Callable[[object], str] class JSONEncoder: - def __init__(self, *args: Any, **kwargs: Any) -> None: ... + item_separator: str + key_separator: str + skipkeys: bool + ensure_ascii: bool + check_circular: bool + allow_nan: bool + sort_keys: bool + indent: str + encoding: str + use_decimal: bool + namedtuple_as_object: bool + tuple_as_array: bool + bigint_as_string: bool + item_sort_key: Callable[[Any], SupportsRichComparison] | None + for_json: bool + ignore_nan: bool + int_as_string_bitcount: int | None + iterable_as_array: bool + + def __init__( + self, + skipkeys: bool = ..., + ensure_ascii: bool = ..., + check_circular: bool = ..., + allow_nan: bool = ..., + sort_keys: bool = ..., + indent: str | int | None = ..., + separators: tuple[str, str] | None = ..., + encoding: str = ..., + default: Callable[[Any], Any] | None = ..., + use_decimal: bool = ..., + namedtuple_as_object: bool = ..., + tuple_as_array: bool = ..., + bigint_as_string: bool = ..., + item_sort_key: Callable[[Any], SupportsRichComparison] | None = ..., + for_json: bool = ..., + ignore_nan: bool = ..., + int_as_string_bitcount: int | None = ..., + iterable_as_array: bool = ..., + ) -> None: ... def encode(self, o: Any) -> str: ... def default(self, o: Any) -> NoReturn: ... def iterencode(self, o: Any) -> Iterator[str]: ... class JSONEncoderForHTML(JSONEncoder): ... + +def encode_basestring(s: str | bytes, _PY3: Literal[True] = ..., _q: str = ...) -> str: ... +def py_encode_basestring_ascii(s: str | bytes, _PY3: Literal[True] = ...) -> str: ... diff --git a/stubs/simplejson/simplejson/scanner.pyi b/stubs/simplejson/simplejson/scanner.pyi index 66b8d27f8..ea5ba8c47 100644 --- a/stubs/simplejson/simplejson/scanner.pyi +++ b/stubs/simplejson/simplejson/scanner.pyi @@ -1,3 +1,7 @@ +from collections.abc import Callable + +from simplejson.decoder import JSONDecoder + class JSONDecodeError(ValueError): msg: str doc: str @@ -7,3 +11,5 @@ class JSONDecodeError(ValueError): colno: int endlineno: int | None endcolno: int | None + +def make_scanner(context: JSONDecoder) -> Callable[[str, int], tuple[bool, int]]: ...