Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +2 -0
- infer_4_33_0/share/terminfo/2/2621 +0 -0
- infer_4_33_0/share/terminfo/m/m2-nam +0 -0
- infer_4_33_0/share/terminfo/m/mime314 +0 -0
- infer_4_33_0/share/terminfo/m/minix +0 -0
- infer_4_33_0/share/terminfo/m/modgraph +0 -0
- infer_4_33_0/share/terminfo/m/mrxvt +0 -0
- infer_4_33_0/share/terminfo/m/mskermit227 +0 -0
- infer_4_37_2/lib/python3.10/site-packages/huggingface_hub/__pycache__/hf_api.cpython-310.pyc +3 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/__init__.py +120 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/__main__.py +6 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/__init__.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/__main__.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/_format.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/_keywords.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/_legacy_keywords.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/_types.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/_typing.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/_utils.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/cli.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/exceptions.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/protocols.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/validators.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/_format.py +519 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/_keywords.py +449 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/_legacy_keywords.py +449 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/_types.py +200 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/_typing.py +28 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/_utils.py +351 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/__init__.py +5 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/__pycache__/contains.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/__pycache__/nested_schemas.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/__pycache__/subcomponents.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/__pycache__/unused_registry.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/__pycache__/validator_creation.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/contains.py +28 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/issue232.py +25 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/issue232/issue.json +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/json_schema_test_suite.py +12 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/subcomponents.py +42 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/useless_keywords.py +32 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/validator_creation.py +14 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/cli.py +296 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/exceptions.py +487 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/protocols.py +236 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/tests/__init__.py +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/tests/__pycache__/_suite.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/tests/__pycache__/fuzz_validate.cpython-310.pyc +0 -0
- infer_4_37_2/lib/python3.10/site-packages/jsonschema/tests/__pycache__/test_cli.cpython-310.pyc +0 -0
.gitattributes
CHANGED
|
@@ -1511,3 +1511,5 @@ infer_4_37_2/lib/python3.10/site-packages/pyarrow/_dataset_parquet.cpython-310-x
|
|
| 1511 |
infer_4_37_2/lib/python3.10/site-packages/pyarrow/_json.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1512 |
infer_4_33_0/lib/ossl-modules/legacy.so filter=lfs diff=lfs merge=lfs -text
|
| 1513 |
emu3/lib/python3.10/site-packages/numpy.libs/libscipy_openblas64_-ff651d7f.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 1511 |
infer_4_37_2/lib/python3.10/site-packages/pyarrow/_json.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1512 |
infer_4_33_0/lib/ossl-modules/legacy.so filter=lfs diff=lfs merge=lfs -text
|
| 1513 |
emu3/lib/python3.10/site-packages/numpy.libs/libscipy_openblas64_-ff651d7f.so filter=lfs diff=lfs merge=lfs -text
|
| 1514 |
+
infer_4_37_2/lib/python3.10/site-packages/xxhash/_xxhash.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1515 |
+
infer_4_37_2/lib/python3.10/site-packages/huggingface_hub/__pycache__/hf_api.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
infer_4_33_0/share/terminfo/2/2621
ADDED
|
Binary file (622 Bytes). View file
|
|
|
infer_4_33_0/share/terminfo/m/m2-nam
ADDED
|
Binary file (1.2 kB). View file
|
|
|
infer_4_33_0/share/terminfo/m/mime314
ADDED
|
Binary file (360 Bytes). View file
|
|
|
infer_4_33_0/share/terminfo/m/minix
ADDED
|
Binary file (1.46 kB). View file
|
|
|
infer_4_33_0/share/terminfo/m/modgraph
ADDED
|
Binary file (1.14 kB). View file
|
|
|
infer_4_33_0/share/terminfo/m/mrxvt
ADDED
|
Binary file (3.04 kB). View file
|
|
|
infer_4_33_0/share/terminfo/m/mskermit227
ADDED
|
Binary file (531 Bytes). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/huggingface_hub/__pycache__/hf_api.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:358caaaa0aabd39f297e569df7e6f5e21792f1665b6457111b3505790b3fe5f5
|
| 3 |
+
size 358713
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/__init__.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
An implementation of JSON Schema for Python.
|
| 3 |
+
|
| 4 |
+
The main functionality is provided by the validator classes for each of the
|
| 5 |
+
supported JSON Schema versions.
|
| 6 |
+
|
| 7 |
+
Most commonly, `jsonschema.validators.validate` is the quickest way to simply
|
| 8 |
+
validate a given instance under a schema, and will create a validator
|
| 9 |
+
for you.
|
| 10 |
+
"""
|
| 11 |
+
import warnings
|
| 12 |
+
|
| 13 |
+
from jsonschema._format import FormatChecker
|
| 14 |
+
from jsonschema._types import TypeChecker
|
| 15 |
+
from jsonschema.exceptions import SchemaError, ValidationError
|
| 16 |
+
from jsonschema.validators import (
|
| 17 |
+
Draft3Validator,
|
| 18 |
+
Draft4Validator,
|
| 19 |
+
Draft6Validator,
|
| 20 |
+
Draft7Validator,
|
| 21 |
+
Draft201909Validator,
|
| 22 |
+
Draft202012Validator,
|
| 23 |
+
validate,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def __getattr__(name):
|
| 28 |
+
if name == "__version__":
|
| 29 |
+
warnings.warn(
|
| 30 |
+
"Accessing jsonschema.__version__ is deprecated and will be "
|
| 31 |
+
"removed in a future release. Use importlib.metadata directly "
|
| 32 |
+
"to query for jsonschema's version.",
|
| 33 |
+
DeprecationWarning,
|
| 34 |
+
stacklevel=2,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
from importlib import metadata
|
| 38 |
+
return metadata.version("jsonschema")
|
| 39 |
+
elif name == "RefResolver":
|
| 40 |
+
from jsonschema.validators import _RefResolver
|
| 41 |
+
warnings.warn(
|
| 42 |
+
_RefResolver._DEPRECATION_MESSAGE,
|
| 43 |
+
DeprecationWarning,
|
| 44 |
+
stacklevel=2,
|
| 45 |
+
)
|
| 46 |
+
return _RefResolver
|
| 47 |
+
elif name == "ErrorTree":
|
| 48 |
+
warnings.warn(
|
| 49 |
+
"Importing ErrorTree directly from the jsonschema package "
|
| 50 |
+
"is deprecated and will become an ImportError. Import it from "
|
| 51 |
+
"jsonschema.exceptions instead.",
|
| 52 |
+
DeprecationWarning,
|
| 53 |
+
stacklevel=2,
|
| 54 |
+
)
|
| 55 |
+
from jsonschema.exceptions import ErrorTree
|
| 56 |
+
return ErrorTree
|
| 57 |
+
elif name == "FormatError":
|
| 58 |
+
warnings.warn(
|
| 59 |
+
"Importing FormatError directly from the jsonschema package "
|
| 60 |
+
"is deprecated and will become an ImportError. Import it from "
|
| 61 |
+
"jsonschema.exceptions instead.",
|
| 62 |
+
DeprecationWarning,
|
| 63 |
+
stacklevel=2,
|
| 64 |
+
)
|
| 65 |
+
from jsonschema.exceptions import FormatError
|
| 66 |
+
return FormatError
|
| 67 |
+
elif name == "Validator":
|
| 68 |
+
warnings.warn(
|
| 69 |
+
"Importing Validator directly from the jsonschema package "
|
| 70 |
+
"is deprecated and will become an ImportError. Import it from "
|
| 71 |
+
"jsonschema.protocols instead.",
|
| 72 |
+
DeprecationWarning,
|
| 73 |
+
stacklevel=2,
|
| 74 |
+
)
|
| 75 |
+
from jsonschema.protocols import Validator
|
| 76 |
+
return Validator
|
| 77 |
+
elif name == "RefResolutionError":
|
| 78 |
+
from jsonschema.exceptions import _RefResolutionError
|
| 79 |
+
warnings.warn(
|
| 80 |
+
_RefResolutionError._DEPRECATION_MESSAGE,
|
| 81 |
+
DeprecationWarning,
|
| 82 |
+
stacklevel=2,
|
| 83 |
+
)
|
| 84 |
+
return _RefResolutionError
|
| 85 |
+
|
| 86 |
+
format_checkers = {
|
| 87 |
+
"draft3_format_checker": Draft3Validator,
|
| 88 |
+
"draft4_format_checker": Draft4Validator,
|
| 89 |
+
"draft6_format_checker": Draft6Validator,
|
| 90 |
+
"draft7_format_checker": Draft7Validator,
|
| 91 |
+
"draft201909_format_checker": Draft201909Validator,
|
| 92 |
+
"draft202012_format_checker": Draft202012Validator,
|
| 93 |
+
}
|
| 94 |
+
ValidatorForFormat = format_checkers.get(name)
|
| 95 |
+
if ValidatorForFormat is not None:
|
| 96 |
+
warnings.warn(
|
| 97 |
+
f"Accessing jsonschema.{name} is deprecated and will be "
|
| 98 |
+
"removed in a future release. Instead, use the FORMAT_CHECKER "
|
| 99 |
+
"attribute on the corresponding Validator.",
|
| 100 |
+
DeprecationWarning,
|
| 101 |
+
stacklevel=2,
|
| 102 |
+
)
|
| 103 |
+
return ValidatorForFormat.FORMAT_CHECKER
|
| 104 |
+
|
| 105 |
+
raise AttributeError(f"module {__name__} has no attribute {name}")
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
__all__ = [
|
| 109 |
+
"Draft201909Validator",
|
| 110 |
+
"Draft202012Validator",
|
| 111 |
+
"Draft3Validator",
|
| 112 |
+
"Draft4Validator",
|
| 113 |
+
"Draft6Validator",
|
| 114 |
+
"Draft7Validator",
|
| 115 |
+
"FormatChecker",
|
| 116 |
+
"SchemaError",
|
| 117 |
+
"TypeChecker",
|
| 118 |
+
"ValidationError",
|
| 119 |
+
"validate",
|
| 120 |
+
]
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/__main__.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
The jsonschema CLI is now deprecated in favor of check-jsonschema.
|
| 3 |
+
"""
|
| 4 |
+
from jsonschema.cli import main
|
| 5 |
+
|
| 6 |
+
main()
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (2.98 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/__main__.cpython-310.pyc
ADDED
|
Binary file (304 Bytes). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/_format.cpython-310.pyc
ADDED
|
Binary file (12.1 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/_keywords.cpython-310.pyc
ADDED
|
Binary file (12 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/_legacy_keywords.cpython-310.pyc
ADDED
|
Binary file (9.43 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/_types.cpython-310.pyc
ADDED
|
Binary file (5.7 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/_typing.cpython-310.pyc
ADDED
|
Binary file (1.04 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/_utils.cpython-310.pyc
ADDED
|
Binary file (8.76 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/cli.cpython-310.pyc
ADDED
|
Binary file (8.58 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/exceptions.cpython-310.pyc
ADDED
|
Binary file (16 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/protocols.cpython-310.pyc
ADDED
|
Binary file (6.62 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/__pycache__/validators.cpython-310.pyc
ADDED
|
Binary file (35.3 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/_format.py
ADDED
|
@@ -0,0 +1,519 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 |
+
|
| 3 |
+
from contextlib import suppress
|
| 4 |
+
from datetime import date, datetime
|
| 5 |
+
from uuid import UUID
|
| 6 |
+
import ipaddress
|
| 7 |
+
import re
|
| 8 |
+
import typing
|
| 9 |
+
import warnings
|
| 10 |
+
|
| 11 |
+
from jsonschema.exceptions import FormatError
|
| 12 |
+
|
| 13 |
+
_FormatCheckCallable = typing.Callable[[object], bool]
|
| 14 |
+
#: A format checker callable.
|
| 15 |
+
_F = typing.TypeVar("_F", bound=_FormatCheckCallable)
|
| 16 |
+
_RaisesType = typing.Union[
|
| 17 |
+
typing.Type[Exception], typing.Tuple[typing.Type[Exception], ...],
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
_RE_DATE = re.compile(r"^\d{4}-\d{2}-\d{2}$", re.ASCII)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class FormatChecker:
|
| 24 |
+
"""
|
| 25 |
+
A ``format`` property checker.
|
| 26 |
+
|
| 27 |
+
JSON Schema does not mandate that the ``format`` property actually do any
|
| 28 |
+
validation. If validation is desired however, instances of this class can
|
| 29 |
+
be hooked into validators to enable format validation.
|
| 30 |
+
|
| 31 |
+
`FormatChecker` objects always return ``True`` when asked about
|
| 32 |
+
formats that they do not know how to validate.
|
| 33 |
+
|
| 34 |
+
To add a check for a custom format use the `FormatChecker.checks`
|
| 35 |
+
decorator.
|
| 36 |
+
|
| 37 |
+
Arguments:
|
| 38 |
+
|
| 39 |
+
formats:
|
| 40 |
+
|
| 41 |
+
The known formats to validate. This argument can be used to
|
| 42 |
+
limit which formats will be used during validation.
|
| 43 |
+
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
checkers: dict[
|
| 47 |
+
str,
|
| 48 |
+
tuple[_FormatCheckCallable, _RaisesType],
|
| 49 |
+
] = {} # noqa: RUF012
|
| 50 |
+
|
| 51 |
+
def __init__(self, formats: typing.Iterable[str] | None = None):
|
| 52 |
+
if formats is None:
|
| 53 |
+
formats = self.checkers.keys()
|
| 54 |
+
self.checkers = {k: self.checkers[k] for k in formats}
|
| 55 |
+
|
| 56 |
+
def __repr__(self):
|
| 57 |
+
return f"<FormatChecker checkers={sorted(self.checkers)}>"
|
| 58 |
+
|
| 59 |
+
def checks(
|
| 60 |
+
self, format: str, raises: _RaisesType = (),
|
| 61 |
+
) -> typing.Callable[[_F], _F]:
|
| 62 |
+
"""
|
| 63 |
+
Register a decorated function as validating a new format.
|
| 64 |
+
|
| 65 |
+
Arguments:
|
| 66 |
+
|
| 67 |
+
format:
|
| 68 |
+
|
| 69 |
+
The format that the decorated function will check.
|
| 70 |
+
|
| 71 |
+
raises:
|
| 72 |
+
|
| 73 |
+
The exception(s) raised by the decorated function when an
|
| 74 |
+
invalid instance is found.
|
| 75 |
+
|
| 76 |
+
The exception object will be accessible as the
|
| 77 |
+
`jsonschema.exceptions.ValidationError.cause` attribute of the
|
| 78 |
+
resulting validation error.
|
| 79 |
+
|
| 80 |
+
"""
|
| 81 |
+
|
| 82 |
+
def _checks(func: _F) -> _F:
|
| 83 |
+
self.checkers[format] = (func, raises)
|
| 84 |
+
return func
|
| 85 |
+
|
| 86 |
+
return _checks
|
| 87 |
+
|
| 88 |
+
@classmethod
|
| 89 |
+
def cls_checks(
|
| 90 |
+
cls, format: str, raises: _RaisesType = (),
|
| 91 |
+
) -> typing.Callable[[_F], _F]:
|
| 92 |
+
warnings.warn(
|
| 93 |
+
(
|
| 94 |
+
"FormatChecker.cls_checks is deprecated. Call "
|
| 95 |
+
"FormatChecker.checks on a specific FormatChecker instance "
|
| 96 |
+
"instead."
|
| 97 |
+
),
|
| 98 |
+
DeprecationWarning,
|
| 99 |
+
stacklevel=2,
|
| 100 |
+
)
|
| 101 |
+
return cls._cls_checks(format=format, raises=raises)
|
| 102 |
+
|
| 103 |
+
@classmethod
|
| 104 |
+
def _cls_checks(
|
| 105 |
+
cls, format: str, raises: _RaisesType = (),
|
| 106 |
+
) -> typing.Callable[[_F], _F]:
|
| 107 |
+
def _checks(func: _F) -> _F:
|
| 108 |
+
cls.checkers[format] = (func, raises)
|
| 109 |
+
return func
|
| 110 |
+
|
| 111 |
+
return _checks
|
| 112 |
+
|
| 113 |
+
def check(self, instance: object, format: str) -> None:
|
| 114 |
+
"""
|
| 115 |
+
Check whether the instance conforms to the given format.
|
| 116 |
+
|
| 117 |
+
Arguments:
|
| 118 |
+
|
| 119 |
+
instance (*any primitive type*, i.e. str, number, bool):
|
| 120 |
+
|
| 121 |
+
The instance to check
|
| 122 |
+
|
| 123 |
+
format:
|
| 124 |
+
|
| 125 |
+
The format that instance should conform to
|
| 126 |
+
|
| 127 |
+
Raises:
|
| 128 |
+
|
| 129 |
+
FormatError:
|
| 130 |
+
|
| 131 |
+
if the instance does not conform to ``format``
|
| 132 |
+
|
| 133 |
+
"""
|
| 134 |
+
if format not in self.checkers:
|
| 135 |
+
return
|
| 136 |
+
|
| 137 |
+
func, raises = self.checkers[format]
|
| 138 |
+
result, cause = None, None
|
| 139 |
+
try:
|
| 140 |
+
result = func(instance)
|
| 141 |
+
except raises as e:
|
| 142 |
+
cause = e
|
| 143 |
+
if not result:
|
| 144 |
+
raise FormatError(f"{instance!r} is not a {format!r}", cause=cause)
|
| 145 |
+
|
| 146 |
+
def conforms(self, instance: object, format: str) -> bool:
|
| 147 |
+
"""
|
| 148 |
+
Check whether the instance conforms to the given format.
|
| 149 |
+
|
| 150 |
+
Arguments:
|
| 151 |
+
|
| 152 |
+
instance (*any primitive type*, i.e. str, number, bool):
|
| 153 |
+
|
| 154 |
+
The instance to check
|
| 155 |
+
|
| 156 |
+
format:
|
| 157 |
+
|
| 158 |
+
The format that instance should conform to
|
| 159 |
+
|
| 160 |
+
Returns:
|
| 161 |
+
|
| 162 |
+
bool: whether it conformed
|
| 163 |
+
|
| 164 |
+
"""
|
| 165 |
+
try:
|
| 166 |
+
self.check(instance, format)
|
| 167 |
+
except FormatError:
|
| 168 |
+
return False
|
| 169 |
+
else:
|
| 170 |
+
return True
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
draft3_format_checker = FormatChecker()
|
| 174 |
+
draft4_format_checker = FormatChecker()
|
| 175 |
+
draft6_format_checker = FormatChecker()
|
| 176 |
+
draft7_format_checker = FormatChecker()
|
| 177 |
+
draft201909_format_checker = FormatChecker()
|
| 178 |
+
draft202012_format_checker = FormatChecker()
|
| 179 |
+
|
| 180 |
+
_draft_checkers: dict[str, FormatChecker] = dict(
|
| 181 |
+
draft3=draft3_format_checker,
|
| 182 |
+
draft4=draft4_format_checker,
|
| 183 |
+
draft6=draft6_format_checker,
|
| 184 |
+
draft7=draft7_format_checker,
|
| 185 |
+
draft201909=draft201909_format_checker,
|
| 186 |
+
draft202012=draft202012_format_checker,
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def _checks_drafts(
|
| 191 |
+
name=None,
|
| 192 |
+
draft3=None,
|
| 193 |
+
draft4=None,
|
| 194 |
+
draft6=None,
|
| 195 |
+
draft7=None,
|
| 196 |
+
draft201909=None,
|
| 197 |
+
draft202012=None,
|
| 198 |
+
raises=(),
|
| 199 |
+
) -> typing.Callable[[_F], _F]:
|
| 200 |
+
draft3 = draft3 or name
|
| 201 |
+
draft4 = draft4 or name
|
| 202 |
+
draft6 = draft6 or name
|
| 203 |
+
draft7 = draft7 or name
|
| 204 |
+
draft201909 = draft201909 or name
|
| 205 |
+
draft202012 = draft202012 or name
|
| 206 |
+
|
| 207 |
+
def wrap(func: _F) -> _F:
|
| 208 |
+
if draft3:
|
| 209 |
+
func = _draft_checkers["draft3"].checks(draft3, raises)(func)
|
| 210 |
+
if draft4:
|
| 211 |
+
func = _draft_checkers["draft4"].checks(draft4, raises)(func)
|
| 212 |
+
if draft6:
|
| 213 |
+
func = _draft_checkers["draft6"].checks(draft6, raises)(func)
|
| 214 |
+
if draft7:
|
| 215 |
+
func = _draft_checkers["draft7"].checks(draft7, raises)(func)
|
| 216 |
+
if draft201909:
|
| 217 |
+
func = _draft_checkers["draft201909"].checks(draft201909, raises)(
|
| 218 |
+
func,
|
| 219 |
+
)
|
| 220 |
+
if draft202012:
|
| 221 |
+
func = _draft_checkers["draft202012"].checks(draft202012, raises)(
|
| 222 |
+
func,
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
# Oy. This is bad global state, but relied upon for now, until
|
| 226 |
+
# deprecation. See #519 and test_format_checkers_come_with_defaults
|
| 227 |
+
FormatChecker._cls_checks(
|
| 228 |
+
draft202012 or draft201909 or draft7 or draft6 or draft4 or draft3,
|
| 229 |
+
raises,
|
| 230 |
+
)(func)
|
| 231 |
+
return func
|
| 232 |
+
|
| 233 |
+
return wrap
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
@_checks_drafts(name="idn-email")
|
| 237 |
+
@_checks_drafts(name="email")
|
| 238 |
+
def is_email(instance: object) -> bool:
|
| 239 |
+
if not isinstance(instance, str):
|
| 240 |
+
return True
|
| 241 |
+
return "@" in instance
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
@_checks_drafts(
|
| 245 |
+
draft3="ip-address",
|
| 246 |
+
draft4="ipv4",
|
| 247 |
+
draft6="ipv4",
|
| 248 |
+
draft7="ipv4",
|
| 249 |
+
draft201909="ipv4",
|
| 250 |
+
draft202012="ipv4",
|
| 251 |
+
raises=ipaddress.AddressValueError,
|
| 252 |
+
)
|
| 253 |
+
def is_ipv4(instance: object) -> bool:
|
| 254 |
+
if not isinstance(instance, str):
|
| 255 |
+
return True
|
| 256 |
+
return bool(ipaddress.IPv4Address(instance))
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
@_checks_drafts(name="ipv6", raises=ipaddress.AddressValueError)
|
| 260 |
+
def is_ipv6(instance: object) -> bool:
|
| 261 |
+
if not isinstance(instance, str):
|
| 262 |
+
return True
|
| 263 |
+
address = ipaddress.IPv6Address(instance)
|
| 264 |
+
return not getattr(address, "scope_id", "")
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
with suppress(ImportError):
|
| 268 |
+
from fqdn import FQDN
|
| 269 |
+
|
| 270 |
+
@_checks_drafts(
|
| 271 |
+
draft3="host-name",
|
| 272 |
+
draft4="hostname",
|
| 273 |
+
draft6="hostname",
|
| 274 |
+
draft7="hostname",
|
| 275 |
+
draft201909="hostname",
|
| 276 |
+
draft202012="hostname",
|
| 277 |
+
)
|
| 278 |
+
def is_host_name(instance: object) -> bool:
|
| 279 |
+
if not isinstance(instance, str):
|
| 280 |
+
return True
|
| 281 |
+
return FQDN(instance, min_labels=1).is_valid
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
with suppress(ImportError):
|
| 285 |
+
# The built-in `idna` codec only implements RFC 3890, so we go elsewhere.
|
| 286 |
+
import idna
|
| 287 |
+
|
| 288 |
+
@_checks_drafts(
|
| 289 |
+
draft7="idn-hostname",
|
| 290 |
+
draft201909="idn-hostname",
|
| 291 |
+
draft202012="idn-hostname",
|
| 292 |
+
raises=(idna.IDNAError, UnicodeError),
|
| 293 |
+
)
|
| 294 |
+
def is_idn_host_name(instance: object) -> bool:
|
| 295 |
+
if not isinstance(instance, str):
|
| 296 |
+
return True
|
| 297 |
+
idna.encode(instance)
|
| 298 |
+
return True
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
try:
|
| 302 |
+
import rfc3987
|
| 303 |
+
except ImportError:
|
| 304 |
+
with suppress(ImportError):
|
| 305 |
+
from rfc3986_validator import validate_rfc3986
|
| 306 |
+
|
| 307 |
+
@_checks_drafts(name="uri")
|
| 308 |
+
def is_uri(instance: object) -> bool:
|
| 309 |
+
if not isinstance(instance, str):
|
| 310 |
+
return True
|
| 311 |
+
return validate_rfc3986(instance, rule="URI")
|
| 312 |
+
|
| 313 |
+
@_checks_drafts(
|
| 314 |
+
draft6="uri-reference",
|
| 315 |
+
draft7="uri-reference",
|
| 316 |
+
draft201909="uri-reference",
|
| 317 |
+
draft202012="uri-reference",
|
| 318 |
+
raises=ValueError,
|
| 319 |
+
)
|
| 320 |
+
def is_uri_reference(instance: object) -> bool:
|
| 321 |
+
if not isinstance(instance, str):
|
| 322 |
+
return True
|
| 323 |
+
return validate_rfc3986(instance, rule="URI_reference")
|
| 324 |
+
|
| 325 |
+
else:
|
| 326 |
+
|
| 327 |
+
@_checks_drafts(
|
| 328 |
+
draft7="iri",
|
| 329 |
+
draft201909="iri",
|
| 330 |
+
draft202012="iri",
|
| 331 |
+
raises=ValueError,
|
| 332 |
+
)
|
| 333 |
+
def is_iri(instance: object) -> bool:
|
| 334 |
+
if not isinstance(instance, str):
|
| 335 |
+
return True
|
| 336 |
+
return rfc3987.parse(instance, rule="IRI")
|
| 337 |
+
|
| 338 |
+
@_checks_drafts(
|
| 339 |
+
draft7="iri-reference",
|
| 340 |
+
draft201909="iri-reference",
|
| 341 |
+
draft202012="iri-reference",
|
| 342 |
+
raises=ValueError,
|
| 343 |
+
)
|
| 344 |
+
def is_iri_reference(instance: object) -> bool:
|
| 345 |
+
if not isinstance(instance, str):
|
| 346 |
+
return True
|
| 347 |
+
return rfc3987.parse(instance, rule="IRI_reference")
|
| 348 |
+
|
| 349 |
+
@_checks_drafts(name="uri", raises=ValueError)
|
| 350 |
+
def is_uri(instance: object) -> bool:
|
| 351 |
+
if not isinstance(instance, str):
|
| 352 |
+
return True
|
| 353 |
+
return rfc3987.parse(instance, rule="URI")
|
| 354 |
+
|
| 355 |
+
@_checks_drafts(
|
| 356 |
+
draft6="uri-reference",
|
| 357 |
+
draft7="uri-reference",
|
| 358 |
+
draft201909="uri-reference",
|
| 359 |
+
draft202012="uri-reference",
|
| 360 |
+
raises=ValueError,
|
| 361 |
+
)
|
| 362 |
+
def is_uri_reference(instance: object) -> bool:
|
| 363 |
+
if not isinstance(instance, str):
|
| 364 |
+
return True
|
| 365 |
+
return rfc3987.parse(instance, rule="URI_reference")
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
with suppress(ImportError):
|
| 369 |
+
from rfc3339_validator import validate_rfc3339
|
| 370 |
+
|
| 371 |
+
@_checks_drafts(name="date-time")
|
| 372 |
+
def is_datetime(instance: object) -> bool:
|
| 373 |
+
if not isinstance(instance, str):
|
| 374 |
+
return True
|
| 375 |
+
return validate_rfc3339(instance.upper())
|
| 376 |
+
|
| 377 |
+
@_checks_drafts(
|
| 378 |
+
draft7="time",
|
| 379 |
+
draft201909="time",
|
| 380 |
+
draft202012="time",
|
| 381 |
+
)
|
| 382 |
+
def is_time(instance: object) -> bool:
|
| 383 |
+
if not isinstance(instance, str):
|
| 384 |
+
return True
|
| 385 |
+
return is_datetime("1970-01-01T" + instance)
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
@_checks_drafts(name="regex", raises=re.error)
|
| 389 |
+
def is_regex(instance: object) -> bool:
|
| 390 |
+
if not isinstance(instance, str):
|
| 391 |
+
return True
|
| 392 |
+
return bool(re.compile(instance))
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
@_checks_drafts(
|
| 396 |
+
draft3="date",
|
| 397 |
+
draft7="date",
|
| 398 |
+
draft201909="date",
|
| 399 |
+
draft202012="date",
|
| 400 |
+
raises=ValueError,
|
| 401 |
+
)
|
| 402 |
+
def is_date(instance: object) -> bool:
|
| 403 |
+
if not isinstance(instance, str):
|
| 404 |
+
return True
|
| 405 |
+
return bool(_RE_DATE.fullmatch(instance) and date.fromisoformat(instance))
|
| 406 |
+
|
| 407 |
+
|
| 408 |
+
@_checks_drafts(draft3="time", raises=ValueError)
|
| 409 |
+
def is_draft3_time(instance: object) -> bool:
|
| 410 |
+
if not isinstance(instance, str):
|
| 411 |
+
return True
|
| 412 |
+
return bool(datetime.strptime(instance, "%H:%M:%S")) # noqa: DTZ007
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
with suppress(ImportError):
|
| 416 |
+
import webcolors
|
| 417 |
+
|
| 418 |
+
@_checks_drafts(draft3="color", raises=(ValueError, TypeError))
|
| 419 |
+
def is_css21_color(instance: object) -> bool:
|
| 420 |
+
if isinstance(instance, str):
|
| 421 |
+
try:
|
| 422 |
+
webcolors.name_to_hex(instance)
|
| 423 |
+
except ValueError:
|
| 424 |
+
webcolors.normalize_hex(instance.lower())
|
| 425 |
+
return True
|
| 426 |
+
|
| 427 |
+
|
| 428 |
+
with suppress(ImportError):
|
| 429 |
+
import jsonpointer
|
| 430 |
+
|
| 431 |
+
@_checks_drafts(
|
| 432 |
+
draft6="json-pointer",
|
| 433 |
+
draft7="json-pointer",
|
| 434 |
+
draft201909="json-pointer",
|
| 435 |
+
draft202012="json-pointer",
|
| 436 |
+
raises=jsonpointer.JsonPointerException,
|
| 437 |
+
)
|
| 438 |
+
def is_json_pointer(instance: object) -> bool:
|
| 439 |
+
if not isinstance(instance, str):
|
| 440 |
+
return True
|
| 441 |
+
return bool(jsonpointer.JsonPointer(instance))
|
| 442 |
+
|
| 443 |
+
# TODO: I don't want to maintain this, so it
|
| 444 |
+
# needs to go either into jsonpointer (pending
|
| 445 |
+
# https://github.com/stefankoegl/python-json-pointer/issues/34) or
|
| 446 |
+
# into a new external library.
|
| 447 |
+
@_checks_drafts(
|
| 448 |
+
draft7="relative-json-pointer",
|
| 449 |
+
draft201909="relative-json-pointer",
|
| 450 |
+
draft202012="relative-json-pointer",
|
| 451 |
+
raises=jsonpointer.JsonPointerException,
|
| 452 |
+
)
|
| 453 |
+
def is_relative_json_pointer(instance: object) -> bool:
|
| 454 |
+
# Definition taken from:
|
| 455 |
+
# https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01#section-3
|
| 456 |
+
if not isinstance(instance, str):
|
| 457 |
+
return True
|
| 458 |
+
if not instance:
|
| 459 |
+
return False
|
| 460 |
+
|
| 461 |
+
non_negative_integer, rest = [], ""
|
| 462 |
+
for i, character in enumerate(instance):
|
| 463 |
+
if character.isdigit():
|
| 464 |
+
# digits with a leading "0" are not allowed
|
| 465 |
+
if i > 0 and int(instance[i - 1]) == 0:
|
| 466 |
+
return False
|
| 467 |
+
|
| 468 |
+
non_negative_integer.append(character)
|
| 469 |
+
continue
|
| 470 |
+
|
| 471 |
+
if not non_negative_integer:
|
| 472 |
+
return False
|
| 473 |
+
|
| 474 |
+
rest = instance[i:]
|
| 475 |
+
break
|
| 476 |
+
return (rest == "#") or bool(jsonpointer.JsonPointer(rest))
|
| 477 |
+
|
| 478 |
+
|
| 479 |
+
with suppress(ImportError):
|
| 480 |
+
import uri_template
|
| 481 |
+
|
| 482 |
+
@_checks_drafts(
|
| 483 |
+
draft6="uri-template",
|
| 484 |
+
draft7="uri-template",
|
| 485 |
+
draft201909="uri-template",
|
| 486 |
+
draft202012="uri-template",
|
| 487 |
+
)
|
| 488 |
+
def is_uri_template(instance: object) -> bool:
|
| 489 |
+
if not isinstance(instance, str):
|
| 490 |
+
return True
|
| 491 |
+
return uri_template.validate(instance)
|
| 492 |
+
|
| 493 |
+
|
| 494 |
+
with suppress(ImportError):
|
| 495 |
+
import isoduration
|
| 496 |
+
|
| 497 |
+
@_checks_drafts(
|
| 498 |
+
draft201909="duration",
|
| 499 |
+
draft202012="duration",
|
| 500 |
+
raises=isoduration.DurationParsingException,
|
| 501 |
+
)
|
| 502 |
+
def is_duration(instance: object) -> bool:
|
| 503 |
+
if not isinstance(instance, str):
|
| 504 |
+
return True
|
| 505 |
+
isoduration.parse_duration(instance)
|
| 506 |
+
# FIXME: See bolsote/isoduration#25 and bolsote/isoduration#21
|
| 507 |
+
return instance.endswith(tuple("DMYWHMS"))
|
| 508 |
+
|
| 509 |
+
|
| 510 |
+
@_checks_drafts(
|
| 511 |
+
draft201909="uuid",
|
| 512 |
+
draft202012="uuid",
|
| 513 |
+
raises=ValueError,
|
| 514 |
+
)
|
| 515 |
+
def is_uuid(instance: object) -> bool:
|
| 516 |
+
if not isinstance(instance, str):
|
| 517 |
+
return True
|
| 518 |
+
UUID(instance)
|
| 519 |
+
return all(instance[position] == "-" for position in (8, 13, 18, 23))
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/_keywords.py
ADDED
|
@@ -0,0 +1,449 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fractions import Fraction
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
from jsonschema._utils import (
|
| 5 |
+
ensure_list,
|
| 6 |
+
equal,
|
| 7 |
+
extras_msg,
|
| 8 |
+
find_additional_properties,
|
| 9 |
+
find_evaluated_item_indexes_by_schema,
|
| 10 |
+
find_evaluated_property_keys_by_schema,
|
| 11 |
+
uniq,
|
| 12 |
+
)
|
| 13 |
+
from jsonschema.exceptions import FormatError, ValidationError
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def patternProperties(validator, patternProperties, instance, schema):
|
| 17 |
+
if not validator.is_type(instance, "object"):
|
| 18 |
+
return
|
| 19 |
+
|
| 20 |
+
for pattern, subschema in patternProperties.items():
|
| 21 |
+
for k, v in instance.items():
|
| 22 |
+
if re.search(pattern, k):
|
| 23 |
+
yield from validator.descend(
|
| 24 |
+
v, subschema, path=k, schema_path=pattern,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def propertyNames(validator, propertyNames, instance, schema):
|
| 29 |
+
if not validator.is_type(instance, "object"):
|
| 30 |
+
return
|
| 31 |
+
|
| 32 |
+
for property in instance:
|
| 33 |
+
yield from validator.descend(instance=property, schema=propertyNames)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def additionalProperties(validator, aP, instance, schema):
|
| 37 |
+
if not validator.is_type(instance, "object"):
|
| 38 |
+
return
|
| 39 |
+
|
| 40 |
+
extras = set(find_additional_properties(instance, schema))
|
| 41 |
+
|
| 42 |
+
if validator.is_type(aP, "object"):
|
| 43 |
+
for extra in extras:
|
| 44 |
+
yield from validator.descend(instance[extra], aP, path=extra)
|
| 45 |
+
elif not aP and extras:
|
| 46 |
+
if "patternProperties" in schema:
|
| 47 |
+
verb = "does" if len(extras) == 1 else "do"
|
| 48 |
+
joined = ", ".join(repr(each) for each in sorted(extras))
|
| 49 |
+
patterns = ", ".join(
|
| 50 |
+
repr(each) for each in sorted(schema["patternProperties"])
|
| 51 |
+
)
|
| 52 |
+
error = f"{joined} {verb} not match any of the regexes: {patterns}"
|
| 53 |
+
yield ValidationError(error)
|
| 54 |
+
else:
|
| 55 |
+
error = "Additional properties are not allowed (%s %s unexpected)"
|
| 56 |
+
yield ValidationError(error % extras_msg(sorted(extras, key=str)))
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def items(validator, items, instance, schema):
|
| 60 |
+
if not validator.is_type(instance, "array"):
|
| 61 |
+
return
|
| 62 |
+
|
| 63 |
+
prefix = len(schema.get("prefixItems", []))
|
| 64 |
+
total = len(instance)
|
| 65 |
+
extra = total - prefix
|
| 66 |
+
if extra <= 0:
|
| 67 |
+
return
|
| 68 |
+
|
| 69 |
+
if items is False:
|
| 70 |
+
rest = instance[prefix:] if extra != 1 else instance[prefix]
|
| 71 |
+
item = "items" if prefix != 1 else "item"
|
| 72 |
+
yield ValidationError(
|
| 73 |
+
f"Expected at most {prefix} {item} but found {extra} "
|
| 74 |
+
f"extra: {rest!r}",
|
| 75 |
+
)
|
| 76 |
+
else:
|
| 77 |
+
for index in range(prefix, total):
|
| 78 |
+
yield from validator.descend(
|
| 79 |
+
instance=instance[index],
|
| 80 |
+
schema=items,
|
| 81 |
+
path=index,
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def const(validator, const, instance, schema):
|
| 86 |
+
if not equal(instance, const):
|
| 87 |
+
yield ValidationError(f"{const!r} was expected")
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def contains(validator, contains, instance, schema):
|
| 91 |
+
if not validator.is_type(instance, "array"):
|
| 92 |
+
return
|
| 93 |
+
|
| 94 |
+
matches = 0
|
| 95 |
+
min_contains = schema.get("minContains", 1)
|
| 96 |
+
max_contains = schema.get("maxContains", len(instance))
|
| 97 |
+
|
| 98 |
+
contains_validator = validator.evolve(schema=contains)
|
| 99 |
+
|
| 100 |
+
for each in instance:
|
| 101 |
+
if contains_validator.is_valid(each):
|
| 102 |
+
matches += 1
|
| 103 |
+
if matches > max_contains:
|
| 104 |
+
yield ValidationError(
|
| 105 |
+
"Too many items match the given schema "
|
| 106 |
+
f"(expected at most {max_contains})",
|
| 107 |
+
validator="maxContains",
|
| 108 |
+
validator_value=max_contains,
|
| 109 |
+
)
|
| 110 |
+
return
|
| 111 |
+
|
| 112 |
+
if matches < min_contains:
|
| 113 |
+
if not matches:
|
| 114 |
+
yield ValidationError(
|
| 115 |
+
f"{instance!r} does not contain items "
|
| 116 |
+
"matching the given schema",
|
| 117 |
+
)
|
| 118 |
+
else:
|
| 119 |
+
yield ValidationError(
|
| 120 |
+
"Too few items match the given schema (expected at least "
|
| 121 |
+
f"{min_contains} but only {matches} matched)",
|
| 122 |
+
validator="minContains",
|
| 123 |
+
validator_value=min_contains,
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def exclusiveMinimum(validator, minimum, instance, schema):
|
| 128 |
+
if not validator.is_type(instance, "number"):
|
| 129 |
+
return
|
| 130 |
+
|
| 131 |
+
if instance <= minimum:
|
| 132 |
+
yield ValidationError(
|
| 133 |
+
f"{instance!r} is less than or equal to "
|
| 134 |
+
f"the minimum of {minimum!r}",
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def exclusiveMaximum(validator, maximum, instance, schema):
|
| 139 |
+
if not validator.is_type(instance, "number"):
|
| 140 |
+
return
|
| 141 |
+
|
| 142 |
+
if instance >= maximum:
|
| 143 |
+
yield ValidationError(
|
| 144 |
+
f"{instance!r} is greater than or equal "
|
| 145 |
+
f"to the maximum of {maximum!r}",
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def minimum(validator, minimum, instance, schema):
|
| 150 |
+
if not validator.is_type(instance, "number"):
|
| 151 |
+
return
|
| 152 |
+
|
| 153 |
+
if instance < minimum:
|
| 154 |
+
message = f"{instance!r} is less than the minimum of {minimum!r}"
|
| 155 |
+
yield ValidationError(message)
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def maximum(validator, maximum, instance, schema):
|
| 159 |
+
if not validator.is_type(instance, "number"):
|
| 160 |
+
return
|
| 161 |
+
|
| 162 |
+
if instance > maximum:
|
| 163 |
+
message = f"{instance!r} is greater than the maximum of {maximum!r}"
|
| 164 |
+
yield ValidationError(message)
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
def multipleOf(validator, dB, instance, schema):
|
| 168 |
+
if not validator.is_type(instance, "number"):
|
| 169 |
+
return
|
| 170 |
+
|
| 171 |
+
if isinstance(dB, float):
|
| 172 |
+
quotient = instance / dB
|
| 173 |
+
try:
|
| 174 |
+
failed = int(quotient) != quotient
|
| 175 |
+
except OverflowError:
|
| 176 |
+
# When `instance` is large and `dB` is less than one,
|
| 177 |
+
# quotient can overflow to infinity; and then casting to int
|
| 178 |
+
# raises an error.
|
| 179 |
+
#
|
| 180 |
+
# In this case we fall back to Fraction logic, which is
|
| 181 |
+
# exact and cannot overflow. The performance is also
|
| 182 |
+
# acceptable: we try the fast all-float option first, and
|
| 183 |
+
# we know that fraction(dB) can have at most a few hundred
|
| 184 |
+
# digits in each part. The worst-case slowdown is therefore
|
| 185 |
+
# for already-slow enormous integers or Decimals.
|
| 186 |
+
failed = (Fraction(instance) / Fraction(dB)).denominator != 1
|
| 187 |
+
else:
|
| 188 |
+
failed = instance % dB
|
| 189 |
+
|
| 190 |
+
if failed:
|
| 191 |
+
yield ValidationError(f"{instance!r} is not a multiple of {dB}")
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def minItems(validator, mI, instance, schema):
|
| 195 |
+
if validator.is_type(instance, "array") and len(instance) < mI:
|
| 196 |
+
message = "should be non-empty" if mI == 1 else "is too short"
|
| 197 |
+
yield ValidationError(f"{instance!r} {message}")
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
def maxItems(validator, mI, instance, schema):
|
| 201 |
+
if validator.is_type(instance, "array") and len(instance) > mI:
|
| 202 |
+
message = "is expected to be empty" if mI == 0 else "is too long"
|
| 203 |
+
yield ValidationError(f"{instance!r} {message}")
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
def uniqueItems(validator, uI, instance, schema):
|
| 207 |
+
if (
|
| 208 |
+
uI
|
| 209 |
+
and validator.is_type(instance, "array")
|
| 210 |
+
and not uniq(instance)
|
| 211 |
+
):
|
| 212 |
+
yield ValidationError(f"{instance!r} has non-unique elements")
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
def pattern(validator, patrn, instance, schema):
|
| 216 |
+
if (
|
| 217 |
+
validator.is_type(instance, "string")
|
| 218 |
+
and not re.search(patrn, instance)
|
| 219 |
+
):
|
| 220 |
+
yield ValidationError(f"{instance!r} does not match {patrn!r}")
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
def format(validator, format, instance, schema):
|
| 224 |
+
if validator.format_checker is not None:
|
| 225 |
+
try:
|
| 226 |
+
validator.format_checker.check(instance, format)
|
| 227 |
+
except FormatError as error:
|
| 228 |
+
yield ValidationError(error.message, cause=error.cause)
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
def minLength(validator, mL, instance, schema):
|
| 232 |
+
if validator.is_type(instance, "string") and len(instance) < mL:
|
| 233 |
+
message = "should be non-empty" if mL == 1 else "is too short"
|
| 234 |
+
yield ValidationError(f"{instance!r} {message}")
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
def maxLength(validator, mL, instance, schema):
|
| 238 |
+
if validator.is_type(instance, "string") and len(instance) > mL:
|
| 239 |
+
message = "is expected to be empty" if mL == 0 else "is too long"
|
| 240 |
+
yield ValidationError(f"{instance!r} {message}")
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def dependentRequired(validator, dependentRequired, instance, schema):
|
| 244 |
+
if not validator.is_type(instance, "object"):
|
| 245 |
+
return
|
| 246 |
+
|
| 247 |
+
for property, dependency in dependentRequired.items():
|
| 248 |
+
if property not in instance:
|
| 249 |
+
continue
|
| 250 |
+
|
| 251 |
+
for each in dependency:
|
| 252 |
+
if each not in instance:
|
| 253 |
+
message = f"{each!r} is a dependency of {property!r}"
|
| 254 |
+
yield ValidationError(message)
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
def dependentSchemas(validator, dependentSchemas, instance, schema):
|
| 258 |
+
if not validator.is_type(instance, "object"):
|
| 259 |
+
return
|
| 260 |
+
|
| 261 |
+
for property, dependency in dependentSchemas.items():
|
| 262 |
+
if property not in instance:
|
| 263 |
+
continue
|
| 264 |
+
yield from validator.descend(
|
| 265 |
+
instance, dependency, schema_path=property,
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
def enum(validator, enums, instance, schema):
|
| 270 |
+
if all(not equal(each, instance) for each in enums):
|
| 271 |
+
yield ValidationError(f"{instance!r} is not one of {enums!r}")
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
def ref(validator, ref, instance, schema):
|
| 275 |
+
yield from validator._validate_reference(ref=ref, instance=instance)
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
def dynamicRef(validator, dynamicRef, instance, schema):
|
| 279 |
+
yield from validator._validate_reference(ref=dynamicRef, instance=instance)
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
def type(validator, types, instance, schema):
|
| 283 |
+
types = ensure_list(types)
|
| 284 |
+
|
| 285 |
+
if not any(validator.is_type(instance, type) for type in types):
|
| 286 |
+
reprs = ", ".join(repr(type) for type in types)
|
| 287 |
+
yield ValidationError(f"{instance!r} is not of type {reprs}")
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
def properties(validator, properties, instance, schema):
|
| 291 |
+
if not validator.is_type(instance, "object"):
|
| 292 |
+
return
|
| 293 |
+
|
| 294 |
+
for property, subschema in properties.items():
|
| 295 |
+
if property in instance:
|
| 296 |
+
yield from validator.descend(
|
| 297 |
+
instance[property],
|
| 298 |
+
subschema,
|
| 299 |
+
path=property,
|
| 300 |
+
schema_path=property,
|
| 301 |
+
)
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
def required(validator, required, instance, schema):
|
| 305 |
+
if not validator.is_type(instance, "object"):
|
| 306 |
+
return
|
| 307 |
+
for property in required:
|
| 308 |
+
if property not in instance:
|
| 309 |
+
yield ValidationError(f"{property!r} is a required property")
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
def minProperties(validator, mP, instance, schema):
|
| 313 |
+
if validator.is_type(instance, "object") and len(instance) < mP:
|
| 314 |
+
message = (
|
| 315 |
+
"should be non-empty" if mP == 1
|
| 316 |
+
else "does not have enough properties"
|
| 317 |
+
)
|
| 318 |
+
yield ValidationError(f"{instance!r} {message}")
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
def maxProperties(validator, mP, instance, schema):
|
| 322 |
+
if not validator.is_type(instance, "object"):
|
| 323 |
+
return
|
| 324 |
+
if validator.is_type(instance, "object") and len(instance) > mP:
|
| 325 |
+
message = (
|
| 326 |
+
"is expected to be empty" if mP == 0
|
| 327 |
+
else "has too many properties"
|
| 328 |
+
)
|
| 329 |
+
yield ValidationError(f"{instance!r} {message}")
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
def allOf(validator, allOf, instance, schema):
|
| 333 |
+
for index, subschema in enumerate(allOf):
|
| 334 |
+
yield from validator.descend(instance, subschema, schema_path=index)
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
def anyOf(validator, anyOf, instance, schema):
|
| 338 |
+
all_errors = []
|
| 339 |
+
for index, subschema in enumerate(anyOf):
|
| 340 |
+
errs = list(validator.descend(instance, subschema, schema_path=index))
|
| 341 |
+
if not errs:
|
| 342 |
+
break
|
| 343 |
+
all_errors.extend(errs)
|
| 344 |
+
else:
|
| 345 |
+
yield ValidationError(
|
| 346 |
+
f"{instance!r} is not valid under any of the given schemas",
|
| 347 |
+
context=all_errors,
|
| 348 |
+
)
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
def oneOf(validator, oneOf, instance, schema):
|
| 352 |
+
subschemas = enumerate(oneOf)
|
| 353 |
+
all_errors = []
|
| 354 |
+
for index, subschema in subschemas:
|
| 355 |
+
errs = list(validator.descend(instance, subschema, schema_path=index))
|
| 356 |
+
if not errs:
|
| 357 |
+
first_valid = subschema
|
| 358 |
+
break
|
| 359 |
+
all_errors.extend(errs)
|
| 360 |
+
else:
|
| 361 |
+
yield ValidationError(
|
| 362 |
+
f"{instance!r} is not valid under any of the given schemas",
|
| 363 |
+
context=all_errors,
|
| 364 |
+
)
|
| 365 |
+
|
| 366 |
+
more_valid = [
|
| 367 |
+
each for _, each in subschemas
|
| 368 |
+
if validator.evolve(schema=each).is_valid(instance)
|
| 369 |
+
]
|
| 370 |
+
if more_valid:
|
| 371 |
+
more_valid.append(first_valid)
|
| 372 |
+
reprs = ", ".join(repr(schema) for schema in more_valid)
|
| 373 |
+
yield ValidationError(f"{instance!r} is valid under each of {reprs}")
|
| 374 |
+
|
| 375 |
+
|
| 376 |
+
def not_(validator, not_schema, instance, schema):
|
| 377 |
+
if validator.evolve(schema=not_schema).is_valid(instance):
|
| 378 |
+
message = f"{instance!r} should not be valid under {not_schema!r}"
|
| 379 |
+
yield ValidationError(message)
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
def if_(validator, if_schema, instance, schema):
|
| 383 |
+
if validator.evolve(schema=if_schema).is_valid(instance):
|
| 384 |
+
if "then" in schema:
|
| 385 |
+
then = schema["then"]
|
| 386 |
+
yield from validator.descend(instance, then, schema_path="then")
|
| 387 |
+
elif "else" in schema:
|
| 388 |
+
else_ = schema["else"]
|
| 389 |
+
yield from validator.descend(instance, else_, schema_path="else")
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
def unevaluatedItems(validator, unevaluatedItems, instance, schema):
|
| 393 |
+
if not validator.is_type(instance, "array"):
|
| 394 |
+
return
|
| 395 |
+
evaluated_item_indexes = find_evaluated_item_indexes_by_schema(
|
| 396 |
+
validator, instance, schema,
|
| 397 |
+
)
|
| 398 |
+
unevaluated_items = [
|
| 399 |
+
item for index, item in enumerate(instance)
|
| 400 |
+
if index not in evaluated_item_indexes
|
| 401 |
+
]
|
| 402 |
+
if unevaluated_items:
|
| 403 |
+
error = "Unevaluated items are not allowed (%s %s unexpected)"
|
| 404 |
+
yield ValidationError(error % extras_msg(unevaluated_items))
|
| 405 |
+
|
| 406 |
+
|
| 407 |
+
def unevaluatedProperties(validator, unevaluatedProperties, instance, schema):
|
| 408 |
+
if not validator.is_type(instance, "object"):
|
| 409 |
+
return
|
| 410 |
+
evaluated_keys = find_evaluated_property_keys_by_schema(
|
| 411 |
+
validator, instance, schema,
|
| 412 |
+
)
|
| 413 |
+
unevaluated_keys = []
|
| 414 |
+
for property in instance:
|
| 415 |
+
if property not in evaluated_keys:
|
| 416 |
+
for _ in validator.descend(
|
| 417 |
+
instance[property],
|
| 418 |
+
unevaluatedProperties,
|
| 419 |
+
path=property,
|
| 420 |
+
schema_path=property,
|
| 421 |
+
):
|
| 422 |
+
# FIXME: Include context for each unevaluated property
|
| 423 |
+
# indicating why it's invalid under the subschema.
|
| 424 |
+
unevaluated_keys.append(property) # noqa: PERF401
|
| 425 |
+
|
| 426 |
+
if unevaluated_keys:
|
| 427 |
+
if unevaluatedProperties is False:
|
| 428 |
+
error = "Unevaluated properties are not allowed (%s %s unexpected)"
|
| 429 |
+
extras = sorted(unevaluated_keys, key=str)
|
| 430 |
+
yield ValidationError(error % extras_msg(extras))
|
| 431 |
+
else:
|
| 432 |
+
error = (
|
| 433 |
+
"Unevaluated properties are not valid under "
|
| 434 |
+
"the given schema (%s %s unevaluated and invalid)"
|
| 435 |
+
)
|
| 436 |
+
yield ValidationError(error % extras_msg(unevaluated_keys))
|
| 437 |
+
|
| 438 |
+
|
| 439 |
+
def prefixItems(validator, prefixItems, instance, schema):
|
| 440 |
+
if not validator.is_type(instance, "array"):
|
| 441 |
+
return
|
| 442 |
+
|
| 443 |
+
for (index, item), subschema in zip(enumerate(instance), prefixItems):
|
| 444 |
+
yield from validator.descend(
|
| 445 |
+
instance=item,
|
| 446 |
+
schema=subschema,
|
| 447 |
+
schema_path=index,
|
| 448 |
+
path=index,
|
| 449 |
+
)
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/_legacy_keywords.py
ADDED
|
@@ -0,0 +1,449 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
from referencing.jsonschema import lookup_recursive_ref
|
| 4 |
+
|
| 5 |
+
from jsonschema import _utils
|
| 6 |
+
from jsonschema.exceptions import ValidationError
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def ignore_ref_siblings(schema):
|
| 10 |
+
"""
|
| 11 |
+
Ignore siblings of ``$ref`` if it is present.
|
| 12 |
+
|
| 13 |
+
Otherwise, return all keywords.
|
| 14 |
+
|
| 15 |
+
Suitable for use with `create`'s ``applicable_validators`` argument.
|
| 16 |
+
"""
|
| 17 |
+
ref = schema.get("$ref")
|
| 18 |
+
if ref is not None:
|
| 19 |
+
return [("$ref", ref)]
|
| 20 |
+
else:
|
| 21 |
+
return schema.items()
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def dependencies_draft3(validator, dependencies, instance, schema):
|
| 25 |
+
if not validator.is_type(instance, "object"):
|
| 26 |
+
return
|
| 27 |
+
|
| 28 |
+
for property, dependency in dependencies.items():
|
| 29 |
+
if property not in instance:
|
| 30 |
+
continue
|
| 31 |
+
|
| 32 |
+
if validator.is_type(dependency, "object"):
|
| 33 |
+
yield from validator.descend(
|
| 34 |
+
instance, dependency, schema_path=property,
|
| 35 |
+
)
|
| 36 |
+
elif validator.is_type(dependency, "string"):
|
| 37 |
+
if dependency not in instance:
|
| 38 |
+
message = f"{dependency!r} is a dependency of {property!r}"
|
| 39 |
+
yield ValidationError(message)
|
| 40 |
+
else:
|
| 41 |
+
for each in dependency:
|
| 42 |
+
if each not in instance:
|
| 43 |
+
message = f"{each!r} is a dependency of {property!r}"
|
| 44 |
+
yield ValidationError(message)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def dependencies_draft4_draft6_draft7(
|
| 48 |
+
validator,
|
| 49 |
+
dependencies,
|
| 50 |
+
instance,
|
| 51 |
+
schema,
|
| 52 |
+
):
|
| 53 |
+
"""
|
| 54 |
+
Support for the ``dependencies`` keyword from pre-draft 2019-09.
|
| 55 |
+
|
| 56 |
+
In later drafts, the keyword was split into separate
|
| 57 |
+
``dependentRequired`` and ``dependentSchemas`` validators.
|
| 58 |
+
"""
|
| 59 |
+
if not validator.is_type(instance, "object"):
|
| 60 |
+
return
|
| 61 |
+
|
| 62 |
+
for property, dependency in dependencies.items():
|
| 63 |
+
if property not in instance:
|
| 64 |
+
continue
|
| 65 |
+
|
| 66 |
+
if validator.is_type(dependency, "array"):
|
| 67 |
+
for each in dependency:
|
| 68 |
+
if each not in instance:
|
| 69 |
+
message = f"{each!r} is a dependency of {property!r}"
|
| 70 |
+
yield ValidationError(message)
|
| 71 |
+
else:
|
| 72 |
+
yield from validator.descend(
|
| 73 |
+
instance, dependency, schema_path=property,
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def disallow_draft3(validator, disallow, instance, schema):
|
| 78 |
+
for disallowed in _utils.ensure_list(disallow):
|
| 79 |
+
if validator.evolve(schema={"type": [disallowed]}).is_valid(instance):
|
| 80 |
+
message = f"{disallowed!r} is disallowed for {instance!r}"
|
| 81 |
+
yield ValidationError(message)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def extends_draft3(validator, extends, instance, schema):
|
| 85 |
+
if validator.is_type(extends, "object"):
|
| 86 |
+
yield from validator.descend(instance, extends)
|
| 87 |
+
return
|
| 88 |
+
for index, subschema in enumerate(extends):
|
| 89 |
+
yield from validator.descend(instance, subschema, schema_path=index)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def items_draft3_draft4(validator, items, instance, schema):
|
| 93 |
+
if not validator.is_type(instance, "array"):
|
| 94 |
+
return
|
| 95 |
+
|
| 96 |
+
if validator.is_type(items, "object"):
|
| 97 |
+
for index, item in enumerate(instance):
|
| 98 |
+
yield from validator.descend(item, items, path=index)
|
| 99 |
+
else:
|
| 100 |
+
for (index, item), subschema in zip(enumerate(instance), items):
|
| 101 |
+
yield from validator.descend(
|
| 102 |
+
item, subschema, path=index, schema_path=index,
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def additionalItems(validator, aI, instance, schema):
|
| 107 |
+
if (
|
| 108 |
+
not validator.is_type(instance, "array")
|
| 109 |
+
or validator.is_type(schema.get("items", {}), "object")
|
| 110 |
+
):
|
| 111 |
+
return
|
| 112 |
+
|
| 113 |
+
len_items = len(schema.get("items", []))
|
| 114 |
+
if validator.is_type(aI, "object"):
|
| 115 |
+
for index, item in enumerate(instance[len_items:], start=len_items):
|
| 116 |
+
yield from validator.descend(item, aI, path=index)
|
| 117 |
+
elif not aI and len(instance) > len(schema.get("items", [])):
|
| 118 |
+
error = "Additional items are not allowed (%s %s unexpected)"
|
| 119 |
+
yield ValidationError(
|
| 120 |
+
error % _utils.extras_msg(instance[len(schema.get("items", [])):]),
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def items_draft6_draft7_draft201909(validator, items, instance, schema):
|
| 125 |
+
if not validator.is_type(instance, "array"):
|
| 126 |
+
return
|
| 127 |
+
|
| 128 |
+
if validator.is_type(items, "array"):
|
| 129 |
+
for (index, item), subschema in zip(enumerate(instance), items):
|
| 130 |
+
yield from validator.descend(
|
| 131 |
+
item, subschema, path=index, schema_path=index,
|
| 132 |
+
)
|
| 133 |
+
else:
|
| 134 |
+
for index, item in enumerate(instance):
|
| 135 |
+
yield from validator.descend(item, items, path=index)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def minimum_draft3_draft4(validator, minimum, instance, schema):
|
| 139 |
+
if not validator.is_type(instance, "number"):
|
| 140 |
+
return
|
| 141 |
+
|
| 142 |
+
if schema.get("exclusiveMinimum", False):
|
| 143 |
+
failed = instance <= minimum
|
| 144 |
+
cmp = "less than or equal to"
|
| 145 |
+
else:
|
| 146 |
+
failed = instance < minimum
|
| 147 |
+
cmp = "less than"
|
| 148 |
+
|
| 149 |
+
if failed:
|
| 150 |
+
message = f"{instance!r} is {cmp} the minimum of {minimum!r}"
|
| 151 |
+
yield ValidationError(message)
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def maximum_draft3_draft4(validator, maximum, instance, schema):
|
| 155 |
+
if not validator.is_type(instance, "number"):
|
| 156 |
+
return
|
| 157 |
+
|
| 158 |
+
if schema.get("exclusiveMaximum", False):
|
| 159 |
+
failed = instance >= maximum
|
| 160 |
+
cmp = "greater than or equal to"
|
| 161 |
+
else:
|
| 162 |
+
failed = instance > maximum
|
| 163 |
+
cmp = "greater than"
|
| 164 |
+
|
| 165 |
+
if failed:
|
| 166 |
+
message = f"{instance!r} is {cmp} the maximum of {maximum!r}"
|
| 167 |
+
yield ValidationError(message)
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def properties_draft3(validator, properties, instance, schema):
|
| 171 |
+
if not validator.is_type(instance, "object"):
|
| 172 |
+
return
|
| 173 |
+
|
| 174 |
+
for property, subschema in properties.items():
|
| 175 |
+
if property in instance:
|
| 176 |
+
yield from validator.descend(
|
| 177 |
+
instance[property],
|
| 178 |
+
subschema,
|
| 179 |
+
path=property,
|
| 180 |
+
schema_path=property,
|
| 181 |
+
)
|
| 182 |
+
elif subschema.get("required", False):
|
| 183 |
+
error = ValidationError(f"{property!r} is a required property")
|
| 184 |
+
error._set(
|
| 185 |
+
validator="required",
|
| 186 |
+
validator_value=subschema["required"],
|
| 187 |
+
instance=instance,
|
| 188 |
+
schema=schema,
|
| 189 |
+
)
|
| 190 |
+
error.path.appendleft(property)
|
| 191 |
+
error.schema_path.extend([property, "required"])
|
| 192 |
+
yield error
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def type_draft3(validator, types, instance, schema):
|
| 196 |
+
types = _utils.ensure_list(types)
|
| 197 |
+
|
| 198 |
+
all_errors = []
|
| 199 |
+
for index, type in enumerate(types):
|
| 200 |
+
if validator.is_type(type, "object"):
|
| 201 |
+
errors = list(validator.descend(instance, type, schema_path=index))
|
| 202 |
+
if not errors:
|
| 203 |
+
return
|
| 204 |
+
all_errors.extend(errors)
|
| 205 |
+
elif validator.is_type(instance, type):
|
| 206 |
+
return
|
| 207 |
+
|
| 208 |
+
reprs = []
|
| 209 |
+
for type in types:
|
| 210 |
+
try:
|
| 211 |
+
reprs.append(repr(type["name"]))
|
| 212 |
+
except Exception: # noqa: BLE001
|
| 213 |
+
reprs.append(repr(type))
|
| 214 |
+
yield ValidationError(
|
| 215 |
+
f"{instance!r} is not of type {', '.join(reprs)}",
|
| 216 |
+
context=all_errors,
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def contains_draft6_draft7(validator, contains, instance, schema):
|
| 221 |
+
if not validator.is_type(instance, "array"):
|
| 222 |
+
return
|
| 223 |
+
|
| 224 |
+
if not any(
|
| 225 |
+
validator.evolve(schema=contains).is_valid(element)
|
| 226 |
+
for element in instance
|
| 227 |
+
):
|
| 228 |
+
yield ValidationError(
|
| 229 |
+
f"None of {instance!r} are valid under the given schema",
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
def recursiveRef(validator, recursiveRef, instance, schema):
|
| 234 |
+
resolved = lookup_recursive_ref(validator._resolver)
|
| 235 |
+
yield from validator.descend(
|
| 236 |
+
instance,
|
| 237 |
+
resolved.contents,
|
| 238 |
+
resolver=resolved.resolver,
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
def find_evaluated_item_indexes_by_schema(validator, instance, schema):
|
| 243 |
+
"""
|
| 244 |
+
Get all indexes of items that get evaluated under the current schema.
|
| 245 |
+
|
| 246 |
+
Covers all keywords related to unevaluatedItems: items, prefixItems, if,
|
| 247 |
+
then, else, contains, unevaluatedItems, allOf, oneOf, anyOf
|
| 248 |
+
"""
|
| 249 |
+
if validator.is_type(schema, "boolean"):
|
| 250 |
+
return []
|
| 251 |
+
evaluated_indexes = []
|
| 252 |
+
|
| 253 |
+
ref = schema.get("$ref")
|
| 254 |
+
if ref is not None:
|
| 255 |
+
resolved = validator._resolver.lookup(ref)
|
| 256 |
+
evaluated_indexes.extend(
|
| 257 |
+
find_evaluated_item_indexes_by_schema(
|
| 258 |
+
validator.evolve(
|
| 259 |
+
schema=resolved.contents,
|
| 260 |
+
_resolver=resolved.resolver,
|
| 261 |
+
),
|
| 262 |
+
instance,
|
| 263 |
+
resolved.contents,
|
| 264 |
+
),
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
if "$recursiveRef" in schema:
|
| 268 |
+
resolved = lookup_recursive_ref(validator._resolver)
|
| 269 |
+
evaluated_indexes.extend(
|
| 270 |
+
find_evaluated_item_indexes_by_schema(
|
| 271 |
+
validator.evolve(
|
| 272 |
+
schema=resolved.contents,
|
| 273 |
+
_resolver=resolved.resolver,
|
| 274 |
+
),
|
| 275 |
+
instance,
|
| 276 |
+
resolved.contents,
|
| 277 |
+
),
|
| 278 |
+
)
|
| 279 |
+
|
| 280 |
+
if "items" in schema:
|
| 281 |
+
if "additionalItems" in schema:
|
| 282 |
+
return list(range(len(instance)))
|
| 283 |
+
|
| 284 |
+
if validator.is_type(schema["items"], "object"):
|
| 285 |
+
return list(range(len(instance)))
|
| 286 |
+
evaluated_indexes += list(range(len(schema["items"])))
|
| 287 |
+
|
| 288 |
+
if "if" in schema:
|
| 289 |
+
if validator.evolve(schema=schema["if"]).is_valid(instance):
|
| 290 |
+
evaluated_indexes += find_evaluated_item_indexes_by_schema(
|
| 291 |
+
validator, instance, schema["if"],
|
| 292 |
+
)
|
| 293 |
+
if "then" in schema:
|
| 294 |
+
evaluated_indexes += find_evaluated_item_indexes_by_schema(
|
| 295 |
+
validator, instance, schema["then"],
|
| 296 |
+
)
|
| 297 |
+
elif "else" in schema:
|
| 298 |
+
evaluated_indexes += find_evaluated_item_indexes_by_schema(
|
| 299 |
+
validator, instance, schema["else"],
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
+
for keyword in ["contains", "unevaluatedItems"]:
|
| 303 |
+
if keyword in schema:
|
| 304 |
+
for k, v in enumerate(instance):
|
| 305 |
+
if validator.evolve(schema=schema[keyword]).is_valid(v):
|
| 306 |
+
evaluated_indexes.append(k)
|
| 307 |
+
|
| 308 |
+
for keyword in ["allOf", "oneOf", "anyOf"]:
|
| 309 |
+
if keyword in schema:
|
| 310 |
+
for subschema in schema[keyword]:
|
| 311 |
+
errs = next(validator.descend(instance, subschema), None)
|
| 312 |
+
if errs is None:
|
| 313 |
+
evaluated_indexes += find_evaluated_item_indexes_by_schema(
|
| 314 |
+
validator, instance, subschema,
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
return evaluated_indexes
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
def unevaluatedItems_draft2019(validator, unevaluatedItems, instance, schema):
|
| 321 |
+
if not validator.is_type(instance, "array"):
|
| 322 |
+
return
|
| 323 |
+
evaluated_item_indexes = find_evaluated_item_indexes_by_schema(
|
| 324 |
+
validator, instance, schema,
|
| 325 |
+
)
|
| 326 |
+
unevaluated_items = [
|
| 327 |
+
item for index, item in enumerate(instance)
|
| 328 |
+
if index not in evaluated_item_indexes
|
| 329 |
+
]
|
| 330 |
+
if unevaluated_items:
|
| 331 |
+
error = "Unevaluated items are not allowed (%s %s unexpected)"
|
| 332 |
+
yield ValidationError(error % _utils.extras_msg(unevaluated_items))
|
| 333 |
+
|
| 334 |
+
|
| 335 |
+
def find_evaluated_property_keys_by_schema(validator, instance, schema):
|
| 336 |
+
if validator.is_type(schema, "boolean"):
|
| 337 |
+
return []
|
| 338 |
+
evaluated_keys = []
|
| 339 |
+
|
| 340 |
+
ref = schema.get("$ref")
|
| 341 |
+
if ref is not None:
|
| 342 |
+
resolved = validator._resolver.lookup(ref)
|
| 343 |
+
evaluated_keys.extend(
|
| 344 |
+
find_evaluated_property_keys_by_schema(
|
| 345 |
+
validator.evolve(
|
| 346 |
+
schema=resolved.contents,
|
| 347 |
+
_resolver=resolved.resolver,
|
| 348 |
+
),
|
| 349 |
+
instance,
|
| 350 |
+
resolved.contents,
|
| 351 |
+
),
|
| 352 |
+
)
|
| 353 |
+
|
| 354 |
+
if "$recursiveRef" in schema:
|
| 355 |
+
resolved = lookup_recursive_ref(validator._resolver)
|
| 356 |
+
evaluated_keys.extend(
|
| 357 |
+
find_evaluated_property_keys_by_schema(
|
| 358 |
+
validator.evolve(
|
| 359 |
+
schema=resolved.contents,
|
| 360 |
+
_resolver=resolved.resolver,
|
| 361 |
+
),
|
| 362 |
+
instance,
|
| 363 |
+
resolved.contents,
|
| 364 |
+
),
|
| 365 |
+
)
|
| 366 |
+
|
| 367 |
+
for keyword in [
|
| 368 |
+
"properties", "additionalProperties", "unevaluatedProperties",
|
| 369 |
+
]:
|
| 370 |
+
if keyword in schema:
|
| 371 |
+
schema_value = schema[keyword]
|
| 372 |
+
if validator.is_type(schema_value, "boolean") and schema_value:
|
| 373 |
+
evaluated_keys += instance.keys()
|
| 374 |
+
|
| 375 |
+
elif validator.is_type(schema_value, "object"):
|
| 376 |
+
for property in schema_value:
|
| 377 |
+
if property in instance:
|
| 378 |
+
evaluated_keys.append(property)
|
| 379 |
+
|
| 380 |
+
if "patternProperties" in schema:
|
| 381 |
+
for property in instance:
|
| 382 |
+
for pattern in schema["patternProperties"]:
|
| 383 |
+
if re.search(pattern, property):
|
| 384 |
+
evaluated_keys.append(property)
|
| 385 |
+
|
| 386 |
+
if "dependentSchemas" in schema:
|
| 387 |
+
for property, subschema in schema["dependentSchemas"].items():
|
| 388 |
+
if property not in instance:
|
| 389 |
+
continue
|
| 390 |
+
evaluated_keys += find_evaluated_property_keys_by_schema(
|
| 391 |
+
validator, instance, subschema,
|
| 392 |
+
)
|
| 393 |
+
|
| 394 |
+
for keyword in ["allOf", "oneOf", "anyOf"]:
|
| 395 |
+
if keyword in schema:
|
| 396 |
+
for subschema in schema[keyword]:
|
| 397 |
+
errs = next(validator.descend(instance, subschema), None)
|
| 398 |
+
if errs is None:
|
| 399 |
+
evaluated_keys += find_evaluated_property_keys_by_schema(
|
| 400 |
+
validator, instance, subschema,
|
| 401 |
+
)
|
| 402 |
+
|
| 403 |
+
if "if" in schema:
|
| 404 |
+
if validator.evolve(schema=schema["if"]).is_valid(instance):
|
| 405 |
+
evaluated_keys += find_evaluated_property_keys_by_schema(
|
| 406 |
+
validator, instance, schema["if"],
|
| 407 |
+
)
|
| 408 |
+
if "then" in schema:
|
| 409 |
+
evaluated_keys += find_evaluated_property_keys_by_schema(
|
| 410 |
+
validator, instance, schema["then"],
|
| 411 |
+
)
|
| 412 |
+
elif "else" in schema:
|
| 413 |
+
evaluated_keys += find_evaluated_property_keys_by_schema(
|
| 414 |
+
validator, instance, schema["else"],
|
| 415 |
+
)
|
| 416 |
+
|
| 417 |
+
return evaluated_keys
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
def unevaluatedProperties_draft2019(validator, uP, instance, schema):
|
| 421 |
+
if not validator.is_type(instance, "object"):
|
| 422 |
+
return
|
| 423 |
+
evaluated_keys = find_evaluated_property_keys_by_schema(
|
| 424 |
+
validator, instance, schema,
|
| 425 |
+
)
|
| 426 |
+
unevaluated_keys = []
|
| 427 |
+
for property in instance:
|
| 428 |
+
if property not in evaluated_keys:
|
| 429 |
+
for _ in validator.descend(
|
| 430 |
+
instance[property],
|
| 431 |
+
uP,
|
| 432 |
+
path=property,
|
| 433 |
+
schema_path=property,
|
| 434 |
+
):
|
| 435 |
+
# FIXME: Include context for each unevaluated property
|
| 436 |
+
# indicating why it's invalid under the subschema.
|
| 437 |
+
unevaluated_keys.append(property) # noqa: PERF401
|
| 438 |
+
|
| 439 |
+
if unevaluated_keys:
|
| 440 |
+
if uP is False:
|
| 441 |
+
error = "Unevaluated properties are not allowed (%s %s unexpected)"
|
| 442 |
+
extras = sorted(unevaluated_keys, key=str)
|
| 443 |
+
yield ValidationError(error % _utils.extras_msg(extras))
|
| 444 |
+
else:
|
| 445 |
+
error = (
|
| 446 |
+
"Unevaluated properties are not valid under "
|
| 447 |
+
"the given schema (%s %s unevaluated and invalid)"
|
| 448 |
+
)
|
| 449 |
+
yield ValidationError(error % _utils.extras_msg(unevaluated_keys))
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/_types.py
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import Any, Callable, Mapping
|
| 4 |
+
import numbers
|
| 5 |
+
|
| 6 |
+
from attrs import evolve, field, frozen
|
| 7 |
+
from rpds import HashTrieMap
|
| 8 |
+
|
| 9 |
+
from jsonschema.exceptions import UndefinedTypeCheck
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# unfortunately, the type of HashTrieMap is generic, and if used as an attrs
|
| 13 |
+
# converter, the generic type is presented to mypy, which then fails to match
|
| 14 |
+
# the concrete type of a type checker mapping
|
| 15 |
+
# this "do nothing" wrapper presents the correct information to mypy
|
| 16 |
+
def _typed_map_converter(
|
| 17 |
+
init_val: Mapping[str, Callable[[TypeChecker, Any], bool]],
|
| 18 |
+
) -> HashTrieMap[str, Callable[[TypeChecker, Any], bool]]:
|
| 19 |
+
return HashTrieMap.convert(init_val)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def is_array(checker, instance):
|
| 23 |
+
return isinstance(instance, list)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def is_bool(checker, instance):
|
| 27 |
+
return isinstance(instance, bool)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def is_integer(checker, instance):
|
| 31 |
+
# bool inherits from int, so ensure bools aren't reported as ints
|
| 32 |
+
if isinstance(instance, bool):
|
| 33 |
+
return False
|
| 34 |
+
return isinstance(instance, int)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def is_null(checker, instance):
|
| 38 |
+
return instance is None
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def is_number(checker, instance):
|
| 42 |
+
# bool inherits from int, so ensure bools aren't reported as ints
|
| 43 |
+
if isinstance(instance, bool):
|
| 44 |
+
return False
|
| 45 |
+
return isinstance(instance, numbers.Number)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def is_object(checker, instance):
|
| 49 |
+
return isinstance(instance, dict)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def is_string(checker, instance):
|
| 53 |
+
return isinstance(instance, str)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def is_any(checker, instance):
|
| 57 |
+
return True
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
@frozen(repr=False)
|
| 61 |
+
class TypeChecker:
|
| 62 |
+
"""
|
| 63 |
+
A :kw:`type` property checker.
|
| 64 |
+
|
| 65 |
+
A `TypeChecker` performs type checking for a `Validator`, converting
|
| 66 |
+
between the defined JSON Schema types and some associated Python types or
|
| 67 |
+
objects.
|
| 68 |
+
|
| 69 |
+
Modifying the behavior just mentioned by redefining which Python objects
|
| 70 |
+
are considered to be of which JSON Schema types can be done using
|
| 71 |
+
`TypeChecker.redefine` or `TypeChecker.redefine_many`, and types can be
|
| 72 |
+
removed via `TypeChecker.remove`. Each of these return a new `TypeChecker`.
|
| 73 |
+
|
| 74 |
+
Arguments:
|
| 75 |
+
|
| 76 |
+
type_checkers:
|
| 77 |
+
|
| 78 |
+
The initial mapping of types to their checking functions.
|
| 79 |
+
|
| 80 |
+
"""
|
| 81 |
+
|
| 82 |
+
_type_checkers: HashTrieMap[
|
| 83 |
+
str, Callable[[TypeChecker, Any], bool],
|
| 84 |
+
] = field(default=HashTrieMap(), converter=_typed_map_converter)
|
| 85 |
+
|
| 86 |
+
def __repr__(self):
|
| 87 |
+
types = ", ".join(repr(k) for k in sorted(self._type_checkers))
|
| 88 |
+
return f"<{self.__class__.__name__} types={{{types}}}>"
|
| 89 |
+
|
| 90 |
+
def is_type(self, instance, type: str) -> bool:
|
| 91 |
+
"""
|
| 92 |
+
Check if the instance is of the appropriate type.
|
| 93 |
+
|
| 94 |
+
Arguments:
|
| 95 |
+
|
| 96 |
+
instance:
|
| 97 |
+
|
| 98 |
+
The instance to check
|
| 99 |
+
|
| 100 |
+
type:
|
| 101 |
+
|
| 102 |
+
The name of the type that is expected.
|
| 103 |
+
|
| 104 |
+
Raises:
|
| 105 |
+
|
| 106 |
+
`jsonschema.exceptions.UndefinedTypeCheck`:
|
| 107 |
+
|
| 108 |
+
if ``type`` is unknown to this object.
|
| 109 |
+
|
| 110 |
+
"""
|
| 111 |
+
try:
|
| 112 |
+
fn = self._type_checkers[type]
|
| 113 |
+
except KeyError:
|
| 114 |
+
raise UndefinedTypeCheck(type) from None
|
| 115 |
+
|
| 116 |
+
return fn(self, instance)
|
| 117 |
+
|
| 118 |
+
def redefine(self, type: str, fn) -> TypeChecker:
|
| 119 |
+
"""
|
| 120 |
+
Produce a new checker with the given type redefined.
|
| 121 |
+
|
| 122 |
+
Arguments:
|
| 123 |
+
|
| 124 |
+
type:
|
| 125 |
+
|
| 126 |
+
The name of the type to check.
|
| 127 |
+
|
| 128 |
+
fn (collections.abc.Callable):
|
| 129 |
+
|
| 130 |
+
A callable taking exactly two parameters - the type
|
| 131 |
+
checker calling the function and the instance to check.
|
| 132 |
+
The function should return true if instance is of this
|
| 133 |
+
type and false otherwise.
|
| 134 |
+
|
| 135 |
+
"""
|
| 136 |
+
return self.redefine_many({type: fn})
|
| 137 |
+
|
| 138 |
+
def redefine_many(self, definitions=()) -> TypeChecker:
|
| 139 |
+
"""
|
| 140 |
+
Produce a new checker with the given types redefined.
|
| 141 |
+
|
| 142 |
+
Arguments:
|
| 143 |
+
|
| 144 |
+
definitions (dict):
|
| 145 |
+
|
| 146 |
+
A dictionary mapping types to their checking functions.
|
| 147 |
+
|
| 148 |
+
"""
|
| 149 |
+
type_checkers = self._type_checkers.update(definitions)
|
| 150 |
+
return evolve(self, type_checkers=type_checkers)
|
| 151 |
+
|
| 152 |
+
def remove(self, *types) -> TypeChecker:
|
| 153 |
+
"""
|
| 154 |
+
Produce a new checker with the given types forgotten.
|
| 155 |
+
|
| 156 |
+
Arguments:
|
| 157 |
+
|
| 158 |
+
types:
|
| 159 |
+
|
| 160 |
+
the names of the types to remove.
|
| 161 |
+
|
| 162 |
+
Raises:
|
| 163 |
+
|
| 164 |
+
`jsonschema.exceptions.UndefinedTypeCheck`:
|
| 165 |
+
|
| 166 |
+
if any given type is unknown to this object
|
| 167 |
+
|
| 168 |
+
"""
|
| 169 |
+
type_checkers = self._type_checkers
|
| 170 |
+
for each in types:
|
| 171 |
+
try:
|
| 172 |
+
type_checkers = type_checkers.remove(each)
|
| 173 |
+
except KeyError:
|
| 174 |
+
raise UndefinedTypeCheck(each) from None
|
| 175 |
+
return evolve(self, type_checkers=type_checkers)
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
draft3_type_checker = TypeChecker(
|
| 179 |
+
{
|
| 180 |
+
"any": is_any,
|
| 181 |
+
"array": is_array,
|
| 182 |
+
"boolean": is_bool,
|
| 183 |
+
"integer": is_integer,
|
| 184 |
+
"object": is_object,
|
| 185 |
+
"null": is_null,
|
| 186 |
+
"number": is_number,
|
| 187 |
+
"string": is_string,
|
| 188 |
+
},
|
| 189 |
+
)
|
| 190 |
+
draft4_type_checker = draft3_type_checker.remove("any")
|
| 191 |
+
draft6_type_checker = draft4_type_checker.redefine(
|
| 192 |
+
"integer",
|
| 193 |
+
lambda checker, instance: (
|
| 194 |
+
is_integer(checker, instance)
|
| 195 |
+
or isinstance(instance, float) and instance.is_integer()
|
| 196 |
+
),
|
| 197 |
+
)
|
| 198 |
+
draft7_type_checker = draft6_type_checker
|
| 199 |
+
draft201909_type_checker = draft7_type_checker
|
| 200 |
+
draft202012_type_checker = draft201909_type_checker
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/_typing.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Some (initially private) typing helpers for jsonschema's types.
|
| 3 |
+
"""
|
| 4 |
+
from typing import Any, Callable, Iterable, Protocol, Tuple, Union
|
| 5 |
+
|
| 6 |
+
import referencing.jsonschema
|
| 7 |
+
|
| 8 |
+
from jsonschema.protocols import Validator
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class SchemaKeywordValidator(Protocol):
|
| 12 |
+
def __call__(
|
| 13 |
+
self,
|
| 14 |
+
validator: Validator,
|
| 15 |
+
value: Any,
|
| 16 |
+
instance: Any,
|
| 17 |
+
schema: referencing.jsonschema.Schema,
|
| 18 |
+
) -> None:
|
| 19 |
+
...
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
id_of = Callable[[referencing.jsonschema.Schema], Union[str, None]]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
ApplicableValidators = Callable[
|
| 26 |
+
[referencing.jsonschema.Schema],
|
| 27 |
+
Iterable[Tuple[str, Any]],
|
| 28 |
+
]
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/_utils.py
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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utableMapping, Sequence
|
| 2 |
+
from urllib.parse import urlsplit
|
| 3 |
+
import itertools
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class URIDict(MutableMapping):
|
| 8 |
+
"""
|
| 9 |
+
Dictionary which uses normalized URIs as keys.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
def normalize(self, uri):
|
| 13 |
+
return urlsplit(uri).geturl()
|
| 14 |
+
|
| 15 |
+
def __init__(self, *args, **kwargs):
|
| 16 |
+
self.store = dict()
|
| 17 |
+
self.store.update(*args, **kwargs)
|
| 18 |
+
|
| 19 |
+
def __getitem__(self, uri):
|
| 20 |
+
return self.store[self.normalize(uri)]
|
| 21 |
+
|
| 22 |
+
def __setitem__(self, uri, value):
|
| 23 |
+
self.store[self.normalize(uri)] = value
|
| 24 |
+
|
| 25 |
+
def __delitem__(self, uri):
|
| 26 |
+
del self.store[self.normalize(uri)]
|
| 27 |
+
|
| 28 |
+
def __iter__(self):
|
| 29 |
+
return iter(self.store)
|
| 30 |
+
|
| 31 |
+
def __len__(self): # pragma: no cover -- untested, but to be removed
|
| 32 |
+
return len(self.store)
|
| 33 |
+
|
| 34 |
+
def __repr__(self): # pragma: no cover -- untested, but to be removed
|
| 35 |
+
return repr(self.store)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class Unset:
|
| 39 |
+
"""
|
| 40 |
+
An as-of-yet unset attribute or unprovided default parameter.
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
def __repr__(self): # pragma: no cover
|
| 44 |
+
return "<unset>"
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def format_as_index(container, indices):
|
| 48 |
+
"""
|
| 49 |
+
Construct a single string containing indexing operations for the indices.
|
| 50 |
+
|
| 51 |
+
For example for a container ``bar``, [1, 2, "foo"] -> bar[1][2]["foo"]
|
| 52 |
+
|
| 53 |
+
Arguments:
|
| 54 |
+
|
| 55 |
+
container (str):
|
| 56 |
+
|
| 57 |
+
A word to use for the thing being indexed
|
| 58 |
+
|
| 59 |
+
indices (sequence):
|
| 60 |
+
|
| 61 |
+
The indices to format.
|
| 62 |
+
|
| 63 |
+
"""
|
| 64 |
+
if not indices:
|
| 65 |
+
return container
|
| 66 |
+
return f"{container}[{']['.join(repr(index) for index in indices)}]"
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def find_additional_properties(instance, schema):
|
| 70 |
+
"""
|
| 71 |
+
Return the set of additional properties for the given ``instance``.
|
| 72 |
+
|
| 73 |
+
Weeds out properties that should have been validated by ``properties`` and
|
| 74 |
+
/ or ``patternProperties``.
|
| 75 |
+
|
| 76 |
+
Assumes ``instance`` is dict-like already.
|
| 77 |
+
"""
|
| 78 |
+
properties = schema.get("properties", {})
|
| 79 |
+
patterns = "|".join(schema.get("patternProperties", {}))
|
| 80 |
+
for property in instance:
|
| 81 |
+
if property not in properties:
|
| 82 |
+
if patterns and re.search(patterns, property):
|
| 83 |
+
continue
|
| 84 |
+
yield property
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def extras_msg(extras):
|
| 88 |
+
"""
|
| 89 |
+
Create an error message for extra items or properties.
|
| 90 |
+
"""
|
| 91 |
+
verb = "was" if len(extras) == 1 else "were"
|
| 92 |
+
return ", ".join(repr(extra) for extra in extras), verb
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def ensure_list(thing):
|
| 96 |
+
"""
|
| 97 |
+
Wrap ``thing`` in a list if it's a single str.
|
| 98 |
+
|
| 99 |
+
Otherwise, return it unchanged.
|
| 100 |
+
"""
|
| 101 |
+
if isinstance(thing, str):
|
| 102 |
+
return [thing]
|
| 103 |
+
return thing
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def _mapping_equal(one, two):
|
| 107 |
+
"""
|
| 108 |
+
Check if two mappings are equal using the semantics of `equal`.
|
| 109 |
+
"""
|
| 110 |
+
if len(one) != len(two):
|
| 111 |
+
return False
|
| 112 |
+
return all(
|
| 113 |
+
key in two and equal(value, two[key])
|
| 114 |
+
for key, value in one.items()
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def _sequence_equal(one, two):
|
| 119 |
+
"""
|
| 120 |
+
Check if two sequences are equal using the semantics of `equal`.
|
| 121 |
+
"""
|
| 122 |
+
if len(one) != len(two):
|
| 123 |
+
return False
|
| 124 |
+
return all(equal(i, j) for i, j in zip(one, two))
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def equal(one, two):
|
| 128 |
+
"""
|
| 129 |
+
Check if two things are equal evading some Python type hierarchy semantics.
|
| 130 |
+
|
| 131 |
+
Specifically in JSON Schema, evade `bool` inheriting from `int`,
|
| 132 |
+
recursing into sequences to do the same.
|
| 133 |
+
"""
|
| 134 |
+
if one is two:
|
| 135 |
+
return True
|
| 136 |
+
if isinstance(one, str) or isinstance(two, str):
|
| 137 |
+
return one == two
|
| 138 |
+
if isinstance(one, Sequence) and isinstance(two, Sequence):
|
| 139 |
+
return _sequence_equal(one, two)
|
| 140 |
+
if isinstance(one, Mapping) and isinstance(two, Mapping):
|
| 141 |
+
return _mapping_equal(one, two)
|
| 142 |
+
return unbool(one) == unbool(two)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def unbool(element, true=object(), false=object()):
|
| 146 |
+
"""
|
| 147 |
+
A hack to make True and 1 and False and 0 unique for ``uniq``.
|
| 148 |
+
"""
|
| 149 |
+
if element is True:
|
| 150 |
+
return true
|
| 151 |
+
elif element is False:
|
| 152 |
+
return false
|
| 153 |
+
return element
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def uniq(container):
|
| 157 |
+
"""
|
| 158 |
+
Check if all of a container's elements are unique.
|
| 159 |
+
|
| 160 |
+
Tries to rely on the container being recursively sortable, or otherwise
|
| 161 |
+
falls back on (slow) brute force.
|
| 162 |
+
"""
|
| 163 |
+
try:
|
| 164 |
+
sort = sorted(unbool(i) for i in container)
|
| 165 |
+
sliced = itertools.islice(sort, 1, None)
|
| 166 |
+
|
| 167 |
+
for i, j in zip(sort, sliced):
|
| 168 |
+
if equal(i, j):
|
| 169 |
+
return False
|
| 170 |
+
|
| 171 |
+
except (NotImplementedError, TypeError):
|
| 172 |
+
seen = []
|
| 173 |
+
for e in container:
|
| 174 |
+
e = unbool(e)
|
| 175 |
+
|
| 176 |
+
for i in seen:
|
| 177 |
+
if equal(i, e):
|
| 178 |
+
return False
|
| 179 |
+
|
| 180 |
+
seen.append(e)
|
| 181 |
+
return True
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
def find_evaluated_item_indexes_by_schema(validator, instance, schema):
|
| 185 |
+
"""
|
| 186 |
+
Get all indexes of items that get evaluated under the current schema.
|
| 187 |
+
|
| 188 |
+
Covers all keywords related to unevaluatedItems: items, prefixItems, if,
|
| 189 |
+
then, else, contains, unevaluatedItems, allOf, oneOf, anyOf
|
| 190 |
+
"""
|
| 191 |
+
if validator.is_type(schema, "boolean"):
|
| 192 |
+
return []
|
| 193 |
+
evaluated_indexes = []
|
| 194 |
+
|
| 195 |
+
if "items" in schema:
|
| 196 |
+
return list(range(len(instance)))
|
| 197 |
+
|
| 198 |
+
ref = schema.get("$ref")
|
| 199 |
+
if ref is not None:
|
| 200 |
+
resolved = validator._resolver.lookup(ref)
|
| 201 |
+
evaluated_indexes.extend(
|
| 202 |
+
find_evaluated_item_indexes_by_schema(
|
| 203 |
+
validator.evolve(
|
| 204 |
+
schema=resolved.contents,
|
| 205 |
+
_resolver=resolved.resolver,
|
| 206 |
+
),
|
| 207 |
+
instance,
|
| 208 |
+
resolved.contents,
|
| 209 |
+
),
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
dynamicRef = schema.get("$dynamicRef")
|
| 213 |
+
if dynamicRef is not None:
|
| 214 |
+
resolved = validator._resolver.lookup(dynamicRef)
|
| 215 |
+
evaluated_indexes.extend(
|
| 216 |
+
find_evaluated_item_indexes_by_schema(
|
| 217 |
+
validator.evolve(
|
| 218 |
+
schema=resolved.contents,
|
| 219 |
+
_resolver=resolved.resolver,
|
| 220 |
+
),
|
| 221 |
+
instance,
|
| 222 |
+
resolved.contents,
|
| 223 |
+
),
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
if "prefixItems" in schema:
|
| 227 |
+
evaluated_indexes += list(range(len(schema["prefixItems"])))
|
| 228 |
+
|
| 229 |
+
if "if" in schema:
|
| 230 |
+
if validator.evolve(schema=schema["if"]).is_valid(instance):
|
| 231 |
+
evaluated_indexes += find_evaluated_item_indexes_by_schema(
|
| 232 |
+
validator, instance, schema["if"],
|
| 233 |
+
)
|
| 234 |
+
if "then" in schema:
|
| 235 |
+
evaluated_indexes += find_evaluated_item_indexes_by_schema(
|
| 236 |
+
validator, instance, schema["then"],
|
| 237 |
+
)
|
| 238 |
+
elif "else" in schema:
|
| 239 |
+
evaluated_indexes += find_evaluated_item_indexes_by_schema(
|
| 240 |
+
validator, instance, schema["else"],
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
for keyword in ["contains", "unevaluatedItems"]:
|
| 244 |
+
if keyword in schema:
|
| 245 |
+
for k, v in enumerate(instance):
|
| 246 |
+
if validator.evolve(schema=schema[keyword]).is_valid(v):
|
| 247 |
+
evaluated_indexes.append(k)
|
| 248 |
+
|
| 249 |
+
for keyword in ["allOf", "oneOf", "anyOf"]:
|
| 250 |
+
if keyword in schema:
|
| 251 |
+
for subschema in schema[keyword]:
|
| 252 |
+
errs = next(validator.descend(instance, subschema), None)
|
| 253 |
+
if errs is None:
|
| 254 |
+
evaluated_indexes += find_evaluated_item_indexes_by_schema(
|
| 255 |
+
validator, instance, subschema,
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
return evaluated_indexes
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
def find_evaluated_property_keys_by_schema(validator, instance, schema):
|
| 262 |
+
"""
|
| 263 |
+
Get all keys of items that get evaluated under the current schema.
|
| 264 |
+
|
| 265 |
+
Covers all keywords related to unevaluatedProperties: properties,
|
| 266 |
+
additionalProperties, unevaluatedProperties, patternProperties,
|
| 267 |
+
dependentSchemas, allOf, oneOf, anyOf, if, then, else
|
| 268 |
+
"""
|
| 269 |
+
if validator.is_type(schema, "boolean"):
|
| 270 |
+
return []
|
| 271 |
+
evaluated_keys = []
|
| 272 |
+
|
| 273 |
+
ref = schema.get("$ref")
|
| 274 |
+
if ref is not None:
|
| 275 |
+
resolved = validator._resolver.lookup(ref)
|
| 276 |
+
evaluated_keys.extend(
|
| 277 |
+
find_evaluated_property_keys_by_schema(
|
| 278 |
+
validator.evolve(
|
| 279 |
+
schema=resolved.contents,
|
| 280 |
+
_resolver=resolved.resolver,
|
| 281 |
+
),
|
| 282 |
+
instance,
|
| 283 |
+
resolved.contents,
|
| 284 |
+
),
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
dynamicRef = schema.get("$dynamicRef")
|
| 288 |
+
if dynamicRef is not None:
|
| 289 |
+
resolved = validator._resolver.lookup(dynamicRef)
|
| 290 |
+
evaluated_keys.extend(
|
| 291 |
+
find_evaluated_property_keys_by_schema(
|
| 292 |
+
validator.evolve(
|
| 293 |
+
schema=resolved.contents,
|
| 294 |
+
_resolver=resolved.resolver,
|
| 295 |
+
),
|
| 296 |
+
instance,
|
| 297 |
+
resolved.contents,
|
| 298 |
+
),
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
for keyword in [
|
| 302 |
+
"properties", "additionalProperties", "unevaluatedProperties",
|
| 303 |
+
]:
|
| 304 |
+
if keyword in schema:
|
| 305 |
+
schema_value = schema[keyword]
|
| 306 |
+
if validator.is_type(schema_value, "boolean") and schema_value:
|
| 307 |
+
evaluated_keys += instance.keys()
|
| 308 |
+
|
| 309 |
+
elif validator.is_type(schema_value, "object"):
|
| 310 |
+
for property in schema_value:
|
| 311 |
+
if property in instance:
|
| 312 |
+
evaluated_keys.append(property)
|
| 313 |
+
|
| 314 |
+
if "patternProperties" in schema:
|
| 315 |
+
for property in instance:
|
| 316 |
+
for pattern in schema["patternProperties"]:
|
| 317 |
+
if re.search(pattern, property):
|
| 318 |
+
evaluated_keys.append(property)
|
| 319 |
+
|
| 320 |
+
if "dependentSchemas" in schema:
|
| 321 |
+
for property, subschema in schema["dependentSchemas"].items():
|
| 322 |
+
if property not in instance:
|
| 323 |
+
continue
|
| 324 |
+
evaluated_keys += find_evaluated_property_keys_by_schema(
|
| 325 |
+
validator, instance, subschema,
|
| 326 |
+
)
|
| 327 |
+
|
| 328 |
+
for keyword in ["allOf", "oneOf", "anyOf"]:
|
| 329 |
+
if keyword in schema:
|
| 330 |
+
for subschema in schema[keyword]:
|
| 331 |
+
errs = next(validator.descend(instance, subschema), None)
|
| 332 |
+
if errs is None:
|
| 333 |
+
evaluated_keys += find_evaluated_property_keys_by_schema(
|
| 334 |
+
validator, instance, subschema,
|
| 335 |
+
)
|
| 336 |
+
|
| 337 |
+
if "if" in schema:
|
| 338 |
+
if validator.evolve(schema=schema["if"]).is_valid(instance):
|
| 339 |
+
evaluated_keys += find_evaluated_property_keys_by_schema(
|
| 340 |
+
validator, instance, schema["if"],
|
| 341 |
+
)
|
| 342 |
+
if "then" in schema:
|
| 343 |
+
evaluated_keys += find_evaluated_property_keys_by_schema(
|
| 344 |
+
validator, instance, schema["then"],
|
| 345 |
+
)
|
| 346 |
+
elif "else" in schema:
|
| 347 |
+
evaluated_keys += find_evaluated_property_keys_by_schema(
|
| 348 |
+
validator, instance, schema["else"],
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
return evaluated_keys
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/__init__.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Benchmarks for validation.
|
| 3 |
+
|
| 4 |
+
This package is *not* public API.
|
| 5 |
+
"""
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/__pycache__/contains.cpython-310.pyc
ADDED
|
Binary file (1.23 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/__pycache__/nested_schemas.cpython-310.pyc
ADDED
|
Binary file (1.98 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/__pycache__/subcomponents.cpython-310.pyc
ADDED
|
Binary file (1.59 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/__pycache__/unused_registry.cpython-310.pyc
ADDED
|
Binary file (1.2 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/__pycache__/validator_creation.cpython-310.pyc
ADDED
|
Binary file (466 Bytes). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/contains.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
A benchmark for validation of the `contains` keyword.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from pyperf import Runner
|
| 6 |
+
|
| 7 |
+
from jsonschema import Draft202012Validator
|
| 8 |
+
|
| 9 |
+
schema = {
|
| 10 |
+
"type": "array",
|
| 11 |
+
"contains": {"const": 37},
|
| 12 |
+
}
|
| 13 |
+
validator = Draft202012Validator(schema)
|
| 14 |
+
|
| 15 |
+
size = 1000
|
| 16 |
+
beginning = [37] + [0] * (size - 1)
|
| 17 |
+
middle = [0] * (size // 2) + [37] + [0] * (size // 2)
|
| 18 |
+
end = [0] * (size - 1) + [37]
|
| 19 |
+
invalid = [0] * size
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
if __name__ == "__main__":
|
| 23 |
+
runner = Runner()
|
| 24 |
+
runner.bench_func("baseline", lambda: validator.is_valid([]))
|
| 25 |
+
runner.bench_func("beginning", lambda: validator.is_valid(beginning))
|
| 26 |
+
runner.bench_func("middle", lambda: validator.is_valid(middle))
|
| 27 |
+
runner.bench_func("end", lambda: validator.is_valid(end))
|
| 28 |
+
runner.bench_func("invalid", lambda: validator.is_valid(invalid))
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/issue232.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
A performance benchmark using the example from issue #232.
|
| 3 |
+
|
| 4 |
+
See https://github.com/python-jsonschema/jsonschema/pull/232.
|
| 5 |
+
"""
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
from pyperf import Runner
|
| 9 |
+
from referencing import Registry
|
| 10 |
+
|
| 11 |
+
from jsonschema.tests._suite import Version
|
| 12 |
+
import jsonschema
|
| 13 |
+
|
| 14 |
+
issue232 = Version(
|
| 15 |
+
path=Path(__file__).parent / "issue232",
|
| 16 |
+
remotes=Registry(),
|
| 17 |
+
name="issue232",
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
if __name__ == "__main__":
|
| 22 |
+
issue232.benchmark(
|
| 23 |
+
runner=Runner(),
|
| 24 |
+
Validator=jsonschema.Draft4Validator,
|
| 25 |
+
)
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/issue232/issue.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/json_schema_test_suite.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
A performance benchmark using the official test suite.
|
| 3 |
+
|
| 4 |
+
This benchmarks jsonschema using every valid example in the
|
| 5 |
+
JSON-Schema-Test-Suite. It will take some time to complete.
|
| 6 |
+
"""
|
| 7 |
+
from pyperf import Runner
|
| 8 |
+
|
| 9 |
+
from jsonschema.tests._suite import Suite
|
| 10 |
+
|
| 11 |
+
if __name__ == "__main__":
|
| 12 |
+
Suite().benchmark(runner=Runner())
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/subcomponents.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
A benchmark which tries to compare the possible slow subparts of validation.
|
| 3 |
+
"""
|
| 4 |
+
from referencing import Registry
|
| 5 |
+
from referencing.jsonschema import DRAFT202012
|
| 6 |
+
from rpds import HashTrieMap, HashTrieSet
|
| 7 |
+
|
| 8 |
+
from jsonschema import Draft202012Validator
|
| 9 |
+
|
| 10 |
+
schema = {
|
| 11 |
+
"type": "array",
|
| 12 |
+
"minLength": 1,
|
| 13 |
+
"maxLength": 1,
|
| 14 |
+
"items": {"type": "integer"},
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
hmap = HashTrieMap()
|
| 18 |
+
hset = HashTrieSet()
|
| 19 |
+
|
| 20 |
+
registry = Registry()
|
| 21 |
+
|
| 22 |
+
v = Draft202012Validator(schema)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def registry_data_structures():
|
| 26 |
+
return hmap.insert("foo", "bar"), hset.insert("foo")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def registry_add():
|
| 30 |
+
resource = DRAFT202012.create_resource(schema)
|
| 31 |
+
return registry.with_resource(uri="urn:example", resource=resource)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
from pyperf import Runner
|
| 36 |
+
runner = Runner()
|
| 37 |
+
|
| 38 |
+
runner.bench_func("HashMap/HashSet insertion", registry_data_structures)
|
| 39 |
+
runner.bench_func("Registry insertion", registry_add)
|
| 40 |
+
runner.bench_func("Success", lambda: v.is_valid([1]))
|
| 41 |
+
runner.bench_func("Failure", lambda: v.is_valid(["foo"]))
|
| 42 |
+
runner.bench_func("Metaschema validation", lambda: v.check_schema(schema))
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/useless_keywords.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
A benchmark for validation of schemas containing lots of useless keywords.
|
| 3 |
+
|
| 4 |
+
Checks we filter them out once, ahead of time.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from pyperf import Runner
|
| 8 |
+
|
| 9 |
+
from jsonschema import Draft202012Validator
|
| 10 |
+
|
| 11 |
+
NUM_USELESS = 100000
|
| 12 |
+
schema = dict(
|
| 13 |
+
[
|
| 14 |
+
("not", {"const": 42}),
|
| 15 |
+
*((str(i), i) for i in range(NUM_USELESS)),
|
| 16 |
+
("type", "integer"),
|
| 17 |
+
*((str(i), i) for i in range(NUM_USELESS, NUM_USELESS)),
|
| 18 |
+
("minimum", 37),
|
| 19 |
+
],
|
| 20 |
+
)
|
| 21 |
+
validator = Draft202012Validator(schema)
|
| 22 |
+
|
| 23 |
+
valid = 3737
|
| 24 |
+
invalid = 12
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
runner = Runner()
|
| 29 |
+
runner.bench_func("beginning of schema", lambda: validator.is_valid(42))
|
| 30 |
+
runner.bench_func("middle of schema", lambda: validator.is_valid("foo"))
|
| 31 |
+
runner.bench_func("end of schema", lambda: validator.is_valid(12))
|
| 32 |
+
runner.bench_func("valid", lambda: validator.is_valid(3737))
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/benchmarks/validator_creation.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pyperf import Runner
|
| 2 |
+
|
| 3 |
+
from jsonschema import Draft202012Validator
|
| 4 |
+
|
| 5 |
+
schema = {
|
| 6 |
+
"type": "array",
|
| 7 |
+
"minLength": 1,
|
| 8 |
+
"maxLength": 1,
|
| 9 |
+
"items": {"type": "integer"},
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
if __name__ == "__main__":
|
| 14 |
+
Runner().bench_func("validator creation", Draft202012Validator, schema)
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/cli.py
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
The ``jsonschema`` command line.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from importlib import metadata
|
| 6 |
+
from json import JSONDecodeError
|
| 7 |
+
from textwrap import dedent
|
| 8 |
+
import argparse
|
| 9 |
+
import json
|
| 10 |
+
import sys
|
| 11 |
+
import traceback
|
| 12 |
+
import warnings
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
from pkgutil import resolve_name
|
| 16 |
+
except ImportError:
|
| 17 |
+
from pkgutil_resolve_name import resolve_name # type: ignore[no-redef]
|
| 18 |
+
|
| 19 |
+
from attrs import define, field
|
| 20 |
+
|
| 21 |
+
from jsonschema.exceptions import SchemaError
|
| 22 |
+
from jsonschema.validators import _RefResolver, validator_for
|
| 23 |
+
|
| 24 |
+
warnings.warn(
|
| 25 |
+
(
|
| 26 |
+
"The jsonschema CLI is deprecated and will be removed in a future "
|
| 27 |
+
"version. Please use check-jsonschema instead, which can be installed "
|
| 28 |
+
"from https://pypi.org/project/check-jsonschema/"
|
| 29 |
+
),
|
| 30 |
+
DeprecationWarning,
|
| 31 |
+
stacklevel=2,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class _CannotLoadFile(Exception):
|
| 36 |
+
pass
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@define
|
| 40 |
+
class _Outputter:
|
| 41 |
+
|
| 42 |
+
_formatter = field()
|
| 43 |
+
_stdout = field()
|
| 44 |
+
_stderr = field()
|
| 45 |
+
|
| 46 |
+
@classmethod
|
| 47 |
+
def from_arguments(cls, arguments, stdout, stderr):
|
| 48 |
+
if arguments["output"] == "plain":
|
| 49 |
+
formatter = _PlainFormatter(arguments["error_format"])
|
| 50 |
+
elif arguments["output"] == "pretty":
|
| 51 |
+
formatter = _PrettyFormatter()
|
| 52 |
+
return cls(formatter=formatter, stdout=stdout, stderr=stderr)
|
| 53 |
+
|
| 54 |
+
def load(self, path):
|
| 55 |
+
try:
|
| 56 |
+
file = open(path) # noqa: SIM115, PTH123
|
| 57 |
+
except FileNotFoundError as error:
|
| 58 |
+
self.filenotfound_error(path=path, exc_info=sys.exc_info())
|
| 59 |
+
raise _CannotLoadFile() from error
|
| 60 |
+
|
| 61 |
+
with file:
|
| 62 |
+
try:
|
| 63 |
+
return json.load(file)
|
| 64 |
+
except JSONDecodeError as error:
|
| 65 |
+
self.parsing_error(path=path, exc_info=sys.exc_info())
|
| 66 |
+
raise _CannotLoadFile() from error
|
| 67 |
+
|
| 68 |
+
def filenotfound_error(self, **kwargs):
|
| 69 |
+
self._stderr.write(self._formatter.filenotfound_error(**kwargs))
|
| 70 |
+
|
| 71 |
+
def parsing_error(self, **kwargs):
|
| 72 |
+
self._stderr.write(self._formatter.parsing_error(**kwargs))
|
| 73 |
+
|
| 74 |
+
def validation_error(self, **kwargs):
|
| 75 |
+
self._stderr.write(self._formatter.validation_error(**kwargs))
|
| 76 |
+
|
| 77 |
+
def validation_success(self, **kwargs):
|
| 78 |
+
self._stdout.write(self._formatter.validation_success(**kwargs))
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@define
|
| 82 |
+
class _PrettyFormatter:
|
| 83 |
+
|
| 84 |
+
_ERROR_MSG = dedent(
|
| 85 |
+
"""\
|
| 86 |
+
===[{type}]===({path})===
|
| 87 |
+
|
| 88 |
+
{body}
|
| 89 |
+
-----------------------------
|
| 90 |
+
""",
|
| 91 |
+
)
|
| 92 |
+
_SUCCESS_MSG = "===[SUCCESS]===({path})===\n"
|
| 93 |
+
|
| 94 |
+
def filenotfound_error(self, path, exc_info):
|
| 95 |
+
return self._ERROR_MSG.format(
|
| 96 |
+
path=path,
|
| 97 |
+
type="FileNotFoundError",
|
| 98 |
+
body=f"{path!r} does not exist.",
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
def parsing_error(self, path, exc_info):
|
| 102 |
+
exc_type, exc_value, exc_traceback = exc_info
|
| 103 |
+
exc_lines = "".join(
|
| 104 |
+
traceback.format_exception(exc_type, exc_value, exc_traceback),
|
| 105 |
+
)
|
| 106 |
+
return self._ERROR_MSG.format(
|
| 107 |
+
path=path,
|
| 108 |
+
type=exc_type.__name__,
|
| 109 |
+
body=exc_lines,
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
def validation_error(self, instance_path, error):
|
| 113 |
+
return self._ERROR_MSG.format(
|
| 114 |
+
path=instance_path,
|
| 115 |
+
type=error.__class__.__name__,
|
| 116 |
+
body=error,
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
def validation_success(self, instance_path):
|
| 120 |
+
return self._SUCCESS_MSG.format(path=instance_path)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
@define
|
| 124 |
+
class _PlainFormatter:
|
| 125 |
+
|
| 126 |
+
_error_format = field()
|
| 127 |
+
|
| 128 |
+
def filenotfound_error(self, path, exc_info):
|
| 129 |
+
return f"{path!r} does not exist.\n"
|
| 130 |
+
|
| 131 |
+
def parsing_error(self, path, exc_info):
|
| 132 |
+
return "Failed to parse {}: {}\n".format(
|
| 133 |
+
"<stdin>" if path == "<stdin>" else repr(path),
|
| 134 |
+
exc_info[1],
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
def validation_error(self, instance_path, error):
|
| 138 |
+
return self._error_format.format(file_name=instance_path, error=error)
|
| 139 |
+
|
| 140 |
+
def validation_success(self, instance_path):
|
| 141 |
+
return ""
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def _resolve_name_with_default(name):
|
| 145 |
+
if "." not in name:
|
| 146 |
+
name = "jsonschema." + name
|
| 147 |
+
return resolve_name(name)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
parser = argparse.ArgumentParser(
|
| 151 |
+
description="JSON Schema Validation CLI",
|
| 152 |
+
)
|
| 153 |
+
parser.add_argument(
|
| 154 |
+
"-i", "--instance",
|
| 155 |
+
action="append",
|
| 156 |
+
dest="instances",
|
| 157 |
+
help="""
|
| 158 |
+
a path to a JSON instance (i.e. filename.json) to validate (may
|
| 159 |
+
be specified multiple times). If no instances are provided via this
|
| 160 |
+
option, one will be expected on standard input.
|
| 161 |
+
""",
|
| 162 |
+
)
|
| 163 |
+
parser.add_argument(
|
| 164 |
+
"-F", "--error-format",
|
| 165 |
+
help="""
|
| 166 |
+
the format to use for each validation error message, specified
|
| 167 |
+
in a form suitable for str.format. This string will be passed
|
| 168 |
+
one formatted object named 'error' for each ValidationError.
|
| 169 |
+
Only provide this option when using --output=plain, which is the
|
| 170 |
+
default. If this argument is unprovided and --output=plain is
|
| 171 |
+
used, a simple default representation will be used.
|
| 172 |
+
""",
|
| 173 |
+
)
|
| 174 |
+
parser.add_argument(
|
| 175 |
+
"-o", "--output",
|
| 176 |
+
choices=["plain", "pretty"],
|
| 177 |
+
default="plain",
|
| 178 |
+
help="""
|
| 179 |
+
an output format to use. 'plain' (default) will produce minimal
|
| 180 |
+
text with one line for each error, while 'pretty' will produce
|
| 181 |
+
more detailed human-readable output on multiple lines.
|
| 182 |
+
""",
|
| 183 |
+
)
|
| 184 |
+
parser.add_argument(
|
| 185 |
+
"-V", "--validator",
|
| 186 |
+
type=_resolve_name_with_default,
|
| 187 |
+
help="""
|
| 188 |
+
the fully qualified object name of a validator to use, or, for
|
| 189 |
+
validators that are registered with jsonschema, simply the name
|
| 190 |
+
of the class.
|
| 191 |
+
""",
|
| 192 |
+
)
|
| 193 |
+
parser.add_argument(
|
| 194 |
+
"--base-uri",
|
| 195 |
+
help="""
|
| 196 |
+
a base URI to assign to the provided schema, even if it does not
|
| 197 |
+
declare one (via e.g. $id). This option can be used if you wish to
|
| 198 |
+
resolve relative references to a particular URI (or local path)
|
| 199 |
+
""",
|
| 200 |
+
)
|
| 201 |
+
parser.add_argument(
|
| 202 |
+
"--version",
|
| 203 |
+
action="version",
|
| 204 |
+
version=metadata.version("jsonschema"),
|
| 205 |
+
)
|
| 206 |
+
parser.add_argument(
|
| 207 |
+
"schema",
|
| 208 |
+
help="the path to a JSON Schema to validate with (i.e. schema.json)",
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
def parse_args(args): # noqa: D103
|
| 213 |
+
arguments = vars(parser.parse_args(args=args or ["--help"]))
|
| 214 |
+
if arguments["output"] != "plain" and arguments["error_format"]:
|
| 215 |
+
raise parser.error(
|
| 216 |
+
"--error-format can only be used with --output plain",
|
| 217 |
+
)
|
| 218 |
+
if arguments["output"] == "plain" and arguments["error_format"] is None:
|
| 219 |
+
arguments["error_format"] = "{error.instance}: {error.message}\n"
|
| 220 |
+
return arguments
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
def _validate_instance(instance_path, instance, validator, outputter):
|
| 224 |
+
invalid = False
|
| 225 |
+
for error in validator.iter_errors(instance):
|
| 226 |
+
invalid = True
|
| 227 |
+
outputter.validation_error(instance_path=instance_path, error=error)
|
| 228 |
+
|
| 229 |
+
if not invalid:
|
| 230 |
+
outputter.validation_success(instance_path=instance_path)
|
| 231 |
+
return invalid
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
def main(args=sys.argv[1:]): # noqa: D103
|
| 235 |
+
sys.exit(run(arguments=parse_args(args=args)))
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
def run(arguments, stdout=sys.stdout, stderr=sys.stderr, stdin=sys.stdin): # noqa: D103
|
| 239 |
+
outputter = _Outputter.from_arguments(
|
| 240 |
+
arguments=arguments,
|
| 241 |
+
stdout=stdout,
|
| 242 |
+
stderr=stderr,
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
try:
|
| 246 |
+
schema = outputter.load(arguments["schema"])
|
| 247 |
+
except _CannotLoadFile:
|
| 248 |
+
return 1
|
| 249 |
+
|
| 250 |
+
Validator = arguments["validator"]
|
| 251 |
+
if Validator is None:
|
| 252 |
+
Validator = validator_for(schema)
|
| 253 |
+
|
| 254 |
+
try:
|
| 255 |
+
Validator.check_schema(schema)
|
| 256 |
+
except SchemaError as error:
|
| 257 |
+
outputter.validation_error(
|
| 258 |
+
instance_path=arguments["schema"],
|
| 259 |
+
error=error,
|
| 260 |
+
)
|
| 261 |
+
return 1
|
| 262 |
+
|
| 263 |
+
if arguments["instances"]:
|
| 264 |
+
load, instances = outputter.load, arguments["instances"]
|
| 265 |
+
else:
|
| 266 |
+
def load(_):
|
| 267 |
+
try:
|
| 268 |
+
return json.load(stdin)
|
| 269 |
+
except JSONDecodeError as error:
|
| 270 |
+
outputter.parsing_error(
|
| 271 |
+
path="<stdin>", exc_info=sys.exc_info(),
|
| 272 |
+
)
|
| 273 |
+
raise _CannotLoadFile() from error
|
| 274 |
+
instances = ["<stdin>"]
|
| 275 |
+
|
| 276 |
+
resolver = _RefResolver(
|
| 277 |
+
base_uri=arguments["base_uri"],
|
| 278 |
+
referrer=schema,
|
| 279 |
+
) if arguments["base_uri"] is not None else None
|
| 280 |
+
|
| 281 |
+
validator = Validator(schema, resolver=resolver)
|
| 282 |
+
exit_code = 0
|
| 283 |
+
for each in instances:
|
| 284 |
+
try:
|
| 285 |
+
instance = load(each)
|
| 286 |
+
except _CannotLoadFile:
|
| 287 |
+
exit_code = 1
|
| 288 |
+
else:
|
| 289 |
+
exit_code |= _validate_instance(
|
| 290 |
+
instance_path=each,
|
| 291 |
+
instance=instance,
|
| 292 |
+
validator=validator,
|
| 293 |
+
outputter=outputter,
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
return exit_code
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/exceptions.py
ADDED
|
@@ -0,0 +1,487 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Validation errors, and some surrounding helpers.
|
| 3 |
+
"""
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
from collections import defaultdict, deque
|
| 7 |
+
from pprint import pformat
|
| 8 |
+
from textwrap import dedent, indent
|
| 9 |
+
from typing import TYPE_CHECKING, Any, ClassVar
|
| 10 |
+
import heapq
|
| 11 |
+
import itertools
|
| 12 |
+
import warnings
|
| 13 |
+
|
| 14 |
+
from attrs import define
|
| 15 |
+
from referencing.exceptions import Unresolvable as _Unresolvable
|
| 16 |
+
|
| 17 |
+
from jsonschema import _utils
|
| 18 |
+
|
| 19 |
+
if TYPE_CHECKING:
|
| 20 |
+
from collections.abc import Iterable, Mapping, MutableMapping, Sequence
|
| 21 |
+
|
| 22 |
+
from jsonschema import _types
|
| 23 |
+
|
| 24 |
+
WEAK_MATCHES: frozenset[str] = frozenset(["anyOf", "oneOf"])
|
| 25 |
+
STRONG_MATCHES: frozenset[str] = frozenset()
|
| 26 |
+
|
| 27 |
+
_unset = _utils.Unset()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def _pretty(thing: Any, prefix: str):
|
| 31 |
+
"""
|
| 32 |
+
Format something for an error message as prettily as we currently can.
|
| 33 |
+
"""
|
| 34 |
+
return indent(pformat(thing, width=72, sort_dicts=False), prefix).lstrip()
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def __getattr__(name):
|
| 38 |
+
if name == "RefResolutionError":
|
| 39 |
+
warnings.warn(
|
| 40 |
+
_RefResolutionError._DEPRECATION_MESSAGE,
|
| 41 |
+
DeprecationWarning,
|
| 42 |
+
stacklevel=2,
|
| 43 |
+
)
|
| 44 |
+
return _RefResolutionError
|
| 45 |
+
raise AttributeError(f"module {__name__} has no attribute {name}")
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class _Error(Exception):
|
| 49 |
+
|
| 50 |
+
_word_for_schema_in_error_message: ClassVar[str]
|
| 51 |
+
_word_for_instance_in_error_message: ClassVar[str]
|
| 52 |
+
|
| 53 |
+
def __init__(
|
| 54 |
+
self,
|
| 55 |
+
message: str,
|
| 56 |
+
validator: str = _unset, # type: ignore[assignment]
|
| 57 |
+
path: Iterable[str | int] = (),
|
| 58 |
+
cause: Exception | None = None,
|
| 59 |
+
context=(),
|
| 60 |
+
validator_value: Any = _unset,
|
| 61 |
+
instance: Any = _unset,
|
| 62 |
+
schema: Mapping[str, Any] | bool = _unset, # type: ignore[assignment]
|
| 63 |
+
schema_path: Iterable[str | int] = (),
|
| 64 |
+
parent: _Error | None = None,
|
| 65 |
+
type_checker: _types.TypeChecker = _unset, # type: ignore[assignment]
|
| 66 |
+
) -> None:
|
| 67 |
+
super().__init__(
|
| 68 |
+
message,
|
| 69 |
+
validator,
|
| 70 |
+
path,
|
| 71 |
+
cause,
|
| 72 |
+
context,
|
| 73 |
+
validator_value,
|
| 74 |
+
instance,
|
| 75 |
+
schema,
|
| 76 |
+
schema_path,
|
| 77 |
+
parent,
|
| 78 |
+
)
|
| 79 |
+
self.message = message
|
| 80 |
+
self.path = self.relative_path = deque(path)
|
| 81 |
+
self.schema_path = self.relative_schema_path = deque(schema_path)
|
| 82 |
+
self.context = list(context)
|
| 83 |
+
self.cause = self.__cause__ = cause
|
| 84 |
+
self.validator = validator
|
| 85 |
+
self.validator_value = validator_value
|
| 86 |
+
self.instance = instance
|
| 87 |
+
self.schema = schema
|
| 88 |
+
self.parent = parent
|
| 89 |
+
self._type_checker = type_checker
|
| 90 |
+
|
| 91 |
+
for error in context:
|
| 92 |
+
error.parent = self
|
| 93 |
+
|
| 94 |
+
def __repr__(self) -> str:
|
| 95 |
+
return f"<{self.__class__.__name__}: {self.message!r}>"
|
| 96 |
+
|
| 97 |
+
def __str__(self) -> str:
|
| 98 |
+
essential_for_verbose = (
|
| 99 |
+
self.validator, self.validator_value, self.instance, self.schema,
|
| 100 |
+
)
|
| 101 |
+
if any(m is _unset for m in essential_for_verbose):
|
| 102 |
+
return self.message
|
| 103 |
+
|
| 104 |
+
schema_path = _utils.format_as_index(
|
| 105 |
+
container=self._word_for_schema_in_error_message,
|
| 106 |
+
indices=list(self.relative_schema_path)[:-1],
|
| 107 |
+
)
|
| 108 |
+
instance_path = _utils.format_as_index(
|
| 109 |
+
container=self._word_for_instance_in_error_message,
|
| 110 |
+
indices=self.relative_path,
|
| 111 |
+
)
|
| 112 |
+
prefix = 16 * " "
|
| 113 |
+
|
| 114 |
+
return dedent(
|
| 115 |
+
f"""\
|
| 116 |
+
{self.message}
|
| 117 |
+
|
| 118 |
+
Failed validating {self.validator!r} in {schema_path}:
|
| 119 |
+
{_pretty(self.schema, prefix=prefix)}
|
| 120 |
+
|
| 121 |
+
On {instance_path}:
|
| 122 |
+
{_pretty(self.instance, prefix=prefix)}
|
| 123 |
+
""".rstrip(),
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
@classmethod
|
| 127 |
+
def create_from(cls, other: _Error):
|
| 128 |
+
return cls(**other._contents())
|
| 129 |
+
|
| 130 |
+
@property
|
| 131 |
+
def absolute_path(self) -> Sequence[str | int]:
|
| 132 |
+
parent = self.parent
|
| 133 |
+
if parent is None:
|
| 134 |
+
return self.relative_path
|
| 135 |
+
|
| 136 |
+
path = deque(self.relative_path)
|
| 137 |
+
path.extendleft(reversed(parent.absolute_path))
|
| 138 |
+
return path
|
| 139 |
+
|
| 140 |
+
@property
|
| 141 |
+
def absolute_schema_path(self) -> Sequence[str | int]:
|
| 142 |
+
parent = self.parent
|
| 143 |
+
if parent is None:
|
| 144 |
+
return self.relative_schema_path
|
| 145 |
+
|
| 146 |
+
path = deque(self.relative_schema_path)
|
| 147 |
+
path.extendleft(reversed(parent.absolute_schema_path))
|
| 148 |
+
return path
|
| 149 |
+
|
| 150 |
+
@property
|
| 151 |
+
def json_path(self) -> str:
|
| 152 |
+
path = "$"
|
| 153 |
+
for elem in self.absolute_path:
|
| 154 |
+
if isinstance(elem, int):
|
| 155 |
+
path += "[" + str(elem) + "]"
|
| 156 |
+
else:
|
| 157 |
+
path += "." + elem
|
| 158 |
+
return path
|
| 159 |
+
|
| 160 |
+
def _set(
|
| 161 |
+
self,
|
| 162 |
+
type_checker: _types.TypeChecker | None = None,
|
| 163 |
+
**kwargs: Any,
|
| 164 |
+
) -> None:
|
| 165 |
+
if type_checker is not None and self._type_checker is _unset:
|
| 166 |
+
self._type_checker = type_checker
|
| 167 |
+
|
| 168 |
+
for k, v in kwargs.items():
|
| 169 |
+
if getattr(self, k) is _unset:
|
| 170 |
+
setattr(self, k, v)
|
| 171 |
+
|
| 172 |
+
def _contents(self):
|
| 173 |
+
attrs = (
|
| 174 |
+
"message", "cause", "context", "validator", "validator_value",
|
| 175 |
+
"path", "schema_path", "instance", "schema", "parent",
|
| 176 |
+
)
|
| 177 |
+
return {attr: getattr(self, attr) for attr in attrs}
|
| 178 |
+
|
| 179 |
+
def _matches_type(self) -> bool:
|
| 180 |
+
try:
|
| 181 |
+
# We ignore this as we want to simply crash if this happens
|
| 182 |
+
expected = self.schema["type"] # type: ignore[index]
|
| 183 |
+
except (KeyError, TypeError):
|
| 184 |
+
return False
|
| 185 |
+
|
| 186 |
+
if isinstance(expected, str):
|
| 187 |
+
return self._type_checker.is_type(self.instance, expected)
|
| 188 |
+
|
| 189 |
+
return any(
|
| 190 |
+
self._type_checker.is_type(self.instance, expected_type)
|
| 191 |
+
for expected_type in expected
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
class ValidationError(_Error):
|
| 196 |
+
"""
|
| 197 |
+
An instance was invalid under a provided schema.
|
| 198 |
+
"""
|
| 199 |
+
|
| 200 |
+
_word_for_schema_in_error_message = "schema"
|
| 201 |
+
_word_for_instance_in_error_message = "instance"
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
class SchemaError(_Error):
|
| 205 |
+
"""
|
| 206 |
+
A schema was invalid under its corresponding metaschema.
|
| 207 |
+
"""
|
| 208 |
+
|
| 209 |
+
_word_for_schema_in_error_message = "metaschema"
|
| 210 |
+
_word_for_instance_in_error_message = "schema"
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
@define(slots=False)
|
| 214 |
+
class _RefResolutionError(Exception):
|
| 215 |
+
"""
|
| 216 |
+
A ref could not be resolved.
|
| 217 |
+
"""
|
| 218 |
+
|
| 219 |
+
_DEPRECATION_MESSAGE = (
|
| 220 |
+
"jsonschema.exceptions.RefResolutionError is deprecated as of version "
|
| 221 |
+
"4.18.0. If you wish to catch potential reference resolution errors, "
|
| 222 |
+
"directly catch referencing.exceptions.Unresolvable."
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
_cause: Exception
|
| 226 |
+
|
| 227 |
+
def __eq__(self, other):
|
| 228 |
+
if self.__class__ is not other.__class__:
|
| 229 |
+
return NotImplemented # pragma: no cover -- uncovered but deprecated # noqa: E501
|
| 230 |
+
return self._cause == other._cause
|
| 231 |
+
|
| 232 |
+
def __str__(self) -> str:
|
| 233 |
+
return str(self._cause)
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
class _WrappedReferencingError(_RefResolutionError, _Unresolvable): # pragma: no cover -- partially uncovered but to be removed # noqa: E501
|
| 237 |
+
def __init__(self, cause: _Unresolvable):
|
| 238 |
+
object.__setattr__(self, "_wrapped", cause)
|
| 239 |
+
|
| 240 |
+
def __eq__(self, other):
|
| 241 |
+
if other.__class__ is self.__class__:
|
| 242 |
+
return self._wrapped == other._wrapped
|
| 243 |
+
elif other.__class__ is self._wrapped.__class__:
|
| 244 |
+
return self._wrapped == other
|
| 245 |
+
return NotImplemented
|
| 246 |
+
|
| 247 |
+
def __getattr__(self, attr):
|
| 248 |
+
return getattr(self._wrapped, attr)
|
| 249 |
+
|
| 250 |
+
def __hash__(self):
|
| 251 |
+
return hash(self._wrapped)
|
| 252 |
+
|
| 253 |
+
def __repr__(self):
|
| 254 |
+
return f"<WrappedReferencingError {self._wrapped!r}>"
|
| 255 |
+
|
| 256 |
+
def __str__(self):
|
| 257 |
+
return f"{self._wrapped.__class__.__name__}: {self._wrapped}"
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
class UndefinedTypeCheck(Exception):
|
| 261 |
+
"""
|
| 262 |
+
A type checker was asked to check a type it did not have registered.
|
| 263 |
+
"""
|
| 264 |
+
|
| 265 |
+
def __init__(self, type: str) -> None:
|
| 266 |
+
self.type = type
|
| 267 |
+
|
| 268 |
+
def __str__(self) -> str:
|
| 269 |
+
return f"Type {self.type!r} is unknown to this type checker"
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
class UnknownType(Exception):
|
| 273 |
+
"""
|
| 274 |
+
A validator was asked to validate an instance against an unknown type.
|
| 275 |
+
"""
|
| 276 |
+
|
| 277 |
+
def __init__(self, type, instance, schema):
|
| 278 |
+
self.type = type
|
| 279 |
+
self.instance = instance
|
| 280 |
+
self.schema = schema
|
| 281 |
+
|
| 282 |
+
def __str__(self):
|
| 283 |
+
prefix = 16 * " "
|
| 284 |
+
|
| 285 |
+
return dedent(
|
| 286 |
+
f"""\
|
| 287 |
+
Unknown type {self.type!r} for validator with schema:
|
| 288 |
+
{_pretty(self.schema, prefix=prefix)}
|
| 289 |
+
|
| 290 |
+
While checking instance:
|
| 291 |
+
{_pretty(self.instance, prefix=prefix)}
|
| 292 |
+
""".rstrip(),
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
class FormatError(Exception):
|
| 297 |
+
"""
|
| 298 |
+
Validating a format failed.
|
| 299 |
+
"""
|
| 300 |
+
|
| 301 |
+
def __init__(self, message, cause=None):
|
| 302 |
+
super().__init__(message, cause)
|
| 303 |
+
self.message = message
|
| 304 |
+
self.cause = self.__cause__ = cause
|
| 305 |
+
|
| 306 |
+
def __str__(self):
|
| 307 |
+
return self.message
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
class ErrorTree:
|
| 311 |
+
"""
|
| 312 |
+
ErrorTrees make it easier to check which validations failed.
|
| 313 |
+
"""
|
| 314 |
+
|
| 315 |
+
_instance = _unset
|
| 316 |
+
|
| 317 |
+
def __init__(self, errors: Iterable[ValidationError] = ()):
|
| 318 |
+
self.errors: MutableMapping[str, ValidationError] = {}
|
| 319 |
+
self._contents: Mapping[str, ErrorTree] = defaultdict(self.__class__)
|
| 320 |
+
|
| 321 |
+
for error in errors:
|
| 322 |
+
container = self
|
| 323 |
+
for element in error.path:
|
| 324 |
+
container = container[element]
|
| 325 |
+
container.errors[error.validator] = error
|
| 326 |
+
|
| 327 |
+
container._instance = error.instance
|
| 328 |
+
|
| 329 |
+
def __contains__(self, index: str | int):
|
| 330 |
+
"""
|
| 331 |
+
Check whether ``instance[index]`` has any errors.
|
| 332 |
+
"""
|
| 333 |
+
return index in self._contents
|
| 334 |
+
|
| 335 |
+
def __getitem__(self, index):
|
| 336 |
+
"""
|
| 337 |
+
Retrieve the child tree one level down at the given ``index``.
|
| 338 |
+
|
| 339 |
+
If the index is not in the instance that this tree corresponds
|
| 340 |
+
to and is not known by this tree, whatever error would be raised
|
| 341 |
+
by ``instance.__getitem__`` will be propagated (usually this is
|
| 342 |
+
some subclass of `LookupError`.
|
| 343 |
+
"""
|
| 344 |
+
if self._instance is not _unset and index not in self:
|
| 345 |
+
self._instance[index]
|
| 346 |
+
return self._contents[index]
|
| 347 |
+
|
| 348 |
+
def __setitem__(self, index: str | int, value: ErrorTree):
|
| 349 |
+
"""
|
| 350 |
+
Add an error to the tree at the given ``index``.
|
| 351 |
+
|
| 352 |
+
.. deprecated:: v4.20.0
|
| 353 |
+
|
| 354 |
+
Setting items on an `ErrorTree` is deprecated without replacement.
|
| 355 |
+
To populate a tree, provide all of its sub-errors when you
|
| 356 |
+
construct the tree.
|
| 357 |
+
"""
|
| 358 |
+
warnings.warn(
|
| 359 |
+
"ErrorTree.__setitem__ is deprecated without replacement.",
|
| 360 |
+
DeprecationWarning,
|
| 361 |
+
stacklevel=2,
|
| 362 |
+
)
|
| 363 |
+
self._contents[index] = value # type: ignore[index]
|
| 364 |
+
|
| 365 |
+
def __iter__(self):
|
| 366 |
+
"""
|
| 367 |
+
Iterate (non-recursively) over the indices in the instance with errors.
|
| 368 |
+
"""
|
| 369 |
+
return iter(self._contents)
|
| 370 |
+
|
| 371 |
+
def __len__(self):
|
| 372 |
+
"""
|
| 373 |
+
Return the `total_errors`.
|
| 374 |
+
"""
|
| 375 |
+
return self.total_errors
|
| 376 |
+
|
| 377 |
+
def __repr__(self):
|
| 378 |
+
total = len(self)
|
| 379 |
+
errors = "error" if total == 1 else "errors"
|
| 380 |
+
return f"<{self.__class__.__name__} ({total} total {errors})>"
|
| 381 |
+
|
| 382 |
+
@property
|
| 383 |
+
def total_errors(self):
|
| 384 |
+
"""
|
| 385 |
+
The total number of errors in the entire tree, including children.
|
| 386 |
+
"""
|
| 387 |
+
child_errors = sum(len(tree) for _, tree in self._contents.items())
|
| 388 |
+
return len(self.errors) + child_errors
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
def by_relevance(weak=WEAK_MATCHES, strong=STRONG_MATCHES):
|
| 392 |
+
"""
|
| 393 |
+
Create a key function that can be used to sort errors by relevance.
|
| 394 |
+
|
| 395 |
+
Arguments:
|
| 396 |
+
weak (set):
|
| 397 |
+
a collection of validation keywords to consider to be
|
| 398 |
+
"weak". If there are two errors at the same level of the
|
| 399 |
+
instance and one is in the set of weak validation keywords,
|
| 400 |
+
the other error will take priority. By default, :kw:`anyOf`
|
| 401 |
+
and :kw:`oneOf` are considered weak keywords and will be
|
| 402 |
+
superseded by other same-level validation errors.
|
| 403 |
+
|
| 404 |
+
strong (set):
|
| 405 |
+
a collection of validation keywords to consider to be
|
| 406 |
+
"strong"
|
| 407 |
+
|
| 408 |
+
"""
|
| 409 |
+
|
| 410 |
+
def relevance(error):
|
| 411 |
+
validator = error.validator
|
| 412 |
+
return ( # prefer errors which are ...
|
| 413 |
+
-len(error.path), # 'deeper' and thereby more specific
|
| 414 |
+
error.path, # earlier (for sibling errors)
|
| 415 |
+
validator not in weak, # for a non-low-priority keyword
|
| 416 |
+
validator in strong, # for a high priority keyword
|
| 417 |
+
not error._matches_type(), # at least match the instance's type
|
| 418 |
+
) # otherwise we'll treat them the same
|
| 419 |
+
|
| 420 |
+
return relevance
|
| 421 |
+
|
| 422 |
+
|
| 423 |
+
relevance = by_relevance()
|
| 424 |
+
"""
|
| 425 |
+
A key function (e.g. to use with `sorted`) which sorts errors by relevance.
|
| 426 |
+
|
| 427 |
+
Example:
|
| 428 |
+
|
| 429 |
+
.. code:: python
|
| 430 |
+
|
| 431 |
+
sorted(validator.iter_errors(12), key=jsonschema.exceptions.relevance)
|
| 432 |
+
"""
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
def best_match(errors, key=relevance):
|
| 436 |
+
"""
|
| 437 |
+
Try to find an error that appears to be the best match among given errors.
|
| 438 |
+
|
| 439 |
+
In general, errors that are higher up in the instance (i.e. for which
|
| 440 |
+
`ValidationError.path` is shorter) are considered better matches,
|
| 441 |
+
since they indicate "more" is wrong with the instance.
|
| 442 |
+
|
| 443 |
+
If the resulting match is either :kw:`oneOf` or :kw:`anyOf`, the
|
| 444 |
+
*opposite* assumption is made -- i.e. the deepest error is picked,
|
| 445 |
+
since these keywords only need to match once, and any other errors
|
| 446 |
+
may not be relevant.
|
| 447 |
+
|
| 448 |
+
Arguments:
|
| 449 |
+
errors (collections.abc.Iterable):
|
| 450 |
+
|
| 451 |
+
the errors to select from. Do not provide a mixture of
|
| 452 |
+
errors from different validation attempts (i.e. from
|
| 453 |
+
different instances or schemas), since it won't produce
|
| 454 |
+
sensical output.
|
| 455 |
+
|
| 456 |
+
key (collections.abc.Callable):
|
| 457 |
+
|
| 458 |
+
the key to use when sorting errors. See `relevance` and
|
| 459 |
+
transitively `by_relevance` for more details (the default is
|
| 460 |
+
to sort with the defaults of that function). Changing the
|
| 461 |
+
default is only useful if you want to change the function
|
| 462 |
+
that rates errors but still want the error context descent
|
| 463 |
+
done by this function.
|
| 464 |
+
|
| 465 |
+
Returns:
|
| 466 |
+
the best matching error, or ``None`` if the iterable was empty
|
| 467 |
+
|
| 468 |
+
.. note::
|
| 469 |
+
|
| 470 |
+
This function is a heuristic. Its return value may change for a given
|
| 471 |
+
set of inputs from version to version if better heuristics are added.
|
| 472 |
+
|
| 473 |
+
"""
|
| 474 |
+
errors = iter(errors)
|
| 475 |
+
best = next(errors, None)
|
| 476 |
+
if best is None:
|
| 477 |
+
return
|
| 478 |
+
best = max(itertools.chain([best], errors), key=key)
|
| 479 |
+
|
| 480 |
+
while best.context:
|
| 481 |
+
# Calculate the minimum via nsmallest, because we don't recurse if
|
| 482 |
+
# all nested errors have the same relevance (i.e. if min == max == all)
|
| 483 |
+
smallest = heapq.nsmallest(2, best.context, key=key)
|
| 484 |
+
if len(smallest) == 2 and key(smallest[0]) == key(smallest[1]): # noqa: PLR2004
|
| 485 |
+
return best
|
| 486 |
+
best = smallest[0]
|
| 487 |
+
return best
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/protocols.py
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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sonschema interfaces.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
# for reference material on Protocols, see
|
| 6 |
+
# https://www.python.org/dev/peps/pep-0544/
|
| 7 |
+
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
from typing import (
|
| 11 |
+
TYPE_CHECKING,
|
| 12 |
+
Any,
|
| 13 |
+
ClassVar,
|
| 14 |
+
Iterable,
|
| 15 |
+
Protocol,
|
| 16 |
+
runtime_checkable,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# in order for Sphinx to resolve references accurately from type annotations,
|
| 20 |
+
# it needs to see names like `jsonschema.TypeChecker`
|
| 21 |
+
# therefore, only import at type-checking time (to avoid circular references),
|
| 22 |
+
# but use `jsonschema` for any types which will otherwise not be resolvable
|
| 23 |
+
if TYPE_CHECKING:
|
| 24 |
+
from collections.abc import Mapping
|
| 25 |
+
|
| 26 |
+
import referencing.jsonschema
|
| 27 |
+
|
| 28 |
+
from jsonschema import _typing
|
| 29 |
+
from jsonschema.exceptions import ValidationError
|
| 30 |
+
import jsonschema
|
| 31 |
+
import jsonschema.validators
|
| 32 |
+
|
| 33 |
+
# For code authors working on the validator protocol, these are the three
|
| 34 |
+
# use-cases which should be kept in mind:
|
| 35 |
+
#
|
| 36 |
+
# 1. As a protocol class, it can be used in type annotations to describe the
|
| 37 |
+
# available methods and attributes of a validator
|
| 38 |
+
# 2. It is the source of autodoc for the validator documentation
|
| 39 |
+
# 3. It is runtime_checkable, meaning that it can be used in isinstance()
|
| 40 |
+
# checks.
|
| 41 |
+
#
|
| 42 |
+
# Since protocols are not base classes, isinstance() checking is limited in
|
| 43 |
+
# its capabilities. See docs on runtime_checkable for detail
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@runtime_checkable
|
| 47 |
+
class Validator(Protocol):
|
| 48 |
+
"""
|
| 49 |
+
The protocol to which all validator classes adhere.
|
| 50 |
+
|
| 51 |
+
Arguments:
|
| 52 |
+
|
| 53 |
+
schema:
|
| 54 |
+
|
| 55 |
+
The schema that the validator object will validate with.
|
| 56 |
+
It is assumed to be valid, and providing
|
| 57 |
+
an invalid schema can lead to undefined behavior. See
|
| 58 |
+
`Validator.check_schema` to validate a schema first.
|
| 59 |
+
|
| 60 |
+
registry:
|
| 61 |
+
|
| 62 |
+
a schema registry that will be used for looking up JSON references
|
| 63 |
+
|
| 64 |
+
resolver:
|
| 65 |
+
|
| 66 |
+
a resolver that will be used to resolve :kw:`$ref`
|
| 67 |
+
properties (JSON references). If unprovided, one will be created.
|
| 68 |
+
|
| 69 |
+
.. deprecated:: v4.18.0
|
| 70 |
+
|
| 71 |
+
`RefResolver <_RefResolver>` has been deprecated in favor of
|
| 72 |
+
`referencing`, and with it, this argument.
|
| 73 |
+
|
| 74 |
+
format_checker:
|
| 75 |
+
|
| 76 |
+
if provided, a checker which will be used to assert about
|
| 77 |
+
:kw:`format` properties present in the schema. If unprovided,
|
| 78 |
+
*no* format validation is done, and the presence of format
|
| 79 |
+
within schemas is strictly informational. Certain formats
|
| 80 |
+
require additional packages to be installed in order to assert
|
| 81 |
+
against instances. Ensure you've installed `jsonschema` with
|
| 82 |
+
its `extra (optional) dependencies <index:extras>` when
|
| 83 |
+
invoking ``pip``.
|
| 84 |
+
|
| 85 |
+
.. deprecated:: v4.12.0
|
| 86 |
+
|
| 87 |
+
Subclassing validator classes now explicitly warns this is not part of
|
| 88 |
+
their public API.
|
| 89 |
+
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
+
#: An object representing the validator's meta schema (the schema that
|
| 93 |
+
#: describes valid schemas in the given version).
|
| 94 |
+
META_SCHEMA: ClassVar[Mapping]
|
| 95 |
+
|
| 96 |
+
#: A mapping of validation keywords (`str`\s) to functions that
|
| 97 |
+
#: validate the keyword with that name. For more information see
|
| 98 |
+
#: `creating-validators`.
|
| 99 |
+
VALIDATORS: ClassVar[Mapping]
|
| 100 |
+
|
| 101 |
+
#: A `jsonschema.TypeChecker` that will be used when validating
|
| 102 |
+
#: :kw:`type` keywords in JSON schemas.
|
| 103 |
+
TYPE_CHECKER: ClassVar[jsonschema.TypeChecker]
|
| 104 |
+
|
| 105 |
+
#: A `jsonschema.FormatChecker` that will be used when validating
|
| 106 |
+
#: :kw:`format` keywords in JSON schemas.
|
| 107 |
+
FORMAT_CHECKER: ClassVar[jsonschema.FormatChecker]
|
| 108 |
+
|
| 109 |
+
#: A function which given a schema returns its ID.
|
| 110 |
+
ID_OF: _typing.id_of
|
| 111 |
+
|
| 112 |
+
#: The schema that will be used to validate instances
|
| 113 |
+
schema: Mapping | bool
|
| 114 |
+
|
| 115 |
+
def __init__(
|
| 116 |
+
self,
|
| 117 |
+
schema: Mapping | bool,
|
| 118 |
+
registry: referencing.jsonschema.SchemaRegistry,
|
| 119 |
+
format_checker: jsonschema.FormatChecker | None = None,
|
| 120 |
+
) -> None:
|
| 121 |
+
...
|
| 122 |
+
|
| 123 |
+
@classmethod
|
| 124 |
+
def check_schema(cls, schema: Mapping | bool) -> None:
|
| 125 |
+
"""
|
| 126 |
+
Validate the given schema against the validator's `META_SCHEMA`.
|
| 127 |
+
|
| 128 |
+
Raises:
|
| 129 |
+
|
| 130 |
+
`jsonschema.exceptions.SchemaError`:
|
| 131 |
+
|
| 132 |
+
if the schema is invalid
|
| 133 |
+
|
| 134 |
+
"""
|
| 135 |
+
|
| 136 |
+
def is_type(self, instance: Any, type: str) -> bool:
|
| 137 |
+
"""
|
| 138 |
+
Check if the instance is of the given (JSON Schema) type.
|
| 139 |
+
|
| 140 |
+
Arguments:
|
| 141 |
+
|
| 142 |
+
instance:
|
| 143 |
+
|
| 144 |
+
the value to check
|
| 145 |
+
|
| 146 |
+
type:
|
| 147 |
+
|
| 148 |
+
the name of a known (JSON Schema) type
|
| 149 |
+
|
| 150 |
+
Returns:
|
| 151 |
+
|
| 152 |
+
whether the instance is of the given type
|
| 153 |
+
|
| 154 |
+
Raises:
|
| 155 |
+
|
| 156 |
+
`jsonschema.exceptions.UnknownType`:
|
| 157 |
+
|
| 158 |
+
if ``type`` is not a known type
|
| 159 |
+
|
| 160 |
+
"""
|
| 161 |
+
|
| 162 |
+
def is_valid(self, instance: Any) -> bool:
|
| 163 |
+
"""
|
| 164 |
+
Check if the instance is valid under the current `schema`.
|
| 165 |
+
|
| 166 |
+
Returns:
|
| 167 |
+
|
| 168 |
+
whether the instance is valid or not
|
| 169 |
+
|
| 170 |
+
>>> schema = {"maxItems" : 2}
|
| 171 |
+
>>> Draft202012Validator(schema).is_valid([2, 3, 4])
|
| 172 |
+
False
|
| 173 |
+
|
| 174 |
+
"""
|
| 175 |
+
|
| 176 |
+
def iter_errors(self, instance: Any) -> Iterable[ValidationError]:
|
| 177 |
+
r"""
|
| 178 |
+
Lazily yield each of the validation errors in the given instance.
|
| 179 |
+
|
| 180 |
+
>>> schema = {
|
| 181 |
+
... "type" : "array",
|
| 182 |
+
... "items" : {"enum" : [1, 2, 3]},
|
| 183 |
+
... "maxItems" : 2,
|
| 184 |
+
... }
|
| 185 |
+
>>> v = Draft202012Validator(schema)
|
| 186 |
+
>>> for error in sorted(v.iter_errors([2, 3, 4]), key=str):
|
| 187 |
+
... print(error.message)
|
| 188 |
+
4 is not one of [1, 2, 3]
|
| 189 |
+
[2, 3, 4] is too long
|
| 190 |
+
|
| 191 |
+
.. deprecated:: v4.0.0
|
| 192 |
+
|
| 193 |
+
Calling this function with a second schema argument is deprecated.
|
| 194 |
+
Use `Validator.evolve` instead.
|
| 195 |
+
"""
|
| 196 |
+
|
| 197 |
+
def validate(self, instance: Any) -> None:
|
| 198 |
+
"""
|
| 199 |
+
Check if the instance is valid under the current `schema`.
|
| 200 |
+
|
| 201 |
+
Raises:
|
| 202 |
+
|
| 203 |
+
`jsonschema.exceptions.ValidationError`:
|
| 204 |
+
|
| 205 |
+
if the instance is invalid
|
| 206 |
+
|
| 207 |
+
>>> schema = {"maxItems" : 2}
|
| 208 |
+
>>> Draft202012Validator(schema).validate([2, 3, 4])
|
| 209 |
+
Traceback (most recent call last):
|
| 210 |
+
...
|
| 211 |
+
ValidationError: [2, 3, 4] is too long
|
| 212 |
+
|
| 213 |
+
"""
|
| 214 |
+
|
| 215 |
+
def evolve(self, **kwargs) -> Validator:
|
| 216 |
+
"""
|
| 217 |
+
Create a new validator like this one, but with given changes.
|
| 218 |
+
|
| 219 |
+
Preserves all other attributes, so can be used to e.g. create a
|
| 220 |
+
validator with a different schema but with the same :kw:`$ref`
|
| 221 |
+
resolution behavior.
|
| 222 |
+
|
| 223 |
+
>>> validator = Draft202012Validator({})
|
| 224 |
+
>>> validator.evolve(schema={"type": "number"})
|
| 225 |
+
Draft202012Validator(schema={'type': 'number'}, format_checker=None)
|
| 226 |
+
|
| 227 |
+
The returned object satisfies the validator protocol, but may not
|
| 228 |
+
be of the same concrete class! In particular this occurs
|
| 229 |
+
when a :kw:`$ref` occurs to a schema with a different
|
| 230 |
+
:kw:`$schema` than this one (i.e. for a different draft).
|
| 231 |
+
|
| 232 |
+
>>> validator.evolve(
|
| 233 |
+
... schema={"$schema": Draft7Validator.META_SCHEMA["$id"]}
|
| 234 |
+
... )
|
| 235 |
+
Draft7Validator(schema=..., format_checker=None)
|
| 236 |
+
"""
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/tests/__init__.py
ADDED
|
File without changes
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/tests/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (174 Bytes). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/tests/__pycache__/_suite.cpython-310.pyc
ADDED
|
Binary file (8.94 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/tests/__pycache__/fuzz_validate.cpython-310.pyc
ADDED
|
Binary file (1.37 kB). View file
|
|
|
infer_4_37_2/lib/python3.10/site-packages/jsonschema/tests/__pycache__/test_cli.cpython-310.pyc
ADDED
|
Binary file (23.1 kB). View file
|
|
|