PyCharm
Installing Sourcery
- In PyCharm, press
Cmd+
, to open the Preferences dialog. Select Plugins. - Search for Sourcery in the Marketplace tab and click Install.
- Click the Restart IDE button.
- Go to your Sourcery Dashboard, copy your token, and enter it into the Sourcery Token input in PyCharm.
Getting Started
Getting Your Token
Sourcery requires that you have a token to analyze your code and to give you refactoring suggestions. To get your token go to your Sourcery Dashboard) and copy the token.
Then return to PyCharm and enter the token in the text box that prompts you on install.
If you do not see the Sourcery prompt to enter your token then:
- Open the PyCharm Preferences menu
- Click into the Sourcery menu item
- Paste in the token
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
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
How To
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
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 all refactorings or a specific type of refactoring for a function.
- Add a comment
# sourcery skip
to a function to skip all reactorings for that function - Add a comment
# sourcery skip: <refactoring-id>
to a function to skip the specific refactoring in that function. A full list of refactorings and their IDs are available at Current Refactorings.
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 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.
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.
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.