Skip to content

Commit 2474242

Browse files
committed
Use future import for type annotations
Use of the `__future__` import of annotations allows several niceties, in particular: - parametrization of builtin types as generics - `|` syntax for unions (including `| None` for optionals) Update to use the future import wherever it improves or simplifies annotations. Avoid using new typing features outside of annotations (e.g. in assignment), which fails on older pythons. This is an unfortunate wart in the way that the future import works.
1 parent ec8dab1 commit 2474242

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

jsonschema/_format.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from contextlib import suppress
24
from uuid import UUID
35
import datetime
@@ -34,9 +36,9 @@ class FormatChecker(object):
3436
limit which formats will be used during validation.
3537
"""
3638

37-
checkers: typing.Dict[
39+
checkers: dict[
3840
str,
39-
typing.Tuple[_FormatCheckerFunc, _CheckerRaises],
41+
tuple[_FormatCheckerFunc, _CheckerRaises],
4042
] = {}
4143

4244
def __init__(self, formats=None):

jsonschema/exceptions.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
"""
22
Validation errors, and some surrounding helpers.
33
"""
4+
from __future__ import annotations
5+
46
from collections import defaultdict, deque
57
from pprint import pformat
68
from textwrap import dedent, indent
79
import itertools
8-
import typing
910

1011
import attr
1112

1213
from jsonschema import _utils
1314

14-
WEAK_MATCHES: typing.FrozenSet[str] = frozenset(["anyOf", "oneOf"])
15-
STRONG_MATCHES: typing.FrozenSet[str] = frozenset()
15+
WEAK_MATCHES: frozenset[str] = frozenset(["anyOf", "oneOf"])
16+
STRONG_MATCHES: frozenset[str] = frozenset()
1617

1718
_unset = _utils.Unset()
1819

jsonschema/protocols.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
# for reference material on Protocols, see
66
# https://www.python.org/dev/peps/pep-0544/
77

8-
from typing import Any, ClassVar, Iterator, Optional, Union
8+
from __future__ import annotations
9+
10+
from typing import Any, ClassVar, Iterator
911
import sys
1012

1113
# doing these imports with `try ... except ImportError` doesn't pass mypy
@@ -73,13 +75,13 @@ class Validator(Protocol):
7375
TYPE_CHECKER: ClassVar[TypeChecker]
7476

7577
#: The schema that was passed in when initializing the object.
76-
schema: Union[dict, bool]
78+
schema: dict | bool
7779

7880
def __init__(
7981
self,
80-
schema: Union[dict, bool],
81-
resolver: Optional[RefResolver] = None,
82-
format_checker: Optional[FormatChecker] = None,
82+
schema: dict | bool,
83+
resolver: RefResolver | None = None,
84+
format_checker: FormatChecker | None = None,
8385
) -> None:
8486
...
8587

jsonschema/tests/test_validators.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from collections import deque, namedtuple
24
from contextlib import contextmanager
35
from decimal import Decimal
@@ -8,7 +10,6 @@
810
import os
911
import sys
1012
import tempfile
11-
import typing
1213
import unittest
1314
import warnings
1415

@@ -1663,7 +1664,7 @@ def test_False_is_not_a_schema_even_if_you_forget_to_check(self):
16631664

16641665
class TestDraft3Validator(AntiDraft6LeakMixin, ValidatorTestMixin, TestCase):
16651666
Validator = validators.Draft3Validator
1666-
valid: typing.Tuple[dict, dict] = ({}, {})
1667+
valid: tuple[dict, dict] = ({}, {})
16671668
invalid = {"type": "integer"}, "foo"
16681669

16691670
def test_any_type_is_valid_for_type_any(self):
@@ -1695,31 +1696,31 @@ def test_is_type_does_not_evade_bool_if_it_is_being_tested(self):
16951696

16961697
class TestDraft4Validator(AntiDraft6LeakMixin, ValidatorTestMixin, TestCase):
16971698
Validator = validators.Draft4Validator
1698-
valid: typing.Tuple[dict, dict] = ({}, {})
1699+
valid: tuple[dict, dict] = ({}, {})
16991700
invalid = {"type": "integer"}, "foo"
17001701

17011702

17021703
class TestDraft6Validator(ValidatorTestMixin, TestCase):
17031704
Validator = validators.Draft6Validator
1704-
valid: typing.Tuple[dict, dict] = ({}, {})
1705+
valid: tuple[dict, dict] = ({}, {})
17051706
invalid = {"type": "integer"}, "foo"
17061707

17071708

17081709
class TestDraft7Validator(ValidatorTestMixin, TestCase):
17091710
Validator = validators.Draft7Validator
1710-
valid: typing.Tuple[dict, dict] = ({}, {})
1711+
valid: tuple[dict, dict] = ({}, {})
17111712
invalid = {"type": "integer"}, "foo"
17121713

17131714

17141715
class TestDraft201909Validator(ValidatorTestMixin, TestCase):
17151716
Validator = validators.Draft201909Validator
1716-
valid: typing.Tuple[dict, dict] = ({}, {})
1717+
valid: tuple[dict, dict] = ({}, {})
17171718
invalid = {"type": "integer"}, "foo"
17181719

17191720

17201721
class TestDraft202012Validator(ValidatorTestMixin, TestCase):
17211722
Validator = validators.Draft202012Validator
1722-
valid: typing.Tuple[dict, dict] = ({}, {})
1723+
valid: tuple[dict, dict] = ({}, {})
17231724
invalid = {"type": "integer"}, "foo"
17241725

17251726

jsonschema/validators.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
Creation and extension of validators, with implementations for existing drafts.
33
"""
4+
from __future__ import annotations
5+
46
from collections import deque
57
from collections.abc import Sequence
68
from functools import lru_cache
@@ -23,9 +25,9 @@
2325
exceptions,
2426
)
2527

26-
_VALIDATORS: typing.Dict[str, typing.Any] = {}
28+
_VALIDATORS: dict[str, typing.Any] = {}
2729
_META_SCHEMAS = _utils.URIDict()
28-
_VOCABULARIES: typing.List[typing.Tuple[str, typing.Any]] = []
30+
_VOCABULARIES: list[tuple[str, typing.Any]] = []
2931

3032

3133
def __getattr__(name):

0 commit comments

Comments
 (0)