Giter Club home page Giter Club logo

anticipate's People

Contributors

batsshadow avatar iivaishnavii avatar six8 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

anticipate's Issues

Required parameters

You should be able to specify required parameters so that @anticipate will fail if there is no value for them.

@anticipate(foo=int, required=['foo'])
def something(foo):
    print foo

Or maybe:

@anticipate(foo=required(int))
def something(foo):
    print foo

Can not adapt to a list of fields

@anticipate(strings=[StringField()])
def get_strings(self, strings):

Results in error:

    def _each(self, func, iterable):
        """
            Runs `func` over each item in the iterable and returns a list.
            Returns an empty list if iterable is `None`.
            """
>       if value is None:
E       NameError: global name 'value' is not defined

This is because the following code uses the wrong variable:

    def _each(self, func, iterable):
        """
        Runs `func` over each item in the iterable and returns a list.
        Returns an empty list if iterable is `None`.
        """
        if value is None:
            return []
        return [func(obj) for obj in iterable]

Anticipating a list results in a generator

When using anticipate for a list of values, it actually provides a generator, not a list. After the generator is iterated a single time, it appears to be empty.

def test_list():
    @anticipate(stringz=[basestring])
    def t(stringz=None):
        assert list(stringz) == list(stringz)

    t([])
    t(['a', 'b', 'c'])

This test fails because list() can only consume stringz a single time.

Based on the notation, one would naturally expect to receive a list rather than a generator. If a generator were desired, some other syntax should be used.

This version of adapt_all resolves the issue:

def adapt_all(iter, to_cls):
    """
    Returns a generator that will adapt all objects in an iterable to `cls`
    """
    return [adapt(obj, to_cls) for obj in iter]

inspect.getargspec() is deprecated since Python 3.0

  /pyroot/lib/python3.8/site-packages/anticipate/decorators.py:35: DeprecationWarning: inspect.getargspec() is deprecated since Python 3.0, use inspect.signature() or inspect.getfullargspec()
    args, _, kwargs, _ = inspect.getargspec(func)

-- Docs: https://docs.pytest.org/en/latest/warnings.html

Remove Python 2 support

  • Remove future library
  • Remove shims for Python 2 functions
  • Add 3.10-dev to .travis.yml to verify future compatibility

inspect.argspec does not work on anticipated functions

anticipate_wrapper does:

  def __get__(self, instance, owner):      
        new_func = self.__call__.__get__(instance, owner)
        update_wrapper(new_func, self)
        return new_func

So when you @anticpate() a method, the method will now be a partial. inspect.argspec does not work on partials so partial should not be used. The following can be used instead:

    def __get__(self, instance, owner):
        if instance is None:
            return self

        def wrap(*args, **kwargs):
            return self.__call__(instance, *args, **kwargs)

        update_wrapper(wrap, self)
        return wrap

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.