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.
- 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 usedlocals – 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¶
- parse_annotation(annotation, /) JSONSchemaType [source]¶
Type-specific parsers.
- class AnyParser[source]¶
- types: Tuple[Type] = (typing.Any,)¶
- annotation¶
alias of
JSONSchemaObject
- class ConstantParser[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'>)¶
- parse_annotation(annotation, /) JSONSchemaType [source]¶
- class EnumTypeParser[source]¶
- types: Tuple[Type] = (<enum 'Enum'>,)¶
- strict: bool = False¶
- parse_annotation(annotation: Type[Enum], /) JSONSchemaType [source]¶
- class EnumValueParser[source]¶
- types: Tuple[Type] = (<enum 'Enum'>,)¶
- strict: bool = False¶
- parse_annotation(annotation: Enum, /) JSONSchemaType [source]¶
- class ListParser[source]¶
- types: Tuple[Type] = (<class 'list'>, typing.List, typing.Collection, <class 'collections.abc.Collection'>, <class 'collections.abc.Iterable'>, typing.Iterable)¶
- parse_annotation(annotation, /) JSONSchemaType [source]¶
- class NamedTupleParser[source]¶
-
- strict: bool = False¶
- parse_annotation(annotation: NamedTuple, /) JSONSchemaType [source]¶
- class NewTypeParser[source]¶
-
- parse_annotation(annotation, /) JSONSchemaType [source]¶
- class NumberParser[source]¶
- types: Tuple[Type] = (<class 'float'>, <class 'decimal.Decimal'>, <class 'numbers.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'>)¶
- parse_annotation(annotation, /) JSONSchemaType [source]¶
- class TupleParser[source]¶
- types: Tuple[Type] = (<class 'tuple'>, typing.Tuple)¶
- parse_annotation(annotation, /) JSONSchemaType [source]¶
- class TypedDictParser[source]¶
-
- parse_annotation(annotation: TypedDict, /) JSONSchemaType [source]¶