From edc3e3a89ca2128054689b71a82c1de910655e8c Mon Sep 17 00:00:00 2001 From: Semyon Moroz Date: Mon, 7 Jul 2025 14:18:06 +0000 Subject: [PATCH] [jsonschema] Add missing stubs (#14264) --- .../jsonschema/@tests/stubtest_allowlist.txt | 93 +++++++++++++++++++ stubs/jsonschema/METADATA.toml | 2 - stubs/jsonschema/jsonschema/__init__.pyi | 14 +++ stubs/jsonschema/jsonschema/_types.pyi | 18 ++-- stubs/jsonschema/jsonschema/_utils.pyi | 22 +++-- stubs/jsonschema/jsonschema/cli.pyi | 72 +++++++++----- stubs/jsonschema/jsonschema/validators.pyi | 4 +- 7 files changed, 183 insertions(+), 42 deletions(-) create mode 100644 stubs/jsonschema/@tests/stubtest_allowlist.txt diff --git a/stubs/jsonschema/@tests/stubtest_allowlist.txt b/stubs/jsonschema/@tests/stubtest_allowlist.txt new file mode 100644 index 000000000..b140a9c3b --- /dev/null +++ b/stubs/jsonschema/@tests/stubtest_allowlist.txt @@ -0,0 +1,93 @@ +# Testing modules are not included in type stubs: +jsonschema.tests.* +jsonschema.benchmarks.* + +# When importing this module, SystemExit: 2 is thrown: +jsonschema.__main__ + +# Autogenerated methods using @attr.* decorators: +jsonschema.Draft201909Validator.__attrs_attrs__ +jsonschema.Draft201909Validator.__attrs_own_setattr__ +jsonschema.Draft201909Validator.__attrs_post_init__ +jsonschema.Draft201909Validator.__match_args__ +jsonschema.Draft202012Validator.__attrs_attrs__ +jsonschema.Draft202012Validator.__attrs_own_setattr__ +jsonschema.Draft202012Validator.__attrs_post_init__ +jsonschema.Draft202012Validator.__match_args__ +jsonschema.Draft3Validator.__attrs_attrs__ +jsonschema.Draft3Validator.__attrs_own_setattr__ +jsonschema.Draft3Validator.__attrs_post_init__ +jsonschema.Draft3Validator.__match_args__ +jsonschema.Draft4Validator.__attrs_attrs__ +jsonschema.Draft4Validator.__attrs_own_setattr__ +jsonschema.Draft4Validator.__attrs_post_init__ +jsonschema.Draft4Validator.__match_args__ +jsonschema.Draft6Validator.__attrs_attrs__ +jsonschema.Draft6Validator.__attrs_own_setattr__ +jsonschema.Draft6Validator.__attrs_post_init__ +jsonschema.Draft6Validator.__match_args__ +jsonschema.Draft7Validator.__attrs_attrs__ +jsonschema.Draft7Validator.__attrs_own_setattr__ +jsonschema.Draft7Validator.__attrs_post_init__ +jsonschema.Draft7Validator.__match_args__ +jsonschema.RefResolutionError.__attrs_attrs__ +jsonschema.RefResolutionError.__match_args__ +jsonschema.TypeChecker.__attrs_attrs__ +jsonschema.TypeChecker.__match_args__ +jsonschema._types.TypeChecker.__attrs_attrs__ +jsonschema._types.TypeChecker.__match_args__ +jsonschema.cli._Outputter.__attrs_attrs__ +jsonschema.cli._Outputter.__attrs_own_setattr__ +jsonschema.cli._Outputter.__match_args__ +jsonschema.cli._PlainFormatter.__attrs_attrs__ +jsonschema.cli._PlainFormatter.__attrs_own_setattr__ +jsonschema.cli._PlainFormatter.__match_args__ +jsonschema.cli._PrettyFormatter.__attrs_attrs__ +jsonschema.cli._PrettyFormatter.__attrs_own_setattr__ +jsonschema.cli._PrettyFormatter.__match_args__ +jsonschema.exceptions.RefResolutionError.__attrs_attrs__ +jsonschema.exceptions.RefResolutionError.__match_args__ +jsonschema.validators.Draft201909Validator.__attrs_attrs__ +jsonschema.validators.Draft201909Validator.__attrs_own_setattr__ +jsonschema.validators.Draft201909Validator.__attrs_post_init__ +jsonschema.validators.Draft201909Validator.__match_args__ +jsonschema.validators.Draft202012Validator.__attrs_attrs__ +jsonschema.validators.Draft202012Validator.__attrs_own_setattr__ +jsonschema.validators.Draft202012Validator.__attrs_post_init__ +jsonschema.validators.Draft202012Validator.__match_args__ +jsonschema.validators.Draft3Validator.__attrs_attrs__ +jsonschema.validators.Draft3Validator.__attrs_own_setattr__ +jsonschema.validators.Draft3Validator.__attrs_post_init__ +jsonschema.validators.Draft3Validator.__match_args__ +jsonschema.validators.Draft4Validator.__attrs_attrs__ +jsonschema.validators.Draft4Validator.__attrs_own_setattr__ +jsonschema.validators.Draft4Validator.__attrs_post_init__ +jsonschema.validators.Draft4Validator.__match_args__ +jsonschema.validators.Draft6Validator.__attrs_attrs__ +jsonschema.validators.Draft6Validator.__attrs_own_setattr__ +jsonschema.validators.Draft6Validator.__attrs_post_init__ +jsonschema.validators.Draft6Validator.__match_args__ +jsonschema.validators.Draft7Validator.__attrs_attrs__ +jsonschema.validators.Draft7Validator.__attrs_own_setattr__ +jsonschema.validators.Draft7Validator.__attrs_post_init__ +jsonschema.validators.Draft7Validator.__match_args__ +# >= Python 3.13 +jsonschema.Draft201909Validator.__replace__ +jsonschema.Draft202012Validator.__replace__ +jsonschema.Draft3Validator.__replace__ +jsonschema.Draft4Validator.__replace__ +jsonschema.Draft6Validator.__replace__ +jsonschema.Draft7Validator.__replace__ +jsonschema.RefResolutionError.__replace__ +jsonschema.TypeChecker.__replace__ +jsonschema._types.TypeChecker.__replace__ +jsonschema.cli._Outputter.__replace__ +jsonschema.cli._PlainFormatter.__replace__ +jsonschema.cli._PrettyFormatter.__replace__ +jsonschema.exceptions.RefResolutionError.__replace__ +jsonschema.validators.Draft201909Validator.__replace__ +jsonschema.validators.Draft202012Validator.__replace__ +jsonschema.validators.Draft3Validator.__replace__ +jsonschema.validators.Draft4Validator.__replace__ +jsonschema.validators.Draft6Validator.__replace__ +jsonschema.validators.Draft7Validator.__replace__ diff --git a/stubs/jsonschema/METADATA.toml b/stubs/jsonschema/METADATA.toml index 1b90ab4f4..b84b663ac 100644 --- a/stubs/jsonschema/METADATA.toml +++ b/stubs/jsonschema/METADATA.toml @@ -1,8 +1,6 @@ version = "4.24.*" upstream_repository = "https://github.com/python-jsonschema/jsonschema" requires = ["referencing"] -partial_stub = true [tool.stubtest] -ignore_missing_stub = true extras = ["format"] diff --git a/stubs/jsonschema/jsonschema/__init__.pyi b/stubs/jsonschema/jsonschema/__init__.pyi index dcb925bc6..61cf92fd5 100644 --- a/stubs/jsonschema/jsonschema/__init__.pyi +++ b/stubs/jsonschema/jsonschema/__init__.pyi @@ -26,3 +26,17 @@ from jsonschema.validators import ( RefResolver as RefResolver, validate as validate, ) + +__all__ = [ + "Draft3Validator", + "Draft4Validator", + "Draft6Validator", + "Draft7Validator", + "Draft201909Validator", + "Draft202012Validator", + "FormatChecker", + "SchemaError", + "TypeChecker", + "ValidationError", + "validate", +] diff --git a/stubs/jsonschema/jsonschema/_types.pyi b/stubs/jsonschema/jsonschema/_types.pyi index 4c248ab95..c6d963770 100644 --- a/stubs/jsonschema/jsonschema/_types.pyi +++ b/stubs/jsonschema/jsonschema/_types.pyi @@ -1,13 +1,15 @@ +from _typeshed import Unused from collections.abc import Callable, Iterable, Mapping +from typing import Literal -def is_array(checker, instance) -> bool: ... -def is_bool(checker, instance) -> bool: ... -def is_integer(checker, instance) -> bool: ... -def is_null(checker, instance) -> bool: ... -def is_number(checker, instance) -> bool: ... -def is_object(checker, instance) -> bool: ... -def is_string(checker, instance) -> bool: ... -def is_any(checker, instance) -> bool: ... +def is_array(checker: Unused, instance: object) -> bool: ... +def is_bool(checker: Unused, instance: object) -> bool: ... +def is_integer(checker: Unused, instance: object) -> bool: ... +def is_null(checker: Unused, instance: object) -> bool: ... +def is_number(checker: Unused, instance: object) -> bool: ... +def is_object(checker: Unused, instance: object) -> bool: ... +def is_string(checker: Unused, instance: object) -> bool: ... +def is_any(checker: Unused, instance: Unused) -> Literal[True]: ... class TypeChecker: def __init__(self, type_checkers: Mapping[str, Callable[[object], bool]] = ...) -> None: ... diff --git a/stubs/jsonschema/jsonschema/_utils.pyi b/stubs/jsonschema/jsonschema/_utils.pyi index 90c26404e..0ddd9c689 100644 --- a/stubs/jsonschema/jsonschema/_utils.pyi +++ b/stubs/jsonschema/jsonschema/_utils.pyi @@ -1,5 +1,8 @@ -from _typeshed import Incomplete, SupportsKeysAndGetItem, SupportsNext -from collections.abc import Generator, Iterable, Iterator, Mapping, MutableMapping, Sized +from _typeshed import Incomplete, SupportsKeysAndGetItem, SupportsNext, SupportsRichComparison +from collections.abc import Generator, Iterable, Iterator, Mapping, MutableMapping +from typing import Literal, TypeVar, overload + +_T = TypeVar("_T") class URIDict(MutableMapping[str, str]): def normalize(self, uri: str) -> str: ... @@ -13,15 +16,16 @@ class URIDict(MutableMapping[str, str]): class Unset: ... -def format_as_index(container: str, indices) -> str: ... -def find_additional_properties( - instance: Iterable[Incomplete], schema: Mapping[Incomplete, Incomplete] -) -> Generator[Incomplete, None, None]: ... -def extras_msg(extras: Iterable[Incomplete] | Sized) -> str: ... -def ensure_list(thing) -> list[Incomplete]: ... +def format_as_index(container: str, indices: Iterable[Incomplete] | None) -> str: ... +def find_additional_properties(instance: Iterable[str], schema: Mapping[str, Iterable[str]]) -> Generator[str]: ... +def extras_msg(extras: Iterable[object]) -> tuple[str, Literal["was", "were"]]: ... # elements are passed to the repr() function +@overload +def ensure_list(thing: str) -> list[str]: ... +@overload +def ensure_list(thing: _T) -> _T: ... def equal(one, two) -> bool: ... def unbool(element, true=..., false=...): ... -def uniq(container) -> bool: ... +def uniq(container: Iterable[SupportsRichComparison]) -> bool: ... def find_evaluated_item_indexes_by_schema(validator, instance, schema) -> list[Incomplete]: ... def find_evaluated_property_keys_by_schema(validator, instance, schema) -> list[Incomplete]: ... def is_valid(errs_it: SupportsNext[object]) -> bool: ... diff --git a/stubs/jsonschema/jsonschema/cli.pyi b/stubs/jsonschema/jsonschema/cli.pyi index 01ef4137d..0358db4e5 100644 --- a/stubs/jsonschema/jsonschema/cli.pyi +++ b/stubs/jsonschema/jsonschema/cli.pyi @@ -1,32 +1,62 @@ -from _typeshed import Incomplete +import argparse +from _typeshed import ExcInfo, FileDescriptorOrPath, Incomplete, OptExcInfo, SupportsWrite +from collections.abc import Mapping, Sequence +from typing import Any +from typing_extensions import Self, deprecated +@deprecated("The jsonschema CLI is deprecated and will be removed in a future version. Please use check-jsonschema instead.") class _CannotLoadFile(Exception): ... +@deprecated("The jsonschema CLI is deprecated and will be removed in a future version. Please use check-jsonschema instead.") class _Outputter: - def __init__(self, formatter, stdout, stderr): ... + _formatter: _PlainFormatter | _PrettyFormatter + _stdout: SupportsWrite[str] + _stderr: SupportsWrite[str] + + def __init__( + self, formatter: _PlainFormatter | _PrettyFormatter, stdout: SupportsWrite[str], stderr: SupportsWrite[str] + ) -> None: ... @classmethod - def from_arguments(cls, arguments, stdout, stderr): ... - def load(self, path): ... - def filenotfound_error(self, **kwargs) -> None: ... - def parsing_error(self, **kwargs) -> None: ... - def validation_error(self, **kwargs) -> None: ... - def validation_success(self, **kwargs) -> None: ... + def from_arguments( + cls, arguments: Mapping[str, Incomplete], stdout: SupportsWrite[str], stderr: SupportsWrite[str] + ) -> Self: ... + def load(self, path: FileDescriptorOrPath) -> Any: ... # result of json.load() + def filenotfound_error(self, *, path: FileDescriptorOrPath, exc_info: OptExcInfo) -> None: ... + def parsing_error(self, *, path: FileDescriptorOrPath, exc_info: ExcInfo) -> None: ... + def validation_error(self, *, instance_path: FileDescriptorOrPath, error: BaseException) -> None: ... + def validation_success(self, *, instance_path: FileDescriptorOrPath) -> None: ... +@deprecated("The jsonschema CLI is deprecated and will be removed in a future version. Please use check-jsonschema instead.") class _PrettyFormatter: - def filenotfound_error(self, path, exc_info): ... - def parsing_error(self, path, exc_info): ... - def validation_error(self, instance_path, error): ... - def validation_success(self, instance_path): ... + _ERROR_MSG: str + _SUCCESS_MSG: str + def __init__(self) -> None: ... + def filenotfound_error(self, path: FileDescriptorOrPath, exc_info: OptExcInfo) -> str: ... + def parsing_error(self, path: FileDescriptorOrPath, exc_info: ExcInfo) -> str: ... + def validation_error(self, instance_path: FileDescriptorOrPath, error: BaseException) -> str: ... + def validation_success(self, instance_path: FileDescriptorOrPath) -> str: ... + +@deprecated("The jsonschema CLI is deprecated and will be removed in a future version. Please use check-jsonschema instead.") class _PlainFormatter: - def __init__(self, error_format): ... - def filenotfound_error(self, path, exc_info): ... - def parsing_error(self, path, exc_info): ... - def validation_error(self, instance_path, error): ... - def validation_success(self, instance_path): ... + _error_format: str -parser: Incomplete + def __init__(self, error_format: str) -> None: ... + def filenotfound_error(self, path: FileDescriptorOrPath, exc_info: OptExcInfo) -> str: ... + def parsing_error(self, path: FileDescriptorOrPath, exc_info: ExcInfo) -> str: ... + def validation_error(self, instance_path: FileDescriptorOrPath, error: BaseException) -> str: ... + def validation_success(self, instance_path: FileDescriptorOrPath) -> str: ... -def parse_args(args): ... -def main(args=...) -> None: ... -def run(arguments, stdout=..., stderr=..., stdin=...): ... +parser: argparse.ArgumentParser + +@deprecated("The jsonschema CLI is deprecated and will be removed in a future version. Please use check-jsonschema instead.") +def parse_args(args: Sequence[str] | None) -> dict[str, Any]: ... # result of vars(argparse.Namespace()) +@deprecated("The jsonschema CLI is deprecated and will be removed in a future version. Please use check-jsonschema instead.") +def main(args: Sequence[str] = ...) -> None: ... +@deprecated("The jsonschema CLI is deprecated and will be removed in a future version. Please use check-jsonschema instead.") +def run( + arguments: Mapping[str, Incomplete], + stdout: SupportsWrite[str] = ..., + stderr: SupportsWrite[str] = ..., + stdin: SupportsWrite[str] = ..., +) -> int: ... diff --git a/stubs/jsonschema/jsonschema/validators.pyi b/stubs/jsonschema/jsonschema/validators.pyi index cd71f1a43..41b56e2d0 100644 --- a/stubs/jsonschema/jsonschema/validators.pyi +++ b/stubs/jsonschema/jsonschema/validators.pyi @@ -54,7 +54,7 @@ class _Validator: def iter_errors(self, instance, _schema: Schema | None) -> Generator[Incomplete]: ... def descend( self, instance, schema: Schema, path: Incomplete | None = ..., schema_path: Incomplete | None = ..., resolver=None - ) -> Generator[Incomplete, None, None]: ... + ) -> Generator[Incomplete]: ... def validate(self, *args, **kwargs) -> None: ... def is_type(self, instance, type) -> bool: ... @overload @@ -63,7 +63,7 @@ class _Validator: @deprecated("Passing a schema to Validator.is_valid is deprecated and will be removed in a future release.") def is_valid(self, instance, _schema: Schema | None) -> bool: ... -def validates(version: str) -> Callable[..., Incomplete]: ... +def validates(version: str) -> Callable[[_Validator], _Validator]: ... def create( meta_schema: Schema, validators: Mapping[str, _ValidatorCallback] | tuple[()] = (),