diff --git a/src/jsonschema_gen/parsers.py b/src/jsonschema_gen/parsers.py index e5e487c..eed702e 100644 --- a/src/jsonschema_gen/parsers.py +++ b/src/jsonschema_gen/parsers.py @@ -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", @@ -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 = {}, [] @@ -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: @@ -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) @@ -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): @@ -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, ) @@ -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) diff --git a/src/jsonschema_gen/schema.py b/src/jsonschema_gen/schema.py index 2dca9f4..97791a0 100644 --- a/src/jsonschema_gen/schema.py +++ b/src/jsonschema_gen/schema.py @@ -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", @@ -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 diff --git a/src/jsonschema_gen/utils.py b/src/jsonschema_gen/utils.py index e9bafc2..e6949bd 100644 --- a/src/jsonschema_gen/utils.py +++ b/src/jsonschema_gen/utils.py @@ -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", @@ -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: