Closed U8NWXD closed 3 years ago
Tips:
from __future__ import annotations
to ensure they're good with it now, or it might suffice to run PyCharm inspections set to check for Python 3.10 compatibility.
PR #43 adds runscripts to run the static checkers (so we don't have to remember how) and also fixes some checks.
Note: This depends on #22.
Code Style
Get all code to pass lint checks
core/composition.py
core/control.py
core/emitter.py
core/experiment.py
core/store.py
core/process.py
core/registry.py
Complete docstrings
core/composition.py
core/control.py
core/emitter.py
core/experiment.py
core/store.py
core/process.py
core/registry.py
How to Use Pylint
To run pylint, just execute
pylint vivarium
.The files left to fix are listed in
.pylintrc
afterignore=
. We use an exclusion list to ensure that once we fix a file, any code added to it also passes the linter. This prevents regressions. It also ensures that any new files added have proper style.I use pylint's default rules. You can adjust this in
.pylintrc
after thedisable=
line. You can find the list of pylint checks here.Fixing a File
.pylintrc
pylint vivarium
Test Coverage
core/composition.py
core/control.py
core/emitter.py
core/experiment.py
core/store.py
core/process.py
core/registry.py
How to Calculate Coverage
I use the
pytest-cov
plugin to calculate test coverage. Whenever you run the tests, add the--cov=vivarium
argument to calculate coverage. At the end of the test output, you'll get a table like this:This lists the fraction of lines in each file that are covered by the tests. It also tells you which lines in the file weren't covered so you can add tests for them.
Notice the last line says we require a code coverage of 72%. This is just the current coverage value. If coverage dips below 72%, the tests will fail to make sure we don't regress.
How to Improve Coverage
pytest --cov=vivarium
..coveragerc
to your current coverage value for the entire project.Static Typing
core/composition.py
core/control.py
core/emitter.py
core/experiment.py
core/process.py
core/registry.py
How to Use Mypy
We use type annotations as specified by PEP 484 and prefer annotations to special comments like
# type: str
. See the documentation fortyping
for more information.Mypy is configured using the
mypy.ini
file, which looks like this:Notice how we first set
disallow_untyped_defs
anddisallow_incomplete_defs
to True. This means that by default, mypy will raise an error whenever a function is not completely typed. Then, for every file that hasn't been typed yet, we include a section like[mypy-vivarium.core.registry.*]
forvivarium/core/registry.py
that sets those options to False. As we add type hints, we can remove files from this list until everything has type annotations.Also note that we tell mypy to ignore types in our dependencies like
numpy
. Many third-party libraries are poorly-typed, and we want to avoid spending time trying to get mypy to work with them. We will focus on making sure the types in our code are correct instead.How to Add Types
mypy.ini
as not being covered yet.mypy.ini
.mypy vivarim