Parsers

This module contains the main parser class as well as a list of type annotation parsers. To add your custom parser to the default list of parsers you must add it to the jsonschema_gen.parsers.TYPES list.

TYPES : List[TypeParser] - default list of type parsers

default collection of type parsers

class FunctionAnnotation[source]

Function annotation with input kwargs and return values schemas.

kwargs: Object | None

Alias for field number 0

returns: JSONSchemaType | None

Alias for field number 1

class Parser[source]

Python annotations parser.

Parse an annotation:

>>> Parser().parse_annotation(t.List[str], default=[]).json_repr()
{'items': {'type': 'string'}, 'default': [], 'type': 'array'}

Parse a function (method):

>>> def test(value: str) -> int: ...
>>> annotations = Parser().parse_function(test)
>>> annotations.kwargs.json_repr()
{'properties': {'value': {'type': 'string'}}, 'additionalProperties': False, 'required': ['value'], 'type': 'object'}

Parse a class:

>>> class C:
...     def test(self, value: str) -> int: ...
>>> annotations_map = Parser().parse_class(C)
>>> annotations_map['test'].kwargs.json_repr()
{'properties': {'value': {'type': 'string'}}, 'additionalProperties': False, 'required': ['value'], 'type': 'object'}
__init__(*, strict: bool = True, private_arg_prefix: str = '_', types: List[Type[TypeParser]] | None = None, locals: dict | None = None)[source]

Initialize

Parameters:
  • strict – strict parsing - allow only JSONSchema compatible types for example: UUID type is not allowed in strict because it’s not an actual data type in JSON

  • private_arg_prefix – ignore args starting with such prefix

  • types – list of type parsers, by default TYPES is used

  • locals – a map of local variables to resolve plain string references in type hints

parse_class(cls: Type, /) Dict[str, FunctionAnnotation][source]

Parse class methods and create an annotation map for the whole class.

parse_function(f: Callable, /, cls: Type | None = None) FunctionAnnotation[source]

Parse method or function arguments and return type into jsonschema style annotations.

parse_annotation(annotation, /, default=...) JSONSchemaType[source]

Convert python annotation into a jsonschema object.

Base type parser class.

class TypeParser[source]

Type parser

types: Tuple[Type]
annotation: Type[JSONSchemaObject]
attrs: dict = None
strict: bool = True
can_parse(annotation, /) bool[source]
parse_annotation(annotation, /) JSONSchemaType[source]
parse_args(args, /)[source]

Type-specific parsers.

class AnyParser[source]
types: Tuple[Type] = (typing.Any,)
annotation

alias of JSONSchemaObject

class BooleanParser[source]
types: Tuple[Type] = (<class 'bool'>,)
annotation

alias of Boolean

class ConstantParser[source]
can_parse(annotation, /) bool[source]
parse_annotation(annotation, /) JSONSchemaType[source]
class DictParser[source]
types: Tuple[Type] = (<class 'dict'>, <class 'collections.abc.Mapping'>, typing.Mapping, typing.MutableMapping, <class 'collections.abc.MutableMapping'>)
annotation

alias of Object

parse_annotation(annotation, /) JSONSchemaType[source]
class EnumTypeParser[source]
types: Tuple[Type] = (<enum 'Enum'>,)
annotation

alias of Enum

strict: bool = False
can_parse(annotation, /) bool[source]
parse_annotation(annotation: Type[Enum], /) JSONSchemaType[source]
class EnumValueParser[source]
types: Tuple[Type] = (<enum 'Enum'>,)
annotation

alias of Const

strict: bool = False
can_parse(annotation, /) bool[source]
parse_annotation(annotation: Enum, /) JSONSchemaType[source]
class IntegerParser[source]
types: Tuple[Type] = (<class 'int'>,)
annotation

alias of Integer

class ListParser[source]
types: Tuple[Type] = (<class 'list'>, typing.List, typing.Collection, <class 'collections.abc.Collection'>, <class 'collections.abc.Iterable'>, typing.Iterable)
annotation

alias of Array

parse_annotation(annotation, /) JSONSchemaType[source]
class NamedTupleParser[source]
annotation

alias of Array

strict: bool = False
can_parse(annotation, /) bool[source]
parse_annotation(annotation: NamedTuple, /) JSONSchemaType[source]
class NewTypeParser[source]
can_parse(annotation, /) bool[source]
parse_annotation(annotation, /) JSONSchemaType[source]
class NullParser[source]
types: Tuple[Type] = (None, <class 'NoneType'>)
annotation

alias of Null

class NumberParser[source]
types: Tuple[Type] = (<class 'float'>, <class 'decimal.Decimal'>, <class 'numbers.Number'>)
annotation

alias of Number

class SetParser[source]
types: Tuple[Type] = (<class 'set'>, <class 'frozenset'>, typing.Set, <class 'collections.abc.Set'>, typing.FrozenSet, typing.MutableSet, <class 'collections.abc.MutableSet'>)
annotation

alias of Array

parse_annotation(annotation, /) JSONSchemaType[source]
class StringParser[source]
types: Tuple[Type] = (<class 'str'>, <class 'bytes'>, ~AnyStr)
annotation

alias of String

class TupleParser[source]
types: Tuple[Type] = (<class 'tuple'>, typing.Tuple)
annotation

alias of Array

parse_annotation(annotation, /) JSONSchemaType[source]
class TypedDictParser[source]
annotation

alias of Object

can_parse(annotation, /) bool[source]
parse_annotation(annotation: TypedDict, /) JSONSchemaType[source]
class UnionParser[source]
can_parse(annotation, /) bool[source]
parse_annotation(annotation, /) JSONSchemaType[source]