Skip to content

Dependency checks

In our different web apps, we typically have a structure of the form

App dependencies

Typical code base high level dependencies

Where each blue rectangle corresponds to a regroupment of several python modules (for complex apps, could be on the order 10-100).

An oriented arrow between A and B meaning that B depends on some modules in A, but not the other way around.

For instance, we would like our domain model classes and enums not to depend on the web app pages.

To enforce this, we use pre-commit hook that "freezes" these high level dependencies.

check_dependencies

We thus typically put in our .pre-commit-config.yaml file for a repo handling CONTAINER the following part

-   repo: local
    hooks:
    -   id: check_dependencies
        name: check_dependencies
        entry: docker exec CONTAINER python3.11 -c 
               "from ecodev_core import check_dependencies;
                exit(not check_dependencies(code_base_path,  expected_deps_file))"
        language: system

where

  • code_base_path is the path to where the actual code of the code base sits (inside CONTAINER)
  • expected_deps_file is the path towards the expected (frozen, chosen in advance) dependencies.

This is a text file that takes the form

module x depends on,core,db,insertors,nlp,pages,retrievers,routers
core,Yes,No,No,No,No,No,No
db,Yes,Yes,No,No,No,No,No
insertors,Yes,Yes,Yes,No,No,No,No
nlp,Yes,No,No,Yes,No,No,No
pages,Yes,Yes,No,No,No,No,No
retrievers,Yes,Yes,No,Yes,No,Yes,No
routers,Yes,Yes,Yes,No,No,Yes,Yes
so a dependency matrix between the regroupment of modules of interest.

check_dependencies is built on top of the awesome pydeps.

When running on a "working" code base (that has not unintentionally modified high level dependencies), the pre-commit hook returns like so:

OK check

OK pre-commit hook returns

And in case of a change, like so:

KO check

KO pre-commit hook returns

To learn more on check_dependencies, you can go inspect the test suite

compute_dependencies

To automatically compute dependencies and produce the txt file snippet of the previous part, you can call the compute_dependencies method

compute_dependencies(code_base_path: Path, output_folder: Path, plot: bool = True)

where

  • code_base_path is the path to where the actual code of the code base sits
  • output_folder is where the output text file should be written
  • plot is a boolean indicating whether pydeps should generate a png output plot or not

Plot looks like so

Imaginary dependencies

An example of a dependency graph at the regroupment of modules level produced by pydeps

As explained in the pydeps documentation, you will need to install graphviz

To learn more on compute_dependencies, you can go inspect the test suite

If you want to learn about other code good practices that you can enforce, we recommend these (biased 😊) readings/videos