Features Tips & Tricks Tutorials

Feature Spotlight: Find Usages & Locate Duplicates in Python Code

Happy Friday everyone,

In the last week’s blog post I covered different refactoring capabilities of PyCharm. Today I’ll continue highlighting different handy features and show you some more tools that help you keep your code under full control.

One of the features that every developer uses on the daily basis is Find/Replace. It’s supported in every code editor, and PyCharm is no exception: Ctrl+F/Ctrl+R. Besides, PyCharm supports a number of subsidiary functions, such as Find Next/Previous Occurrence, Find/Replace in Path, Select all Occurrences, etc. They all are available under the Edit | Find menu item:

find1

While some of these features are pretty common, others really stand apart thanks to PyCharm outstanding code intelligence. Find usages is one of such noticeably smart features that deserves a closer look.

1. Find usages
Just press Alt + F7 on any symbol at the caret ( no matter if the symbol is a class, method, field, parameter, or another statement) and get a list of references grouped by type of usage, module and file. This feature is really fast and gets you first results almost instantly. More results appear as the IDE finds them:

find2

By default the results should be grouped by usage type, if not you can enable this by pressing Ctrl(Cmd for Mac) + Alt + T or by clicking the corresponding button on the sidebar:

find3

2. Settings
If you want to set custom options for the Find Usages algorithm, you can use Shift + Alt + Ctrl + 7 (Shift + Alt + Cmd + 7 for Mac):

find4

3. Quickpopup
To see the results quickly in place, simply press Alt + Ctrl + 7 (Alt + Cmd + 7 for Mac):

find5
4. Highlight overridden methods
Another useful aspect of highlighting usages in PyCharm is that you can easily find the methods that are overridden for a particular class. Just put the caret at the statement and press Shift + Ctrl + F7 (Shift + Cmd + F7 for Mac). If there are multiple classes, you will be asked whose methods to highlight:

find6
5. Highlight usages in File
Sometimes its useful to quickly see all usages of the variable or method within a file right in the editor. Highlight usages in File (Ctrl + Shift + F7) works perfect in this case:

find7

Locate Duplicates

It’s not a secret that one of the most annoying problem of every project is duplicated code. Obviously any developer tries to get rid of such redundancies. Thanks to PyCharm code intelligence, Locate duplicates tool allows you to examine your code and find different code duplicates. This tool works for different languages and is highly configurable.
To use this tool go to Code | Locate Duplicates. It will ask you for an analysis scope specification:

duplicates1

then you can specify some other options before give it a go:

duplicates2

You can set supported languages that will be analysed within your scope as well as other configurable options for fine tuning the analysis.

On the last step after clicking OK, it will show you the list of code duplicates found, as well as the tool to review these duplicates and navigate between them:

duplicates3

After finding duplicates you might want to use one of the refactoring features described in my previous blog post in order to get rid of them.

I hope I’ve been helpful and shed some light on these advanced PyCharm search features.

Have a great weekend and see you next week!
-Dmitry

image description