-
Notifications
You must be signed in to change notification settings - Fork 74
PyCharm
If this is your first time using Sourcery you'll need to set up a Sourcery account. You can create an account with your GitHub account, Google account, or your email address.
- Open the "File/Settings/Preferences" menu (just "Preferences" on Mac)
- Select "Plugins"
- Click the "Marketplace" tab
- Search for Sourcery
- Click "Install"
Sourcery needs a token to run in PyCharm. To get your token, just log into your Sourcery account and copy the token on the page.
Then:
- Open the PyCharm Preferences menu
- Click into the Sourcery menu item
- Paste in the token
Sourcery will run in the background whenever you're working on a Python file. When Sourcery detects a refactoring opportunity it will underline the first line of code it recommends that you change. Hover over the underlined line to get an explanation of the change along with a diff of the proposed changes to your code.
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
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
We know there might be files or paths you don't want Sourcery to refactor - especially things like third party libraries. If you want Sourcery to ignore these you can add a file or a path to the ignore section in the .sourcery.yaml
file in your project directory like this:
```
ignore:
- data/*
- .venv/*
- '*_test.py' # Note that any strings beginning with * must be quoted
```
Sourcery gives every one 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 in the Sourcery window. By default this is set for functions with a quality score under 25%, but you [can configure this](Sourcery Configuration).
Sometimes you might see a Sourcery suggestion you don't want to accept. You can ignore these suggestions in one of three ways:
- 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. - Select
Skip Suggested Refactoring
from the drop down menu when you press the quickfix shortcut or click on the lightbulb icon.
You can also configure Sourcery to never suggest a particular refactoring. For details see the Sourcery Configuration page.
There are more advanced ways you can improve your code with a Sourcery Pro or Sourcery Team subscription:
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
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 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.
Please visit our newer docs at https://docs.sourcery.ai