Code Monkey home page Code Monkey logo

schema-provider.py's Introduction

schema-provider

Addressing python/typing#182 : generating type stubs and type validators from a type schema description.

This project is WIP.

Usage

A type schema description file:

\scheme MyJSON = {
    "a" : int | str,
     1  : float,
    "recur" : [MyJSON, *int]
}

generates a type stub file:

from __future__ import annotations
import typing as __t
import typing_extensions as __te

class MyJSON(__te.Protocol):
    @__t.overload
    def __getitem__(self, key: __te.Literal["a"]) -> __t.Union[int, str]: ...
    @__t.overload    
    def __setitem__(self, key: __te.Literal["a"], value: __t.Union[int, str]) -> None: ...
    @__t.overload
    def __getitem__(self, key: __te.Literal[1]) -> float: ...
    @__t.overload    
    def __setitem__(self, key: __te.Literal[1], value: float) -> None: ...
    @__t.overload    
    def __getitem__(self, key: __te.Literal["recur"]) -> GeneratedType1: ...
    @__t.overload
    def __setitem__(self, key: __te.Literal["recur"], value: GeneratedType1) -> None: ...

def check_MyJSON(_: dict) -> MyJSON: ...
     

class GeneratedType1(__te.Protocol):
    @__t.overload
    def __getitem__(self, item: __te.Literal[0]) -> MyJSON: ...
    @__t.overload
    def __setitem__(self, item: __te.Literal[0], value: MyJSON) -> None: ...
    @__t.overload    
    def __getitem__(self, item: int) -> int: ...
    @__t.overload
    def __setitem__(self, item: int, value: int) -> None: ...

and a validation file:

unset = object()
def check_MyJSON(value: dict):
    tmp = value.get("a", unset) 
    if isinstance(tmp, int) or isinstance(tmp, str):
        pass
    else:
        raise SomeExeption(type=MyJSON, path=".a", expect="int | str", got=str(tmp))
    
    ...

schema-provider.py's People

Contributors

thautwarm avatar

Stargazers

haruna avatar Tristan de Cacqueray avatar Lîm Tsú-thuàn avatar Hung-I Wang avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.