Fix curry/rcurry/autocurry crashing on unbound built-in methods - #167
Closed
binggao1230 wants to merge 1 commit into
Closed
binggao1230 wants to merge 1 commit into
binggao1230 wants to merge 1 commit into
Conversation
Unbound methods of built-in types such as str.endswith, str.startswith, and list.append are method_descriptor objects. inspect.signature() cannot introspect them on CPython, so get_spec() raised ValueError even when the caller did not supply an explicit n=. Detect this case via __objclass__ (present on all method_descriptor objects) and fall back to a two-positional-argument spec, which matches the most common use-case: self plus one required argument. Callers that need a different arity can still pass n= explicitly. Fixes Suor#108.
Owner
|
What about other methods, which have more than one argument besides self? This code will try to make them 2 args curry too. Also, comments are two verbose. Please prepare a proper PR, not just what coding agent does without much thinking. |
Author
|
You're right. This fallback is not a proper fix: it treats every non-introspectable builtin method descriptor as if n=2, which is not generally true, and even the motivating str.endswith case has optional start/end parameters. I also saw the older issue discussion recommending explicit n rather than partial builtin-method introspection. I'm closing this PR rather than pushing a narrow/incorrect heuristic further. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
curry(),rcurry(), andautocurry()crash withValueErrorwhen passedan unbound method of a built-in type such as
str.endswith,str.startswith,or
list.append:These functions are
method_descriptorobjects.inspect.signature()cannotintrospect them on CPython, and the fallback in
get_spec()raisedunconditionally. Workaround was to pass
n=explicitly (rcurry(str.endswith, 2)),but that shouldn't be required. Reported in #108.
Fix
method_descriptorobjects always expose__objclass__(the class they belongto). When
signature()raises and__objclass__is set, fall back to atwo-positional-argument spec — one for the implicit
selfand one for theprimary method argument. This covers the common use-case:
Methods that need a different arity can still use the explicit
n=form.Changes
funcy/_inspect.py: handle__objclass__in thesignature()except branchtests/test_funcs.py: addtest_curry_builtin_methodcovering the new behaviourAll 206 tests pass.