summaryrefslogtreecommitdiff
path: root/jsonschema/_format.py
diff options
context:
space:
mode:
authorStephen Rosen <sirosen@globus.org>2021-12-10 23:30:32 +0000
committerStephen Rosen <sirosen@globus.org>2022-01-05 22:30:00 +0000
commit5a2f8ee5a4b428582d7224b79da17277648820a2 (patch)
treed129d8e56402e7b6b3ae8ed39cf2c6099d9045ff /jsonschema/_format.py
parentfc0990a365d162a3e90b3d5296788146331f69ee (diff)
downloadjsonschema-5a2f8ee5a4b428582d7224b79da17277648820a2.tar.gz
Setup mypy in `tox -e typing` and get it to pass
This is the smallest possible change to get mypy passing on the jsonschema codebase. The goal of this configuration is to enforce type annotations anywhere that they appear. That is, if a method is added to the codebase, def foo(x: int) -> str: return str(x) then usages of `foo` will by type checked. If no annotations are added, `mypy` will not type check functions. For the most part, this keeps the impact low. The one exceptional case is the use of `pyrsistent.pmap` as an argument to `attr.ib(converter=...)`. Unfortunately, it causes `mypy` to incorrectly deduce the type of the init parameter created by attrs. We need to "explain the type of init" to mypy by creating a callable with a concrete type to act as the converter. The callable in question simply wraps `pmap` with a cast and presents the desired type information to mypy.
Diffstat (limited to 'jsonschema/_format.py')
-rw-r--r--jsonschema/_format.py9
1 files changed, 8 insertions, 1 deletions
diff --git a/jsonschema/_format.py b/jsonschema/_format.py
index c6a0d05..dba54da 100644
--- a/jsonschema/_format.py
+++ b/jsonschema/_format.py
@@ -3,9 +3,13 @@ from uuid import UUID
import datetime
import ipaddress
import re
+import typing
from jsonschema.exceptions import FormatError
+_FormatCheckerFunc = typing.Callable[[typing.Any], bool]
+_CheckerRaises = typing.Union[Exception, typing.Tuple[Exception, ...]]
+
class FormatChecker(object):
"""
@@ -30,7 +34,10 @@ class FormatChecker(object):
limit which formats will be used during validation.
"""
- checkers = {}
+ checkers: typing.Dict[
+ str,
+ typing.Tuple[_FormatCheckerFunc, _CheckerRaises],
+ ] = {}
def __init__(self, formats=None):
if formats is None: