Skip to content

Commit

Permalink
Fix sphinx nitpick error arising from annotations
Browse files Browse the repository at this point in the history
Type annotations can use variable names in order to support easily
named constructs (e.g. `FooType = Union[int, str]; x: FooType`).
However, such variable names are then seen by sphinx autodoc, which
does not evaluate them. As a result, for such a name to avoid tripping
nitpick warnings, these names need to be resolvable.

The simplest resolution is to remove the use of any internal variables
for this purpose (at least where they would be seen by sphinx), and
use the longhand description of types in such cases.
  • Loading branch information
sirosen committed Jan 5, 2022
1 parent 631fba1 commit b7d9a24
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions jsonschema/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@

from jsonschema.exceptions import UndefinedTypeCheck

# internal type declarations for annotations
_TypeCheckerFunc = typing.Callable[["TypeChecker", typing.Any], bool]
_TypeCheckerMapping = typing.Mapping[str, _TypeCheckerFunc]


# unfortunately, the type of pmap is generic, and if used as the attr.ib
# converter, the generic type is presented to mypy, which then fails to match
# the concrete type of a type checker mapping
# this "do nothing" wrapper presents the correct information to mypy
def _typed_pmap_converter(
init_val: _TypeCheckerMapping,
) -> _TypeCheckerMapping:
return typing.cast(_TypeCheckerMapping, pmap(init_val))
init_val: typing.Mapping[str, typing.Callable[["TypeChecker", typing.Any], bool]],
) -> typing.Mapping[str, typing.Callable[["TypeChecker", typing.Any], bool]]:
return typing.cast(
typing.Mapping[str, typing.Callable[["TypeChecker", typing.Any], bool]],
pmap(init_val),
)


def is_array(checker, instance):
Expand Down Expand Up @@ -78,8 +77,11 @@ class TypeChecker(object):
The initial mapping of types to their checking functions.
"""

_type_checkers: _TypeCheckerMapping = attr.ib(
default=pmap(), converter=_typed_pmap_converter,
_type_checkers: typing.Mapping[
str, typing.Callable[["TypeChecker", typing.Any], bool]
] = attr.ib(
default=pmap(),
converter=_typed_pmap_converter,
)

def is_type(self, instance, type):
Expand Down

0 comments on commit b7d9a24

Please sign in to comment.