Fix inconsistent function inspection for @decorated functions #2245
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.
Description
Resolves #2240
When a function is decorated, the decorator often wraps the original function with additional logic. Some decorators, such as
@pandera.check_output
, usefunctools.wraps
, which preserves the original function’s metadata. This allows inspection tools likeinspect.getsource
to return the code of the original function instead of the wrapper.However, decorators that do not use
functools.wraps
replace the original function entirely with the wrapper. As a result, inspection tools will return the decorator code instead of the original function's code, which can cause confusion.This PR addresses this inconsistency by:
Checking if the decorated function has the
__wrapped__
attribute (set byfunctools.wraps
) and returning the original function if it exists.If the function does not have
__wrapped__
falling back to inspecting the closure to retrieve the original function, if available.QA notes
demo_project
, replace feature_engineering/nodes.py with the below files which consists of both user created decorator and pandera decorator. Runkedro viz
with the old code and you will see how one @pandera.decorator displays decorator when you doShow Code
and the user created decorator displays the original code.Click to expand the the nodes.py file
Checklist
RELEASE.md
file