Skip to content

Getting Started with Sourcery for PyCharm

Installation

  1. In PyCharm, press Cmd+, to open the Preferences dialog. Select Plugins.
  2. Search for Sourcery in the Marketplace tab and click Install.
  3. Click the Restart IDE button.
  4. Click the login button in the top left of the Sourcery window

Seeing Your First Suggestions

When you first install Sourcery you should see a welcome file open in PyCharm. This will include a few instructions for getting started and a demo function with a suggested refactoring.

Any suggestion from Sourcery will be highlighted/underlined. Hover your mouse over the underlined/highlighted section to see a description of the proposed change and a diff of the changes. There are three wys you can interact with a Sourcery suggestion

  • Press Alt Enter when you have selected the highlighted line and then choose to apply the refactoring
  • Click on the lightbulb icon next to the highlighted line and then select to accept the refactoring
  • In the Sourcery tool window, right click on the suggested refactoring, and select Refactor Function

Interacting with a suggestion in Sourcery

You can also try out Sourcery by copying this code into a Python file in PyCharm:

def merge_nested_if(a, b):
    if a:
        if b:
            return c

Sourcery will suggest merging together the nested if statements so you get:

def merge_nested_if(a, b):
    if a and b:
        return c

Accept a Suggestion

To accept a suggestion you can:

  • Press Alt Enter when you have selected the highlighted line and then choose to apply the refactoring
  • Click on the lightbulb icon next to the highlighted line and then select to accept the refactoring
  • In the Sourcery tool window, right click on the suggested refactoring, and select Refactor Function

Sourcery Hub

The Sourcery Hub allows you to quickly configure Sourcery. To open it, find the section on the status bar which says Sourcery and click on it. The hub should then open in your default browser.

The Project Config screen allows you to switch Sourcery rules on and off. When you do so it will edit or create a .sourcery.yaml file in your project. This is the configuration file used by Sourcery.

You can also go to the Your Plan section to get information about your Sourcery account, and the benefits of upgrading to Pro and Team accounts.

Skip a Suggestion

You can skip a Sourcery suggestion once by choosing the skip option from the Sourcery action items menu:

  • Press Alt Enter when you have selected the highlighted line and then choose to skip the refactoring
  • Click on the lightbulb icon next to the highlighted line and then select to skip the refactoring
  • In the Sourcery tool window, right click on the suggested refactoring, and select Skip

You can also tell Sourcery not to suggest anything or a specific type of rule for a function.

  • Add a comment # sourcery skip to a function to skip all rules for that function

  • Add a comment # sourcery skip: <rule-id> to a function to skip the specific rule in that function. A full list of rules and their IDs are available at:

  • Python Rules.

  • JavaScript Rules.

Skipping rules in PyCharm

See Code Quality Metrics

Sourcery gives each of your functions a quality score on 4 different metrics:

  • Complexity
  • Method Length
  • Working Memory
  • Overall Quality

To see the metrics for any function, simply hover your mouse of the line defining the function and the Sourcery metrics will pop up. Each metric will have a numeric score, along with a quick qualitative guide ranging from bad to excellent.

Sourcery's Quality Metrics in PyCharm

Sourcery will also automatically flag functions with too low of an overall quality score. By default this is set for functions with a quality score under 25%, but you can adjust this threshold.

Advanced Features

There are more advanced ways you can improve your code with a Sourcery Pro or Sourcery Team subscription:

Detect Duplicate (and Near Duplicate) Code

Sourcery can help you find duplicate sections of code across your project.

  • Right click on files or folders in the Explorer window
  • Select "Sourcery - Detect clones"
  • A list of duplicate and near duplicate sections of code will appear in the Sourcery tool window.

Sourcery finding duplicate code in a project

By default Sourcery will flag exact duplicates as well as near duplicates (sections of code that almost match, except for some altered parameters).

Extract Duplicate Code Into Methods

Sourcery will automatically detect opportunities for repeated or nearly repeated sections of code within a function to be extracted out into their own methods. When these refactorings are suggested, the new methods will be given a generic name based on the function it was extracted from and you can easily rename it.

Sourcery Extracting Duplicate Methods

Full Project or Multi-File Code Review

You can have Sourcery review multiple files, a folder, or an entire project at once:

  • Right click on the folder or file you're interested in.
  • Select "Inspect Code"
  • Click on the three dots next to the "Inspection Profile"
  • Make sure Sourcery is selected and click "Ok"
  • Suggestions from Sourcery will appear in the inspection results

Configuring Sourcery

See our section on Configuring Sourcery for details on customizing your Sourcery configuration.