python/pass-through-function
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| Python | warning | none, edit by hand | All versions |
A function or method that only passes its parameters to one call adds a name without adding behavior. Call the target directly, and delete the wrapper.
def fetch_orders(*args: object, **kwargs: object) -> list[str]: return load_orders(*args, **kwargs)
orders = fetch_orders(region="eu")orders = load_orders(region="eu")What the rule reports
Section titled “What the rule reports”The rule reports two kinds of forwarding wrappers:
- A module level function (bare or decorated only with
@deprecated) whose body is onereturnof a call that forwards only the function's parameters, including*args,**kwargs, keyword-renamed arguments (target(y=x)), and a single-argument wrapper around that call such asfloat(curve(at_price)). - A non-dunder method (bare or decorated only with
@deprecated) whose body is onereturncalling another method onselforclswith only its forwarded parameters.
The rule leaves alone the cases in the list:
- A function that computes something or binds a constant argument, such as
return fetch(url, timeout=30). - A zero-argument factory function such as
def default_config() -> Config: return Config(). - A dunder method such as
__len__or__iter__, a method forwarding to a collaborator attribute such asself._client.fetch(x), or a function decorated by a framework decorator. - A function that returns a chain of two or more
pipecalls, such asframe.pipe(clean).pipe(features), because that function names the steps of a pipeline in the order they run.
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
Update every caller to call the target function or method directly, and delete the pass-through wrapper in the same commit. When the wrapper is passed as a callback, pass the target directly.
Diagnostic message
Section titled “Diagnostic message”This function only passes its parameters to one call. Call that function where this one is called, and delete this one.See all rules in the Python pack.