Dependency checks¶
In our different web apps, we typically have a structure of the form
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 (insideCONTAINER
)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
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 pre-commit hook returns
And in case of a change, like so:
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 sitsoutput_folder
is where the output text file should be writtenplot
is a boolean indicating whetherpydeps
should generate a png output plot or not
Plot looks like so
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
- Google Python Style Guide
- ArjanCodes, very high quality ressources on various python related topics.
- Google Blog post written by one of us, Solution 6. If you're doing ML, the rest of the post might prove useful (and neptune.ai too! 😊).