Skip to content

An example to show how to flag dependencies to a specific library with Sourcery custom rules.

Notifications You must be signed in to change notification settings

sourcery-ai/no-dependency-to-library-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

README

This is an example project to show how to flag dependencies to a library using Sourcery custom rules.

In this example, we'll flag any dependency to the library requests.

Prerequisites

In order for this example to work, you need:

  • To install the sourcery-cli with pip install sourcery-cli. Min. version: 0.12.6 (Released on 2022-07-30.)
  • A Sourcery token with a team subscription.

After installing Sourcery CLI, first you'll need to log in using the login command.

sourcery login --token YOUR_TOKEN

This step is necessary only once per installation.

Configuration

Currently, 4 custom rules are necessary for flagging various ways of dependencies to a library.
You can find these in rules/no-dependency-requests.yaml

If you use isort, you don't have import statements importing multiple packages.
=>
You can omit the 3rd and 4th rules.

Usage

sourcery review \
--config-path rules/no-dependency-requests.yaml \
--include no-dependency-requests-import \
--include no-dependency-requests-from \
--include no-dependency-requests-import-after-others \
--include no-dependency-requests-import-before-others \
code

To get the output in csv format and redirect it to a file:

sourcery review \
--config-path rules/no-dependency-requests.yaml \
--include no-dependency-requests-import \
--include no-dependency-requests-from \
--include no-dependency-requests-import-after-others \
--include no-dependency-requests-import-before-others \
--csv \
code \
> ~/sourcery_review_example_output.csv

This should create a file with the same content as example-output/sourcery_review_example_output.csv .

Known issues:

Depending on the size of your terminal window, the last column (description) may get truncated.

Options Used

  • --config-path: The path of the yaml file containing the rules. If not provided, .sourcery.yaml in the current directory is used.
  • --include: The IDs of the rules that Sourcery should apply. If not provided, Sourcery will check for all (ca. 110) Sourcery core rules. This will make the review significantly slower.

Out of Scope Currently

These rules won't detect a dependency via:

  • __import__ built-in function
  • importlib
  • the edge case, where multiple packages are imported in 1 import statement and one of them is a subpackage of requests. E.g. import json, requests.auth (This edge case can't occur if your imports adhere to isort's standards.)

Roadmap: Coming Soon

  • Fix the issue with the occasionally truncated csv output.
  • Detect the edge case, where multiple packages are imported in 1 line and one of them is a subpackage of requests (see above)
  • Express no-dependency with 1 rule instead of 4.

About

An example to show how to flag dependencies to a specific library with Sourcery custom rules.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages