|
5 | 5 | # for reference material on Protocols, see
|
6 | 6 | # https://www.python.org/dev/peps/pep-0544/
|
7 | 7 |
|
8 |
| -from typing import Any, ClassVar, Iterator, Optional, Union |
| 8 | +from __future__ import annotations |
9 | 9 |
|
10 |
| -try: |
| 10 | +from typing import TYPE_CHECKING, Any, ClassVar, Iterator |
| 11 | +import sys |
| 12 | + |
| 13 | +# doing these imports with `try ... except ImportError` doesn't pass mypy |
| 14 | +# checking because mypy sees `typing._SpecialForm` and |
| 15 | +# `typing_extensions._SpecialForm` as incompatible |
| 16 | +# |
| 17 | +# see: |
| 18 | +# https://mypy.readthedocs.io/en/stable/runtime_troubles.html#using-new-additions-to-the-typing-module |
| 19 | +# https://github.com/python/mypy/issues/4427 |
| 20 | +if sys.version_info >= (3, 8): |
11 | 21 | from typing import Protocol, runtime_checkable
|
12 |
| -except ImportError: |
| 22 | +else: |
13 | 23 | from typing_extensions import Protocol, runtime_checkable
|
14 | 24 |
|
15 |
| -from jsonschema._format import FormatChecker |
16 |
| -from jsonschema._types import TypeChecker |
| 25 | +# in order for Sphinx to resolve references accurately from type annotations, |
| 26 | +# it needs to see names like `jsonschema.TypeChecker` |
| 27 | +# therefore, only import at type-checking time (to avoid circular references), |
| 28 | +# but use `jsonschema` for any types which will otherwise not be resolvable |
| 29 | +if TYPE_CHECKING: |
| 30 | + import jsonschema |
| 31 | + |
17 | 32 | from jsonschema.exceptions import ValidationError
|
18 | 33 | from jsonschema.validators import RefResolver
|
19 | 34 |
|
@@ -62,16 +77,16 @@ class Validator(Protocol):
|
62 | 77 |
|
63 | 78 | #: A `jsonschema.TypeChecker` that will be used when validating
|
64 | 79 | #: :validator:`type` properties in JSON schemas.
|
65 |
| - TYPE_CHECKER: ClassVar[TypeChecker] |
| 80 | + TYPE_CHECKER: ClassVar[jsonschema.TypeChecker] |
66 | 81 |
|
67 | 82 | #: The schema that was passed in when initializing the object.
|
68 |
| - schema: Union[dict, bool] |
| 83 | + schema: dict | bool |
69 | 84 |
|
70 | 85 | def __init__(
|
71 | 86 | self,
|
72 |
| - schema: Union[dict, bool], |
73 |
| - resolver: Optional[RefResolver] = None, |
74 |
| - format_checker: Optional[FormatChecker] = None, |
| 87 | + schema: dict | bool, |
| 88 | + resolver: RefResolver | None = None, |
| 89 | + format_checker: jsonschema.FormatChecker | None = None, |
75 | 90 | ) -> None:
|
76 | 91 | ...
|
77 | 92 |
|
|
0 commit comments