Sourcery is the pair programmer who will help you improve your code anytime you're working in Python. It reviews and refactors your code automatically so you can spend more time focused on writing new code and less time cleaning things up.
Sourcery is a VS Code extension to help make all of your Python code cleaner and more readable. Quickly find areas where your code could be refactored, see instant suggestions for improvement, and understand how new changes impact your code quality.
Here are some of the features Sourcery offers to help improve your code:
To start using Sourcery on your code, check out our Getting Started guide.
Real-time refactoring suggestions
While you work, Sourcery will review all of the Python files you have open and look for opportunities to clean up and refactor your code. Once Sourcery finds a potential refactoring it will underline that section of your code.
Hover your mouse over the underlined section of code to see the changes Sourcery suggests and see a diff of the proposed change.
To make the change, just bring up the quick fix menu and choose to accept the change. Sourcery will then apply the change to your code. We're constantly adding new refactorings that Sourcery can make. The current list of the types of refactorings Sourcery can make is available here.
The commands to bring up the quick fix menu depend on your OS & region, but generally they are:
Sourcery reviews all of the Python files you have open. You can get an overview of all the suggestions Sourcery has in the Problem window.
Other Suggested Tools
Continuous code quality feedback
Sourcery gives each of your functions a quality score ranging from 0% (bad) - 100% (good) and also gives you sub-scores on Method Length, Complexity, and Working Memory so that you can figure out how to structure your code as cleanly as possible.
Method Length is a metric is a measure of how long each method is on average. It is based on the number of nodes in the method's Abstract Syntax Tree.
Complexity is a measure of how difficult your code is to read and understand. It is based on these principles:
Working Memory is a measure of the number of variables that need to be kept in your working memory as you read through the code.
Sourcery will warn you if your overall quality score for a function falls below 25%.
You can use Sourcery to refactor a single file, a folder, or your whole project at once.
Right-click on any item in the Explorer window and select "Sourcery → Scan for Refactorings". Sourcery will show you a notification when the scan is done & will show all of the refactorings it found in the Problems window.
Multi-File Analysis requires a Sourcery Pro subscription. To get a month free access to Sourcery Pro, sign up for an account on the Sourcery site.
Duplicate Code Detection
Sourcery can help you find duplicate code and near-duplicate code across your whole project.
Right-click on any item in the Explorer window and select "Sourcery → Detect Clones". Sourcery will show you a notification when it's done scanning for clones & will show you all of the instances of duplication it found in the Problems window.
By default, Sourcery will flag items where at least 3 lines are duplicates or near-duplicates that occur at least twice in the scanned files.
Duplicate Code Detection requires a Sourcery Pro subscription. To get a month free access to Sourcery Pro, sign up for an account on the Sourcery site.
GitHub Pull Request Review
Sourcery can help you speed up code reviews and clean up every new commit by automatically reviewing each of your GitHub pull requests.
To get started, add Sourcery to your GitHub repo. Sourcery will then start reviewing every new pull request automatically!
If you want to clean up a bigger portion of legacy code, Sourcery CLI comes in handy.
With Sourcery CLI, you can also integrate Sourcery with your favorite tools.
CI / Pre-Commit Hook options:
You can use Sourcery to review every new bit of code you and your team are working on by adding Sourcery into your CI or running it as a Pre-Commit Hook.
Sourcery will review every new change and you can set it up to either automatically make changes to your code when it detects opportunities to refactor, or you can just have it notify you when it finds refactorings.
The Sourcery CLI, CI, & Pre-Commit Hook options require a Sourcery Team subscription. You can sign up for a Team plan here.
Not everyone's coding style is the same, so we know you won't always want to accept every refactoring Sourcery suggests.
Generally, for configurations, Sourcery uses configuration settings from
The config file uses YAML syntax. If you are new to YAML checkout "Learn YAML in 5 minutes".
Here is the default set of values set for the file. If a config file isn't set or if a setting isn't specified then Sourcery will use these values:
You can easily configure Sourcery to:
Choosing which refactorings to be shown
You can choose to have Sourcery never show you a specific type of refactoring by adding it to a list under the
Individual refactoring ids are displayed in the plugin suggestions and GitHub Bot comments for easy lookup or you can find a full list in the refactorings section of the docs.
Would mean that the Assign If Expression & De Morgan's Identity refactorings are never shown.
You can also choose to have Sourcery not review or refactor specific files, paths, or individual functions.
To ignore a path or a file, add the paths or files as items in a list underneath the
If you want to just ignore a specific function you can add a
If you want to ignore specific refactorings in a function you can add in
Setting when Sourcery warns about quality issues
By default, Sourcery will show a warning if your quality score drops below 25. You can easily tweak this by changing the value of the
Sign Up For More Features
Do you like how Sourcery improves your code bit by bit? Sign up and get a 30 day free trial of the Pro features. (No automatic renewal, no credit card required.)
After signing up, add your token in your VSCode configuration by going to the VS Code Settings → Extension Settings → Sourcery and adding it to the "Sourcery Token" field.
We also love to hear your feedback.
We've put together a handful of examples if you want to play around with Sourcery and see a bit more what it can do:
Here is an example where Sourcery would chain together multiple refactorings:
Copy these examples into a Python file to see how Sourcery would handle them.
Privacy / Security
All of the analysis we do on your code is done fully locally. We never see any of your code or pass it back to our servers in any way. The only thing we collect is some basic analytics about the types of suggestions we make, and information about exceptions in Sourcery.
This repository includes source code as well as packaged binaries. The MIT license only applies to the source code, not the binaries.