Last time we introduced custom rules into Sourcery.

This time they're becoming even more powerful as you can filter pattern matches with a condition.

Let's say we want to stop global variables being declared. First let's write down what a global variable is:

  1. It's declared at the top level of the module
  2. It's not a constant using uppercase like DAYS_IN_WEEK = 7
  3. Let's allow private globals starting with _

Here's a rule to identify global variables:

  - id: no-global-variables
    pattern: ${var} = ${value}
    condition: |
        and not var.is_upper_case()
        and not var.starts_with("_")
    description: Don't declare `${var}` as a global variable

Check out the conditions reference to see what conditions are available.


  • Custom rules can contain a condition field that means the rule only matches when the condition is satisfied
  • CLI: display a progress bar showing how many files have been reviewed
  • Docs: Flag Dependencies to a Library with Sourcery custom rules
  • Docs: Establish Rules for Dependencies Between Your Packages with Sourcery custom rules
  • use-file-iterator refactoring


  • reintroduce-else will now trigger more often
  • Refactorings list-literal and dict-literal will now trigger in every instance of list() and dict(), instead of only in assignments
  • CLI: display rule descriptions and explanations as Markdown
  • CLI docs: present the review command instead of refactor


  • dict-assign-update-to-union doesn't trigger for global variables
  • Files using tab indenting will be always be refactored with tabs
  • Identical clone detection will now only trigger for functions if they have the same name
  • Recover from PermissionErrors when rotating log files
  • Sourcery now run refactorings on functions containing nested functions