Skip to content

Instantly share code, notes, and snippets.

@erickpeirson
Last active April 26, 2019 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erickpeirson/54c9a543a97c1ae4e55a8a079d22eee0 to your computer and use it in GitHub Desktop.
Save erickpeirson/54c9a543a97c1ae4e55a8a079d22eee0 to your computer and use it in GitHub Desktop.
import os
import json
class JSONSchemaPlugin(Plugin):
"""Provides support for the JSON Schema as TypedDict."""
JSONSchema = 'jsonschema_typed.types.JSONSchema'
JSONSchemaBase = 'jsonschema_typed.types.JSONSchemaBase'
def get_type_analyze_hook(self, fullname: str) -> Optional[Callable]:
"""Produce an analyzer callback if a JSONSchema annotation is found."""
if fullname == self.JSONSchema:
def callback(ctx: AnalyzeTypeContext) -> TypedDictType:
"""Generate annotations from a JSON Schema."""
schema_path = ctx.type.args[0].literal_value
full_schema_path = os.path.abspath(schema_path)
with open(full_schema_path) as f:
schema = json.load(f)
return make_type(ctx, schema, full_schema_path)
return callback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment