Skip to main content

Schema2type

License PyPI Python version

Interact with JSON and YAML content through custom Python classes. How it works:

  • You specify the desired or expected format of your content using a JSON Schema or OpenAPI Specification (version 3).
  • Schema2type creates dynamic Python types for all the (sub-)schemas described by that schema/specification.
  • You can make use of these types from anywhere in your code to interact with the JSON or YAML content.
  • Schema2type also generates so-called stub files, which allow you to make use of your IDE's auto-complete functionality for the dynamically created types (this is arguably the most convenient feature of this module and works with both PyCharm and VS Code).

JSON/YAML content can be read from a file, but it can also be content obtained through any other means, such as through an HTTP request.

Example

If you frequently interact with OpenAPI Specifications, you might want make use of dynamic classes based on the JSON schema that describes the format of an OpenAPI Specification (indeed, "a specification of a specification document" is rather meta, but very useful in this case).

import prance
from my_schema_classes.openapi import RootObject as OpenAPISpecification

# resolve references in the specification with the prance module
resolved_specification = prance.ResolvingParser('/path/to/pet_store_openapi_spec.yml').specification

# create a schema-based object from the YAML content
specification_object = OpenAPISpecification(**resolved_specification)

# access information from the specification with auto-complete to guide you along the way:
print(specification_object.info.title)                       # > Swagger Petstore - OpenAPI 3.0
print(type(specification_object.components))                 # > <class 'schema2type.Components'>
pet_schema = specification_object.components.schemas['Pet']
print(type(pet_schema))                                      # > <class 'schema2type.Schema'>
print(pet_schema.required)                                   # > ['name', 'photoUrls']

Compatibility

At the time of writing, schema2type has been tested with schemas specified in the following formats/documents:

Disclaimer

This module is currently in development. At the moment, it can handle most, but not all situations when it comes to parsing your JSON schema or OpenAPI specification. Use at your own risk of running into any potential errors. Support for oneOf, allOf, and anyOf schemas is still limited.

How to install

Schema2type is available from PyPI, and can be installed via pip:

pip install schema2type

Usage

First, generate the stub files based on your schema or specification:

mkdir "/path/to/my_package/my_module/schema_classes"
schema2type gen-stubs --out "/path/to/my_package/my_module/schema_classes" --name "my_schema_name" "/path/to/my_package/a_schema.yml" json_schema

Then, import the schema based classes from within your module wherever you need them:

from my_package.schema_classes.my_schema import *

License

Copyright 2021 Mokkit Oy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Metadata

Release files for schema2type 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for schema2type 0.0.3
File Size Uploaded Provenance Actions
schema2type-0.0.3.tar.gz 11.4 kB Without provenance Details for schema2type-0.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for schema2type 0.0.3
File Interpreter ABI Platform Actions
schema2type-0.0.3-py3-none-any.whl Python 3 none any Details for schema2type-0.0.3-py3-none-any.whl

Total release size: 27.0 kB

Release files / schema2type-0.0.3.tar.gz

Download URL schema2type-0.0.3.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
bc1cbd32c30e23b5948ec0e4f4a2279d2ea26022b932ebdf9dfe6cb922b5d6c8
BLAKE2b-256 checksum
How to use checksums
b889a17d2357df2729341abb1ef7a65a2da87934039216d23aef54c4aa624577
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

Release files / schema2type-0.0.3-py3-none-any.whl

Download URL schema2type-0.0.3-py3-none-any.whl
Size 15.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ad83b2c3fa31560f165f72ecf3f997afbd22918634ce5bbf168bca205c35d0b1
BLAKE2b-256 checksum
How to use checksums
31dcbf25437e8c0c67e231d58f52b26f05d9f19c8d04e916b1b1e96bf3e662a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

Security for schema2type 0.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page