Automatic type annotations from JSON schemas
Project description
JSON Schema-powered type annotations
This package provides a way to automatically produce type annotations based
on jsonschema
-schemas.
Not all concepts covered by jsonschema
are expressible within Python typing annotations. However, most things
will work like you'd expect. Most types are translated trivially
(integer
, number
, string
, array
, boolean
and null
).
The interesting type is object
, which is translated into a TypedDict
.
Warning: This is based on the mypy plugin system, which is stated to have no backwards compatibility guarantee. New versions of mypy might not be supported immediately.
Note: This is a maintained fork of erickpeirson's original start
on this project. The original repo seems to be abandoned and its current state is not functional. Make sure to install
the right package from PyPI, jsonschema-typed-v2
Example
A JSON schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://foo.qwerty/some/schema#",
"title": "Foo Schema",
"type": "object",
"properties": {
"title": {
"type": "string"
},
"awesome": {
"type": "number"
}
},
"required": ["title"]
}
A TypedDict:
from typing import TYPE_CHECKING
from jsonschema_typed import JSONSchema
data: JSONSchema["path/to/schema.json"] = {"title": "baz"}
if TYPE_CHECKING:
reveal_type(data) # Revealed type is 'TypedDict('FooSchema', {'title': builtins.str,
# 'awesome'?: Union[builtins.int, builtins.float]})'
data["description"] = "there is no description" # TypedDict "FooSchema" has no key 'description'
data["awesome"] = 42
data["awesome"] = None # Argument 2 has incompatible type "None"; expected "Union[int, float]"
You can also get types of parts of a schema, as well as types of elements in arrays. Take a look at the test cases for more examples of usage.
Installation
pip install jsonschema-typed-v2
You also need to enable the plugin(s) in your mypy.ini
configuration file:
# mypy.ini
[mypy]
plugins = jsonschema_typed.plugin, jsonschema_typed.optional_typed_dict
# Due to a quirk of how these type hints are generated, mypy's caching breaks.
# Disabling caching might be required.
cache_dir = /dev/null
Requirements
The above installations resolves the dependencies, which consist of mypy
and jsonschema
(naturally).
Testing has been done with versions:
- mypy==0.761
- jsonschema==3.2.0
Probably some older versions will also work. Report an issue if you need other versions.
Limitations
additionalProperties
doesn't really have an equivalent in TypedDict.- The
default
keyword is not supported; but see: https://github.com/python/mypy/issues/6131. - Self-references (e.g.
"#"
) can't really work properly until nested forward-references are supported; see: https://github.com/python/mypy/issues/731.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file jsonschema_typed_v2-0.8.0-py3-none-any.whl
.
File metadata
- Download URL: jsonschema_typed_v2-0.8.0-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1461a31e8c4a36f22a2384032492b51b3079af1995b289cc81efbdcbce3e5dc |
|
MD5 | f741a3436931579af66c7c4acc466c11 |
|
BLAKE2b-256 | 817a2957ce3853329c6cfee06bf32cf98763d93c7a3b73a0c145a9eae5f7485a |