Inline
Shortcut: CtrlAlt0N
The Inline refactoring lets you reverse the extract refactoring for a method or a variable.
Place the caret at the code fragment you want to inline.
Press CtrlAlt0N. Alternatively, right-click the code fragment and go to the Refactor menu. Select the inline refactoring that you need.
In the Inline dialog, specify the inlining options.
Preview and apply changes.
Inline Variable refactoring replaces redundant variable usage with its initializer.
The variable must be initialized at declaration. If the initial value is modified somewhere in the code, only the occurrences before modification will be inlined.
Before | After |
---|---|
|
|
Inline Method results in placing method's body into the body of its callers. When you initiate inline refactoring for a method or function, PyCharm prompts to choose whether to remove the method declaration after the refactoring or keep it intact. Depending on your choice, the refactoring results vary.
Before | After |
---|---|
|
|
Before | After |
---|---|
|
|
When PyCharm discovers several occurrence of the method or function to inline, it enables refactoring preview. Click the Preview button in the Inline Refactoring dialog to evaluate the occurrences.
Click the Do Refactor button to complete inlining.
To inline a particular invocation, place the caret at it, press CtrlAlt0N, and select the Use this invocation only and keep the declaration option in the Inline Refactoring dialog.
PyCharm doesn't not support inline refactoring for the following categories of methods and functions:
Functions that are used as a decorator
Functions that are used as a reference
Functions that use any argument unpacking
Generators
Async functions
Constructor calls
Builtin functions
Functions with decorators
Recursive functions
Functions with * arguments
Overridden functions
Functions with global variables
Functions with nonlocal variables
Functions with another function declaration
Functions that interrupt control flow
When you try to perform refactoring for any of these functions or method, the warning message will be shown:
Thanks for your feedback!