Skip to content

Parameter object refactoring

Introduce Parameter Object turns a long parameter list into a single object parameter. Inline Parameter Object performs the reverse transformation when the object no longer carries enough value to stay separate.

How to invoke

  • Place the caret inside a function and use Refactor | BetterPy: Introduce Parameter Object....
  • Place the caret on a function that takes an inlineable parameter object and use Refactor | BetterPy: Inline Parameter Object....
  • When the gutter feature is enabled, a parameter-object gutter icon can start the introduce flow on eligible functions.

Introduce example

def create_order(customer_id: int, product_id: str, quantity: int, urgent: bool):
    ...

can become:

from dataclasses import dataclass


@dataclass
class CreateOrderParams:
    customer_id: int
    product_id: str
    quantity: int
    urgent: bool


def create_order(params: CreateOrderParams):
    ...

Call sites are rewritten to construct the parameter object:

create_order(CreateOrderParams(customer_id, product_id, quantity, urgent))

Inline example

@dataclass
class SearchParams:
    query: str
    limit: int


def search(params: SearchParams):
    return client.search(params.query, params.limit)

can become:

def search(query: str, limit: int):
    return client.search(query, limit)

What it supports

  • Dataclass, NamedTuple, TypedDict, pydantic.BaseModel, and attrs parameter objects.
  • Per-invocation class name, parameter name, and field selection.
  • frozen, slots, and kw_only options where the selected base type supports them.
  • Rewriting function bodies and known call sites, including cross-file and inheritance-aware call sites.
  • Preserving type annotations, scalar defaults, and supported mutable defaults.
  • Inline refactoring for recognized dataclass, NamedTuple, TypedDict, Pydantic, and attrs shapes.

What it deliberately avoids

  • Functions whose parameters or usages cannot be rewritten predictably.
  • Highly dynamic call sites where argument mapping is ambiguous.
  • Parameter objects that are not recognized as a supported shape.
  • Blindly deleting object classes when usages remain outside the refactoring scope.

Calls that unpack extracted arguments with *args or **kwargs are rejected rather than partially rewritten. Review custom decorators and string-based parameter documentation after refactoring; BetterPy intentionally does not rewrite arbitrary strings or comments.

Notes

  • The feature is incubating. Preview the refactoring and review the changed files before committing the result.
  • The feature is gated by the Parameter object refactoring setting under Settings | BetterPy | Refactoring & code transformations | Parameter Object.