Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in compose introduced in 0.10.0 #481

Open
Thomasillo opened this issue Mar 13, 2020 · 2 comments
Open

Bug in compose introduced in 0.10.0 #481

Thomasillo opened this issue Mar 13, 2020 · 2 comments

Comments

@Thomasillo
Copy link

Consider a file temp.py :

import toolz
import functools

print("toolz:", toolz.__version__)

def g(x):
    return x

class A(object):

    def __call__(self):

        _fg = toolz.compose(lambda x:x, g)

        class Inner(object):
            fg = _fg
        return Inner()
    
a = A()()
print(a.fg(2))

with toolz 0.9.0:

$ python temp.py
toolz: 0.9.0
2

With toolz 0.10.0:

$ python temp.py
toolz: 0.10.0
Traceback (most recent call last):
  File "temp.py", line 21, in <module>
    print(a.fg(2))
  File "/Users/Thomas/.local/miniconda3/envs/mantik_dev/lib/python3.7/site-packages/toolz/functoolz.py", line 486, in __call__
    ret = self.first(*args, **kwargs)
TypeError: g() takes 1 positional argument but 2 were given

In the second example, g gets called with an additional <__main__.A.__call__.<locals>.Inner object as first argument.

If not a bug, it's at least a change of behavior breaking code using toolz.

The above example is very reduced and may look strange but is totally sensible in the usecase where it's actually used.

@ariebovenberg
Copy link

ariebovenberg commented Mar 13, 2020

This is due to this change #397 (point 3 in the initial comment).

the cause: __get__ was implemented to behave similarly to python functions and lambdas. This was also done for curry.

@Thomasillo if you want the old behavior, you can use staticmethod:

class A:
    myfunc = staticmethod(compose(lambda x: x, lambda x: x))
    mymethod = compose(lambda x: x, lambda x: x)

A().myfunc(3)  # ok
A().mymethod(3)  # not ok

@eriknw thoughts?

edit: fixed code example

@eriknw
Copy link
Member

eriknw commented Mar 13, 2020

Sorry to hear this change affected you @Thomasillo. This affected somebody else too (#460). We try to keep breaking changes in toolz to a bare minimum, so it's time we use semantic versioning.

I think using staticmethod is excellent advice, and should work with both newer and later versions of toolz.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants