Refactoring enhancements in PyCharm 2.7

PyCharm 2.7 now supports the following new refactoring features:

  • Extract Parameter
  • Extract on substrings
  • Create Parameter quick-fix on unresolved reference
  • Change Method Signature

The Extract Parameter refactoring allows you to add a new parameter to a function declaration and update the function calls in your project. Use it to put the result of the selected expression into the function argument list.

This feature is available to be performed “in-place” by default, or through a dialog.
To perform “in-place” refactoring, highlight an expression or place the caret on it and press Ctrl+Alt+P:

Before

After

Note: For more details, please see “Extract Parameter” in Help.

As shown in the example above, now PyCharm is capable of performing the extract refactorings on substrings. Now it is possible to use all the extract refactorings, such as extract constant/field/variable/parameter and other, on substrings.  This is supported for:
• parts of strings with no formatting.
• parts of strings with concatenation via “+”.
• parts of strings with “%” - old-style formatting , including “%()”.
• parts of strings with “str.format()” – new-style formatting, including replacement fields.

Here is an example of the extract variable on a substring with the new-style formatting:

In case of an unresolved symbol, you can easily apply the Create Parameter quick-fix for that, making it a parameter of the function.

Before:

After:
Note: For more information on quick-fixes, please refer to “Resolving Problems” in Help.

 

 

With Change Method Signature you are able to refactor a signature of the selected method and update its calls in your project.

To try it, in the editor place the caret on a function name and press Ctrl+F6.
Alternatively, select Refactor > Change Signature from the main menu to open the same dialog:

With this dialog you can:
• Change the function name.
• Add new parameters and remove the existing ones. Note that you can also add a parameter using the dedicated Extract Parameter refactoring.
• Reorder parameters.
• Change parameter names.

After applying changes, a new code will be generated with the new signature propagated through the method call hierarchy:

We hope these new refactoring features will improve your user experience with PyCharm.

Develop with pleasure!
JetBrains PyCharm Team

This entry was posted in Cool Feature. Bookmark the permalink.

4 Responses to Refactoring enhancements in PyCharm 2.7

  1. Antonel Pazargic says:

    Great support for python core.

    Is the Extract on substrings also available in IntelliJ java? I use the rest of the presented refactoring features on daily basis in IntelliJ Idea, but I am not aware of extract on substrings refactor.

    Regards

  2. Dmitry says:

    I can’t find “Extract Parameter” refactoring in IDEA 12.0.4 with Python plugin 2.10.0. Screenshot.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>