PyCharm helps you find repetitive blocks of code in a certain range. This range can be a single file, a project, a module, or a custom scope. Analysis results are displayed directly in the editor or in the dedicated tab of the Duplicates tool window.
Detect duplicates on the fly
PyCharm enables spotting duplicates on the fly. The analysis is performed by means of the Duplicated code fragmentinspection, which you can find under the General node in the Editor | Inspections page of the IDE settings Ctrl+Alt+S.
If you stumble upon an existing duplicate or somehow create one by writing or pasting code, PyCharm reports a problem and suggests quick-fixes that let you navigate to the detected duplicates, or view all of them in the Duplicates tool window.
Configure duplicates analysis
Press Ctrl+Alt+S to open the IDE settings and select Editor | Duplicates.
On the Duplicates page that opens, select the file types, to which duplicates analysis should apply. Additionally, select the Variable or identifier names/Function or field names/Constant values checkboxes to define whether two identical entities that use different names should be treated as duplicates.
For example, if the Variable or identifier names and Function or field names are enabled, the following two functions will be considered duplicate:def demo(a, b, c): return_type_of_sqrt = math.sqrt(b ** 2 - 4 * a * c) root1 = (-b + return_type_of_sqrt) / (2 * a) root2 = (-b - return_type_of_sqrt) / (2 * a) print(root1, root2) def sqrt_func(n, m, k): return_type_of_sqrt = math.sqrt(m ** 2 - 4 * n * k) result1 = (-m + return_type_of_sqrt) / (2 * n) result2 = (-m - return_type_of_sqrt) / (2 * n) print(result1, result2)
Press Ctrl+Alt+S to open the IDE settings and select Editor | Inspections.
Select the Duplicated code fragment inspection under the General node.
In the inspection options, select whether you want to view duplicates only within the same file or across the entire project. In the Do not show duplicates simpler than field for the necessary file type, set the size of duplicated language constructs to be detected. In the Python context, the constructs less than 45 units are ignored by default. Alter the number of units to enable duplicate detection for smaller code fragments. The value of units can be calculated as follows:
units = 2*<number of statements> + <number of expressions>.
Search for duplicates manually (deprecated)
From the main menu, select.
In the Specify Code Duplication Analysis Scope dialog, specify the analysis scope: whole project, current file, uncommitted files (for the projects under version control), or some custom scope. In addition, you can include test sources into the analysis, too.
In the Code Duplication Analysis Settings dialog, select the languages that you want to analyze.
For each language, check the options to define the analysis preferences. For example, you can opt to request identical match for code fragments to be considered duplicates, or specify a certain limit below which the code constructs are not considered duplicates (for example, to avoid reporting each
ifconstruct in the source code).
In the Duplicates tool window, explore the analysis results.
View the list of duplicates in the left pane of the tool window.
View the differences between the found duplicates in the right pane. Use the arrow buttons to place the selected duplicate in one of the sections of the differences viewer and compare fragments of the code.
Navigate to the duplicates in the editor by using the Jump to Source or Show Source context menu commands.
Eliminate duplicates from the source code by applying the Extract method refactoring to the detected repetitive blocks of code that are found and highlighted automatically.