Git Product home page Git Product logo

typedecorator's People

Contributors

senko 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  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

typedecorator's Issues

Add a warning mode

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.

Check for a list containing multiple elements

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.

typedecorator and multiprocessing

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!

Cannot use self in method params

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.

Contracts

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

TypeError on class method

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!

Allow annotations to be optional when @typed

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.

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.