Skip to content

Commit

Permalink
Use future import for type annotations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sirosen committed Jan 3, 2022
1 parent 7ecc47d commit 2e18b46
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
8 changes: 5 additions & 3 deletions jsonschema/_format.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from contextlib import suppress
from uuid import UUID
import datetime
Expand All @@ -8,7 +10,7 @@
from jsonschema.exceptions import FormatError

_FormatCheckerFunc = typing.Callable[[typing.Any], bool]
_CheckerRaises = typing.Union[Exception, typing.Tuple[Exception, ...]]
_CheckerRaises = Exception | typing.Tuple[Exception, ...]


class FormatChecker(object):
Expand All @@ -34,9 +36,9 @@ class FormatChecker(object):
limit which formats will be used during validation.
"""

checkers: typing.Dict[
checkers: dict[
str,
typing.Tuple[_FormatCheckerFunc, _CheckerRaises],
tuple[_FormatCheckerFunc, _CheckerRaises],
] = {}

def __init__(self, formats=None):
Expand Down
7 changes: 4 additions & 3 deletions jsonschema/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
"""
Validation errors, and some surrounding helpers.
"""
from __future__ import annotations

from collections import defaultdict, deque
from pprint import pformat
from textwrap import dedent, indent
import itertools
import typing

import attr

from jsonschema import _utils

WEAK_MATCHES: typing.FrozenSet[str] = frozenset(["anyOf", "oneOf"])
STRONG_MATCHES: typing.FrozenSet[str] = frozenset()
WEAK_MATCHES: frozenset[str] = frozenset(["anyOf", "oneOf"])
STRONG_MATCHES: frozenset[str] = frozenset()

_unset = _utils.Unset()

Expand Down
12 changes: 7 additions & 5 deletions jsonschema/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
# for reference material on Protocols, see
# https://www.python.org/dev/peps/pep-0544/

from typing import Any, ClassVar, Iterator, Optional, Union
from __future__ import annotations

from typing import Any, ClassVar, Iterator
import sys

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

#: The schema that was passed in when initializing the object.
schema: Union[dict, bool]
schema: dict | bool

def __init__(
self,
schema: Union[dict, bool],
resolver: Optional[RefResolver] = None,
format_checker: Optional[FormatChecker] = None,
schema: dict | bool,
resolver: RefResolver | None = None,
format_checker: FormatChecker | None = None,
) -> None:
...

Expand Down
6 changes: 4 additions & 2 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Creation and extension of validators, with implementations for existing drafts.
"""
from __future__ import annotations

from collections import deque
from collections.abc import Sequence
from functools import lru_cache
Expand All @@ -23,9 +25,9 @@
exceptions,
)

_VALIDATORS: typing.Dict[str, typing.Any] = {}
_VALIDATORS: dict[str, typing.Any] = {}
_META_SCHEMAS = _utils.URIDict()
_VOCABULARIES: typing.List[typing.Tuple[str, typing.Any]] = []
_VOCABULARIES: list[tuple[str, typing.Any]] = []


def __getattr__(name):
Expand Down

0 comments on commit 2e18b46

Please sign in to comment.