Skip to content

Latest commit

 

History

History
310 lines (196 loc) · 12.6 KB

README.md

File metadata and controls

310 lines (196 loc) · 12.6 KB

Github Repo VSCode Plugin Twitter Follow

Sourcery

WEBSITE | DOCS | BUGS

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 in VS Code


Sourcery

Sourcery is a VS Code extension to help you move faster and ship code with confidence. Sourcery gives you code reviews anytime you want, so you can get feedback before you need to open a PR. Plus Sourcery's coding assistant helps you speed up repetitive tasks while you work.

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.


Features

Code reviews on demand

Sourcery Code Reviews

Get feedback on your code anytime you want. Sourcery can review your code directly in your IDE, looking at the branch you're currently on or the uncommitted changes you have.

GitHub & GitLab Pull Request Review

Sourcery Reviewing GitHub PRs

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 or GitLab project. Sourcery will then start reviewing every new pull request automatically!

Real-time refactoring suggestions

Refactoring Code with Sourcery

While you work, Sourcery will review all of the Python, JavaScript, and TypeScript files you have open and look for opportunities to clean up and improve your code. Once Sourcery finds a potential improvement it will underline that section of your code.

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:

OS Keyboard Shortcut
Mac Command .
Windows Ctrl .
Linux Ctrl .

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

Sourcery Custom Rules

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

Code Quality Metrics in Sourcery

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:

  • Each break in the linear flow of the code makes it harder to understand
  • When these flow-breaking structures are nested they are even harder to understand

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%.

Multi-File Analysis

Multi-File Analysis & Refactoring in Sourcery

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

Duplicate Code Detection in Sourcery

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.


Configuring Sourcery

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:

ignore: []

rule_settings:
  enable: [default]
  disable: []
  rule_types:
    - refactoring
    - suggestion
    - comment
  python_version: '3.9'

rules: []

metrics:
  quality_threshold: 25.0

github:
  labels: []
  ignore_labels:
    - sourcery-ignore
  request_review: author
  sourcery_branch: sourcery/{base_branch}

clone_detection:
  min_lines: 3
  min_duplicates: 2
  identical_clones_only: false

proxy:
  no_ssl_verify: false

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 refactor: skip section of the .sourcery.yaml file. Just add in the rule ID for the rule you don't want to see and Sourcery will stop suggesting it.

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.

For example:

refactor:
  skip:
    - assign-if-exp
    - de-morgan

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

Skip 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 ignore setting in the .sourcery.yaml file.

For example:

ignore:
  - data/*
  - .venv/*
  - '*_test.py'  # Note that any strings beginning with * must be quoted

If you want to just ignore a specific function you can add a # sourcery skip comment next to the line declaring the function and Sourcery will ignore that function.

def func(a,b): #sourcery skip
    if a < b:
        min_value = a
    else: 
        min_value = b
    c = min_value ** 2
    return c

If you want to ignore specific refactorings in a function you can add in # sourcery skip: <refactoring id 1>, <refactoring id 2> where each ID is from our list of available refactorings.

def func(a,b):
    if a < b: #sourcery skip: inline-immediately-returned-variable
        min_value = a
    else: 
        min_value = b
    c = min_value ** 2
    return c

Example Code

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:

def list_comprehension(list, filter, func):
    new_list = list()
    for i in list:
        if filter(i):
            new_list.append(func(i))
    return new_list


def augmented_assignment():
    a = 0
    a = a + 1
    print(a)


def sort_out_return():
    if something == other_thing:
        return True
    return False


def dictionary_get():
    dictionary = {}
    data = ""
    if "message" in dictionary:
        data = dictionary["message"]

Here is an example where Sourcery would chain together multiple refactorings:

def enable_local(self, opt):
  if opt == ECHO:
    return True
  elif opt == SGA:
    return True
  else:
    return False


def __getstate__(self):
  state = {}
  state['min'] = self.min
  state['max'] = self.max
  return state

Copy these examples into a Python file to see how Sourcery would handle them.


About us

We're a small team out of London trying to make it easier for everyone to write brilliant code. Follow us on Twitter or visit our blog to keep up with updates.


Licensing

This repository includes source code as well as packaged binaries. The MIT license only applies to the source code, not the binaries.