Skip to content

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")

The rule reports two kinds of forwarding wrappers:

  • A module level function (bare or decorated only with @deprecated) whose body is one return of 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 as float(curve(at_price)).
  • A non-dunder method (bare or decorated only with @deprecated) whose body is one return calling another method on self or cls with 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 as self._client.fetch(x), or a function decorated by a framework decorator.
  • A function that returns a chain of two or more pipe calls, such as frame.pipe(clean).pipe(features), because that function names the steps of a pipeline in the order they run.

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.

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.