Skip to content

Refactor Configuration

Configuration of rules to include or skip, specified in the .sourcery.yaml file (parent reference).

Fields Overview

All fields are optional.

Field Type Description
include list of string List of rule IDs Sourcery will exclusively suggest.
skip list of string List of rule IDs Sourcery will never suggest.
rule_types list of string List of types of refactoring Sourcery will suggest.
python_version string The minimum Python version your project supports.

Default

refactor:
  include: []
  skip: []
  rule_types:
    - refactoring
    - suggestion
    - comment
  python_version: '3.9'

Complete Example

With the following configuration, Sourcery will

refactor:
  skip:
    - for-append-to-extend
    - raise-from-previous-error
  rule_types:
    - refactoring
    - suggestion
  python_version: 3.8

Fields

include

list of string

List of rule IDs Sourcery will exclusively suggest.

If empty (default), Sourcery may suggest any rule.

include will override any skip configuration.

Default

(Empty list)

Example

With the following configuration, Sourcery will only suggest assign-if-exp and de-morgan refactorings.

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

skip

list of string

If empty (default), Sourcery may suggest any rule.

List of rule IDs Sourcery will never suggest.

skip will be overridden by any include configuration.

Default

(Empty list)

Example

With the following configuration, Sourcery will never suggest assign-if-exp and de-morgan refactorings.

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

rule_types

list of string

The types of rule Sourcery will suggest.

Default

refactor:
  rule_types:
    - refactoring
    - suggestion
    - comment

Example

With the following configuration, Sourcery will only suggest refactorings and suggestions, never comments.

refactor:
  rule_types:
    - refactoring
    - suggestion

See Also

python_version

string

The minimum Python version your project supports.

Sourcery will not suggest any refactorings requiring Python features introduced after this version.

If undefined, Sourcery will suggest refactorings for any Python version.

Default

refactor:
  python_version: '3.9'

Example

refactor:
  python_version: '3.7'

Use Case

Python 3.8 introduced the named expression, also known as the "walrus" operator. By default, Sourcery will assume you are using the latest version of Python, and so it will suggest refactorings (such as use-named-expression) which use the walrus operator. If your project supports Python 3.7 or lower, you should set python_version in the refactor configuration to prevent this refactoring (and others).