Preserve the decorated function's identity in timeit - #987
Merged
Conversation
`timeit`'s inner closure had no `functools.wraps`, so every decorated method
reported the wrapper's own `(*args, **kw)` signature and the name "wrapped",
and lost its docstring:
>>> CausalTreeRegressor.bootstrap_pool.__name__
'wrapped'
>>> inspect.signature(CausalTreeRegressor.bootstrap_pool)
(*args, **kw)
`CausalTreeRegressor.bootstrap_pool` is the decorator's only user in the
package, so that is the method affected today. Anything introspecting it sees
the wrong thing -- `help()`, Sphinx autodoc, and scikit-learn's parameter
discovery among them.
Found while adding a signature-driven deprecation shim for #854, which could
not see `bootstrap_pool` for exactly this reason. The fix stands on its own and
is unrelated to that work, so it is split out here.
Three tests: signature/name/docstring preservation on a synthetic decorated
function, `bootstrap_pool` pinned specifically, and a guard that the decorator
still returns the wrapped result. The first two fail on master.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
Proposed changes
timeit(causalml/inference/tree/utils.py) wraps the decorated function in a closure with nofunctools.wraps, so the decorated method loses its name, docstring and signature:CausalTreeRegressor.bootstrap_poolis the decorator's only user in the package, so that is the method affected today. Anything that introspects it sees the wrong thing —help(), Sphinx autodoc, and scikit-learn's parameter discovery among them.One line of
functools.wrapsrestores it.I found this while adding a signature-driven deprecation shim for #854 (#975): the shim could not see
bootstrap_poolbecause its signature read(*args, **kw). The fix stands on its own and has nothing to do with argument order, so it's split out here rather than left buried in that PR. #975 also carries it; whichever lands first, the other merges cleanly.Types of changes
Tests
Three cases added to
tests/test_utils.py:__name__and__doc__preservation on a synthetic decorated functionCausalTreeRegressor.bootstrap_poolpinned specifically, so a regression is visible where it mattersThe first two fail on master (
assert 'wrapped' == 'bootstrap_pool') and pass with the fix.pytest tests/test_causal_trees.py tests/test_utils.pyis 39 passed;black --checkclean.