Sourcery is the pair programmer who will help you improve your code. It reviews and improves 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 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.
Check out our documentation for more information on how to use Sourcery.
Sourcery is free to use for open source projects.
To use Sourcery on non open sourced projects you'll need a Sourcery Pro or Sourcery Team subscription. To get a 14 day free trial to Sourcery Pro, sign up for an account on the Sourcery site.
Real-time refactoring suggestions
Hover your mouse over the underlined section of code to see the changes Sourcery suggests and to 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 improvements and rules that Sourcery can make. The current list of rules Sourcery checks for 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 files you have open. You can get an overview of all the suggestions Sourcery has in the Problem window.
Set up your own rules - or use public rulesets
Sourcery is widely extendable so that you can have it check for whatever types of best practices you care about within your code.
Sourcery reads rules from a .sourcery.yaml configuration file that you can set up in your project directory - then it will look to apply those rules across the full project. To get started we’ve set up a quick tutorial and you can dive deeper in our full documentation
You can also opt into publicly available sets of rules to help improve your code. Check out the Google Python Style Guide and see how you can add it to Sourcery.
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 review 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 suggestions it found in the Problems window.
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.
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.
Sourcery is fully free for Open Source projects, the CI and Pre-Commit Hook options require a Sourcery Team subscription for private projects. You can sign up for a Team plan here.
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!
Not everyone's coding style is the same, so we know you won't always want to accept every refactoring Sourcery suggests.
Sourcery reads configuration settings from two sources:
You can use the project-specific config file to set project-specific settings, and the user-specific config file to set settings that apply to all projects. The project-specific configuration takes precedence over the user configuration.
The config file uses YAML syntax. If you are new to YAML check out "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 rules to be shown
You can choose to have Sourcery never show you a specific type of rule by adding it to a list under the
Individual 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.
To make this even easier, we’ve set up the Sourcery Hub. Click on the Sourcery button in the status bar to bring up the Hub, click on settings, and you can toggle on and off individual types of suggestions
You can also choose to have Sourcery not review 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
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.