dobarkod / typedecorator Goto Github PK
View Code? Open in Web Editor NEWA decorator-based implementation of type checks.
A decorator-based implementation of type checks.
As the title says, I get a TypeError: non-void function didn't return a value
when I have a function with the decorator @returns(Nullable(T))
return None (where T
is any type).
Python provides a standard module for emitting warnings. This is a reasonable action to take for a type mismatch issue. Please make this one of the built in options for responding to type mismatches.
I understand a list containing a single element can be checked as the document states as follows:
A list containing a single element, requiring that the value be a list of values, all matching the type signature of that element. For example, a type signature specifying a list of integers would be [int].
I was wondering if it is possible to check for a list containing multiple elements. For example, if a function expects a 2D coordinate, it would require the following type:
[int, int]
More precisely, I would like to check for the following type:
typedecorator.Union((int, int), [int, int])
but that does not seem possible as I'm getting TypeError: Invalid type signature
. If I change my code a bit like:
typedecorator.Union((int, int), list)
it seems working okay but that's not quite what I want.
Using Python version 2.7
When you use type decorator with a function, then try to call that function using multiprocessing's apply_async function, it results in a pickling error. Working example below, this was tested with a clean virtualenv with only typedecorator and the standard libraries installed.
from multiprocessing import Pool
from typedecorator import *
@returns(int)
@params(a=int,b=int)
def f(a,b):
print "Function called"
return a + b
def main():
pool = Pool(processes=4)
pool.apply_async(f,[1,2])
pool.close()
pool.join()
if __name__ == '__main__':
main()
This results in:
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib64/python2.7/threading.py", line 811, in __bootstrap_inner
self.run()
File "/usr/lib64/python2.7/threading.py", line 764, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/lib64/python2.7/multiprocessing/pool.py", line 342, in _handle_tasks
put(task)
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
Removing the type decorations for the function 'f' fixes the problem. So far I really like your solution though, and will be implementing it where possible when moving forward. Keep up the great work!
Hi. Sorry to bother you again. I have the following situation:
class Thing(object):
@params(self=other, thing=Thing)
def do_something(self, thing):
pass
The thing=Thing
gives me an "Unresolved reference error". Is there syntax for this already and I'm just missing it? Thanks.
This would be an extremely useful extension.
Should we support contracts?
It would be straightforward to support contracts by supporting custom callables in type signatures. For example:
@params(a=int, b=lambda b: b != 0)
def div(a, b):
return a / b
However, I find that syntax clunky (for contracts), compared to something like:
@contract
def div(a, b):
return a / b
@div.requires
def div(a, b):
assert isinstance(a, int)
assert b != 0
But this being typedecorator, not a full-blown contracts library, I'm hesitant to grow it in that direction (unless there's popular demand).
Hi,
I'm just starting to play with this package, and it looks fantastic. However, I can't find any information about making it work with a class method.
For example:
class Foo(object):
@returns(int)
@params(a=int, b=int):
def add(self, a, b):
return a + b
returns a TypeError. And if I change it to @params(self=self, a=int, b=int)
I get a NameError (name 'self' if not defined).
So what is the proper way to annotate the method signature of an object method?
Thanks!
class Foo:
@typed
def validate(self, string: str): # TypeError: Annotation doesn't match function signature
...
It'd be great if this would not give an error, but just limit the checking to the annotated variables.
A little feature request here: it would be great if it was possible to validate a class passed via function parameter using issubclass().
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.