Refactor Python code
Applies to:
Visual Studio
Visual Studio for Mac
Visual Studio Code
Visual Studio provides several commands for automatically transforming and cleaning up your Python source code:
- Rename renames a selected class, method, or variable name.
- Extract method creates a new method from the selected code.
- Add import provides a smart tag to add a missing import.
- Remove unused imports removes unused imports.
Rename
Right-click the identifier you wish to rename and select Rename, or place the caret in that identifier and select the Edit > Refactor > Rename menu command (F2).
In the Rename dialog that appears, enter the new name for the identifier and select OK:
In the next dialog, select the files and instances in your code to which to apply the renaming; select any individual instance to preview the specific change:
Select Apply to make the changes to your source code files. This action can be undone.
- Right-click the identifier you wish to rename and select Rename, or place the caret in that identifier and select the Edit > Refactor > Rename menu command (F2), or select the identifier and press Ctrl+R.
- In the Rename dialog that appears, enter the new name for the identifier and press Enter:
Extract method
Select the lines of code or the expression to extract into a separate method.
Select the Edit > Refactor > Extract method menu command or press Ctrl+R > M.
In the dialog that appears, enter a new method name, indicate where to extract it to, and select any closure variables. Variables not selected for closure are turned into method arguments:
Select OK, and the code is modified.
Select the lines of code or the expression to extract into a separate method.
When you place the caret on code sample, Visual Studio provides a smart tag (the wrench icon to the left of the code).
In the dialog that appears, select Extract for closure to be turned into method arguments:
Select OK and the code is modified.
Add import
When you place the caret on an identifier that lacks type information, Visual Studio provides a smart tag (the light bulb icon to the left of the code) whose commands add the necessary import or from ... import statement.
Visual Studio offers import completions for top-level packages and modules in the current project and the standard library. Visual Studio also offers from ... import completions for submodules and subpackages as well as module members. Completions include functions, classes, or exported data. Selecting either option adds the statement to at the top of the file after other imports, or into an existing from ... import statement if the same module is already imported.
Visual Studio attempts to filter out members that aren't defined in a module, such as modules that are imported into another but aren't children of the module doing the importing. For example, many modules use import sys rather than from xyz import sys, so you don't see a completion for importing the sys from other modules even if the modules are missing an __all__ member that excludes sys.
Similarly, Visual Studio filters functions that are imported from other modules or the built-in namespace. For example, if a module imports the settrace function from the sys module, then in theory you could import it from that module. But it's best to use import settrace from sys directly, so Visual Studio specifically offers that statement.
Finally, suppose something would normally be excluded but has other values that would be included (because the name was assigned a value in the module, for example); in that case, Visual Studio still excludes the import. This behavior assumes that the value shouldn't be exported because it's defined in another module. Thus another assignment is likely to be a dummy value that is also not exported.
Remove unused imports
When writing code, it's easy to end up with import statements for modules that aren't being used at all. Because Visual Studio analyzes your code, it can automatically determine whether an import statement is needed by looking at whether the imported name is used within the scope after the statement occurs.
Use right-click anywhere in the editor and select Remove Imports, which gives you options to remove from All Scopes or just the Current Scope:
When you place the caret on the import that lacks usage, Visual Studio provides a smart tag (the light bulb icon to the left of the code) whose commands remove the necessary Remove unused import statement:
Visual Studio then makes the appropriate changes to the code:
Note
Visual Studio doesn't account for control flow. Using a name before an import statement is treated as if the name was in fact, used. Visual Studio also ignores all from __future__ imports, imports that are performed inside of a class definition, as well from from ... import * statements.
Feedback
Submit and view feedback for