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¶
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:
Inline example¶
@dataclass
class SearchParams:
query: str
limit: int
def search(params: SearchParams):
return client.search(params.query, params.limit)
can become:
What it supports¶
- Dataclass-backed parameter objects by default.
- Alternate parameter-object base types configured in Settings | BetterPy | Parameter Object Refactoring.
- Rewriting function bodies and known call sites.
- Preserving type annotations when the source signature contains them.
- Inline refactoring for simple object-field access patterns.
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.
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.