Last active
April 26, 2019 13:06
-
-
Save erickpeirson/54c9a543a97c1ae4e55a8a079d22eee0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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