Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Apr 7, 2024
1 parent ae8dae7 commit 55f2241
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 45 deletions.
54 changes: 21 additions & 33 deletions src/jsonschema_gen/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@
from weakref import proxy

import jsonschema_gen.schema as j
from jsonschema_gen.utils import (NoneType, compatible_py39, compatible_py310,
compatible_py311, get_args,
get_function_summary, get_generic_alias,
get_origin, is_namedtuple, is_typeddict,
is_union)
from jsonschema_gen.utils import (
NoneType,
compatible_py39,
compatible_py310,
compatible_py311,
get_args,
get_function_summary,
get_generic_alias,
get_origin,
is_namedtuple,
is_typeddict,
is_union,
)

__all__ = [
"TYPES",
Expand Down Expand Up @@ -115,9 +123,7 @@ def parse_class(self, cls: t.Type, /) -> t.Dict[str, FunctionAnnotation]:
_method_map[name] = self.parse_function(value, cls)
return _method_map

def parse_function(
self, f: t.Callable, /, cls: t.Optional[t.Type] = None
) -> FunctionAnnotation:
def parse_function(self, f: t.Callable, /, cls: t.Optional[t.Type] = None) -> FunctionAnnotation:
"""Parse method or function arguments and return type into jsonschema style annotations."""
sign = inspect.signature(f)
params, required = {}, []
Expand All @@ -141,9 +147,7 @@ def parse_function(
continue

if arg.kind == _ParameterKind.POSITIONAL_ONLY:
raise IncompatibleTypesError(
"Positional only arguments cannot be converted to a JSONSchema object."
)
raise IncompatibleTypesError("Positional only arguments cannot be converted to a JSONSchema object.")

if type(arg.annotation) is t.TypeVar:
if cls:
Expand Down Expand Up @@ -391,13 +395,9 @@ def parse_annotation(self, annotation, /) -> j.JSONSchemaType:
str,
bytes,
):
raise IncompatibleTypesError(
f"Dictionary keys must be strings, got {annotation.__args__[0]}"
)
raise IncompatibleTypesError(f"Dictionary keys must be strings, got {annotation.__args__[0]}")
if annotation.__args__[1] not in (t.Any, ...):
properties = {
"^.+$": self._parser.parse_annotation(annotation.__args__[1])
}
properties = {"^.+$": self._parser.parse_annotation(annotation.__args__[1])}
return self.annotation(patternProperties=properties)


Expand Down Expand Up @@ -450,18 +450,12 @@ def parse_annotation(self, annotation: t.NamedTuple, /) -> j.JSONSchemaType:
annotations = getattr(annotation, "__annotations__", {})
items = []
for key in annotation._fields: # noqa: no public attr
arg = (
self._parser.parse_annotation(annotations[key])
if key in annotations
else j.JSONSchemaObject()
)
arg = self._parser.parse_annotation(annotations[key]) if key in annotations else j.JSONSchemaObject()
arg.title = key
if key in defaults:
arg.default = defaults[key]
items.append(arg)
return self.annotation(
prefixItems=items, description=annotation.__doc__, title=title
)
return self.annotation(prefixItems=items, description=annotation.__doc__, title=title)


class EnumValueParser(TypeParser):
Expand Down Expand Up @@ -491,9 +485,7 @@ def parse_annotation(self, annotation: t.Type[Enum], /) -> j.JSONSchemaType:
title = annotation.__name__
return self.annotation(
description=annotation.__doc__,
enum=[
v.value for k, v in annotation._member_map_.items()
], # noqa: no public attr
enum=[v.value for k, v in annotation._member_map_.items()], # noqa: no public attr
title=title,
)

Expand Down Expand Up @@ -525,9 +517,5 @@ def parse_annotation(self, annotation, /) -> j.JSONSchemaType:


for value in tuple(locals().values()):
if (
inspect.isclass(value)
and issubclass(value, TypeParser)
and value is not TypeParser
):
if inspect.isclass(value) and issubclass(value, TypeParser) and value is not TypeParser:
TYPES.append(value)
7 changes: 2 additions & 5 deletions src/jsonschema_gen/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
# pylint: disable=C0103

from dataclasses import dataclass, field
from typing import (Any, Collection, Dict, List, Literal, Mapping, Protocol,
TypeVar)
from typing import Any, Collection, Dict, List, Literal, Mapping, Protocol, TypeVar

__all__ = (
"JSONSchemaObject",
Expand Down Expand Up @@ -297,9 +296,7 @@ class Array(JSONSchemaType):
"""

items: JSONSchemaType = None #: item type for a strict typed array
prefixItems: Collection[JSONSchemaType] = (
None #: a List of fixed object positions for a tuple type
)
prefixItems: Collection[JSONSchemaType] = None #: a List of fixed object positions for a tuple type
contains: JSONSchemaType = None #: must contain this type of object
additionalItems: bool = None #: allow additional items
uniqueItems: bool = None #: specify an array as a set type
Expand Down
9 changes: 2 additions & 7 deletions src/jsonschema_gen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import inspect
import sys
from textwrap import dedent
from typing import (Any, Type, Union, _GenericAlias, # noqa: magic
_TypedDictMeta)
from typing import Any, Type, Union, _GenericAlias, _TypedDictMeta # noqa: magic

__all__ = [
"NoneType",
Expand Down Expand Up @@ -67,11 +66,7 @@ def is_typeddict(value, /) -> bool:


def is_namedtuple(value, /) -> bool:
return (
inspect.isclass(value)
and issubclass(value, tuple)
and hasattr(value, "_fields")
)
return inspect.isclass(value) and issubclass(value, tuple) and hasattr(value, "_fields")


def is_union(value, /) -> bool:
Expand Down

0 comments on commit 55f2241

Please sign in to comment.