Package: typelanguage
https://github.com/kennknowles/python-typelanguageThis package provides a type language for communicating about Python
programs and values. Humans communicating to other humans, humans
communicating to the computer, and even the computer communicating to
humans (via type inference and run-time contract checking).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 a type language.yes- Parsing and printing.yes- Monitoring of type adherence for monomorphic types.yes- “Any” type for easily saying exactly where things get really
dynamic.upcoming- Monitoring of type adherence for polymorphic types.upcoming- Generation of constraints between types in a program.upcoming- Best-effort inference of suitable types.upcoming- Refinement types with hybrid type checking.The TypesThis type language is built from the following concepts:Named types:int,long,float,complex,str,unicode,file,YourClassNameHere, …List types:[int],[[long]], …Tuple types:(int, long),(float, (int, Regex)), …Dictionary types:{string: float},{ (str, str) : [complex] }, …Union typesint|long|float,str|file, …The “any” type,??, for when a value is too complex to describe
in this language. May be an indication that a piece of code is
metaprogramming or should be treated with gradual typing.Function types:str->int(int)->int(int, int)->int( (int, int) )->int( str|file )->SomeClass(int, *[str])->[(str, int)](int, *[int],**{int:str})->strObject types:object(self_type, field1: int, field2: str,...)Polymorphic types (where~a,~b,~crange over any other
type)~a->~a[~a]->[~a]( (~a, ~b) )->~aTypes as ContractsThe moduletypelanguage.enforcecontains functions for using these
types as run-time monitors.Applied directly:>>> check('{string: int}', {"hello" : "there"})More interestingly, automatically protecting a function from bad input,
for example, putting better error checking on Python’sunicode/strinteractions.>>> '\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 unicodeEventually, the notion ofblamemay be usefully incorporated, for
pointing out which piece of code or agent in a distributed setting is
responsible for the undesirable value.Type InferenceIn the spirit of Python and dynamic languages, type inference is
best-effort. It works like so:By traversing the code, we can discover a bunch of constraints
between types in different bits.Some of these constraints are going to be very easy to solve, so we
can just propagate the results.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 exploreThere are many other projects that check contracts or types for Python
in some way or another, but none makes communication their primary goal,
with the possible exception of pySonar. As such, they make different
design choices. Some are research projects or prototypes – this is not.
This is a library meant for use.PEP 316(deferred)RPythonandPyPy(compilation-oriented)pySonarandmini-pysonar(way cool)Pyntchtypecheckerpycontractpython-dbcandone
pyDBCandanother
pydbcandyet another
pyDBCpython-type-inference(no code, but has a great list of papers and even more tools)And since dynamic languages are much of a muchness, it is worthwhile
seeing what is happening elsewhere, though again very few projects
emphasize the types themselves as fun, interesting and useful, only that
the code has them.Contracts in
RacketandTyped RacketTypescriptakaa slightly
gradually-typed
JavascriptandJavascript++(sort of
gradually-typed Javascript) andjavascript-contractsandcernyEste(statically-typed
coffeescript) andUberscript(gradually-typed coffeescript) andcontracts.coffeeContracts.rubyI’m omitting the billion typed languages that compile to Javascript
because those are just typed languages compiler to the assembly language
of the web.Finally, if you want to actually grok types, then contracts, then types
and contracts together, then types and dynamic types together, thenpolymorphictype as contracts and dynamic types together, then type
inference for such systems, try this chronological series of reading.*Types and Programming
Languages*by Benjamin
Pierce.Contracts for higher-order
functionsby Robert Bruce Findler & Matthias Felleisen. ICFP 2002.Hybrid type
checkingby
Kenneth Knowles & Cormac Flanagan. TOPLAS 2010. (expanded and
corrected from POPL 2006)Gradual typing for functional
languagesby Jeremy Siek & Walid Taha. Scheme workshop 2006.Gradual Typing for
Objectsby Jeremy
Siek and Walid Taha. ECOOP 2007.Type reconstruction for general refinement
typesby
Kenneth Knowles & Cormac Flanagan. ESOP 2007.Relationally-parametric polymorphic
contractsby Arjun Guha, Jacob Matthews, Robert Bruce Findler, and Shriram
Krishnamurthi. DLS 2007.Gradual typing with unification based
inferenceby Jeremy
Siek and Manish Vachharajani. DLS 2008.Blame for
allby Amal Ahmed, Robert Bruce Findler, Jacob Matthews, and Philip
Wadler. STOP 2009.Always available static and dynamic
feedbackby Michael Bayne, Richard Cook, and Michael D. Ernst. ICSE 2011.The ins and outs of of gradual type
inferenceby Aseem
Rastogi, Avik Chaudhuri, and Basil Hosmer. POPL 2012.ContributorsKenn Knowles(@kennknowles)Copyright and LicenseCopyright 2012- Kenneth KnowlesLicensed 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 athttp://www.apache.org/licenses/LICENSE-2.0Unless 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.