Suor / funcy

A fancy and practical functional tools
BSD 3-Clause "New" or "Revised" License
3.35k stars 142 forks source link

rcurry and similar functions do not work with methods of built-in types #108

Open ErisianArchitect opened 2 years ago

ErisianArchitect commented 2 years ago

>>> has_suffix = rcurry(str.endswith)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\funcy\funcs.py", line 57, in rcurry
    n = get_spec(func).max_n
  File "C:\Users\admin\AppData\Local\Programs\Python\Python310\lib\site-packages\funcy\_inspect.py", line 108, in get_spec
    mod = func.__module__
AttributeError: 'method_descriptor' object has no attribute '__module__'. Did you mean: '__reduce__'?```
Suor commented 2 years ago

I'll take a look

Suor commented 2 years ago

Ok, rcurry() and friends behave the same in Python 3.8 and 3.10. And it doesn't work with str.endswith indeed.

Suor commented 2 years ago

funcy fails to introspect methods of builtin types.

Suor commented 2 years ago

The workaround for now is:

has_suffix = rcurry(str.endswith, 2)
Suor commented 2 years ago

Also, str.endswith actually have 4 params, so your example won't work anyway.

Suor commented 2 years ago

Updated docs and readme to recommend specifying n. The issue itself is frozen for now, since I don't see much value in doing proper introspection here.

Suor commented 1 year ago

Note that as a last resort funcy goes to inspect.signature(), which might work or not for the same thing depending on Python version.