Giter Site home page Giter Site logo

kennknowles / python-rightarrow Goto Github PK

View Code? Open in Web Editor NEW
59.0 8.0 7.0 161 KB

A language for describing Python programs with concise higher-order annotations like "(a -> a) -> [a] -> [a]" but don't you dare call them "types" :-)

License: Apache License 2.0

Python 100.00%

python-rightarrow's Introduction

 _          _       _     _                               
 \\        (_)     | |   | |                              
  \\   _ __ _  __ _| |__ | |_ __ _ _ __ _ __ _____      __
   \\ | '__| |/ _` | '_ \| __/ _` | '__| '__/ _ \ \ /\ / /
    \\| |  | | (_| | | | | || (_| | |  | | | (_) \ V  V / 
     \\_|  |_|\__, |_| |_|\__\__,_|_|  |_|  \___/ \_/\_/  
              __/ |                                      
             |___/                                       

==========================

https://github.com/kennknowles/python-rightarrow

Build status

This library provides a language for concise higher-order annotations for Python programs, inspired by the syntax for higher-order contracts and types in advanced languages. Functionality such as run-time checking, static checking, inference of annotations, and documentation generation all exist here in various stages of completeness.

This project has a "duck-typed" status: Whatever you can use it for, it is ready for :-)

Here is a more concrete list of implemented and intended features:

  • yes - Definition of the language.
  • yes - Parsing and printing.
  • yes - Run-time monitoring of adherence for monomorphic annotations.
  • upcoming - Monitoring of adherence for polymorphic annotations.
  • upcoming - Documentation generation.
  • upcoming - Generation of constraints between annotations in a program.
  • upcoming - Best-effort inference of suitable annotations.
  • upcoming - More precise annotations to support full higher-order design-by-contract.

The Annotations

This language is built from the following concepts:

  • Named types: int, long, float, complex, str, unicode, file, YourClassNameHere, ...
  • Lists: [int], [[long]], ...
  • Tuples: (int, long), (float, (int, Regex)), ...
  • Dictionaries: {string: float}, { (str, str) : [complex] }, ...
  • Unions: int|long|float, str|file, ...
  • "Anything goes": ??
  • Functions, after which this library is named :-)
    • str -> int
    • (int) -> int
    • (int, int) -> int
    • ( (int, int) ) -> int
    • ( str|file ) -> SomeClass
    • (int, *[str]) -> [(str, int)]
    • (int, *[int], **{int: str}) -> str
  • Objects: object(self_type, field1: int, field2: str, ...)
  • Polymorphic types (where ~a, ~b, ~c range over any other type. Syntax subject to change; no preference really)
    • ~a -> ~a
    • [~a] -> [~a]
    • ( (~a, ~b) ) -> ~a

Run-time checking

The module rightarrow.enforce contains functions for using these annotations as run-time monitors.

Applied directly:

>>> check('{string: int}', {"hello" : "there"})

Wrapping a function to protect it from funky input is more interesting. For example, putting better error checking on Python's unicode/str interactions (at least in Python 2)

>>> '\xa3'.encode('utf-8')
...
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa3 in position 0: ordinal not in range(128)

>>> @guard('unicode -> str')
... def safe_encode(s):
...    return s.encode('utf-8')

>>> safe_encode(u'hello')
'hello'
>>> safe_encode('\xa3')
TypeError: Type check failed: ? does not have type unicode

If you are familiar with notions of "blame" for higher-order contracts, pull-requests welcome :-)

Inferring Annotations

Inference is a work-in-progress, definitely planned, and almost certainly will always be "best effort" only. It works like so:

  1. By traversing the code, we can discover a bunch of constraints between different missing annotations.
  2. Some of these constraints are going to be very easy to solve, so we can just propagate the results.
  3. Some of these constraints are not going to be practical to try to solve, so we can just drop them or insert some enforcement code if we like.

More to explore

There are many other projects that check contracts or types for Python in some way. They are all different, many are prototypes or research projects, and none seem to serve the need that motivates this library. Still, check them out!

And there are cool things happening in other dynamic languages!

And this library draws inspiration from such a huge amount of academic work it cannot possibly all be mentioned, but special thanks to these research efforts, which you might enjoy.

Contributors

Copyright and License

Copyright 2012- Kenneth Knowles

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.

python-rightarrow's People

Contributors

kennknowles avatar yole avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  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.