Developer’s guide¶
Note
The https://learn.scientific-python.org webpages are an extremely valuable learning resource for Python software developer. The reader is referred to that for any detail not covered in the following guide.
The following rules and conventions have been established for the package development and are enforced throughout the entire code base. Merge requests that do not comply to the following directives will be rejected.
To start developing legend-lh5io, fork the remote repository to your personal GitHub account (see About Forks). If you have not set up your ssh keys on the computer you will be working on, please follow GitHub’s instructions. Once you have your own fork, you can clone it via (replace “yourusername” with your GitHub username):
$ git clone git@github.com:yourusername/legend-lh5io.git
All extra tools needed to develop legend-lh5io are listed as optional dependencies and can be installed via pip by running:
$ cd legend-lh5io
$ pip install -e '.[all]' # single quotes are not needed on bash
Important
Pip’s --editable | -e flag let’s you install the package in “developer
mode”, meaning that any change to the source code will be directly
propagated to the installed package and importable in scripts.
Tip
It is strongly recommended to work inside a virtual environment, which guarantees reproductibility and isolation. For more details, see learn.scientific-python.org.
Code style¶
All functions and methods (arguments and return types) must be type-annotated. Type annotations for variables like class attributes are also highly appreciated.
Messaging to the user is managed through the
loggingmodule. Do not addprint()statements. To make a logging object available in a module, add this:import logging log = logging.getLogger(__name__)
at the top. In general, try to keep the number of
logging.debug()calls low and use informative messages.logging.info()calls should be reserved for messages from high-level routines and very sporadic. Good code is never too verbose.If an error condition leading to undefined behavior occurs, raise an exception. try to find the most suitable between the built-in exceptions, otherwise
raise RuntimeError("message"). Do not raiseWarnings, uselogging.warning()for that and don’t abort the execution.Warning messages (emitted when a problem is encountered that does not lead to undefined behavior) must be emitted through
logging.warning()calls.
A set of pre-commit hooks is configured to make
sure that legend-lh5io coherently follows standard coding style conventions.
The pre-commit tool is able to identify common style problems and automatically
fix them, wherever possible. Configured hooks are listed in the
.pre-commit-config.yaml file at the project root folder. They are run
remotely on the GitHub repository through the pre-commit bot, but should also be run locally before submitting a
pull request:
$ cd legend-lh5io
$ pip install '.[test]'
$ pre-commit run --all-files # analyse the source code and fix it wherever possible
$ pre-commit install # install a Git pre-commit hook (strongly recommended)
For a more comprehensive guide, check out the learn.scientific-python.org documentation about code style.
Testing¶
The legend-lh5io test suite is available below
tests/. We use pytest to run tests and analyze their output. As a starting point to learn how to write good tests, reading of the relevant learn.scientific-python.org webpage is recommended.Unit tests are automatically run for every push event and pull request to the remote Git repository on a remote server (currently handled by GitHub actions). Every pull request must pass all tests before being approved for merging. Running the test suite is simple:
$ cd legend-lh5io $ pip install '.[test]' $ pytest
Additionally, pull request authors are required to provide tests with sufficient code coverage for every proposed change or addition. If necessary, high-level functional tests should be updated. We currently rely on codecov.io to keep track of test coverage. A local report, which must be inspected before submitting pull requests, can be generated by running:
$ pytest --cov=lgdo
Documentation¶
We adopt best practices in writing and maintaining legend-lh5io’s documentation. When contributing to the project, make sure to implement the following:
Documentation should be exclusively available on the Project website legend-lh5io.readthedocs.io. No READMEs, GitHub/LEGEND wiki pages should be written.
Pull request authors are required to provide sufficient documentation for every proposed change or addition.
Documentation for functions, classes, modules and packages should be provided as Docstrings along with the respective source code. Docstrings are automatically converted to HTML as part of the legend-lh5io package API documentation.
General guides, comprehensive tutorials or other high-level documentation (e.g. referring to how separate parts of the code interact between each other) must be provided as separate pages in
docs/source/and linked in the table of contents.Jupyter notebooks should be added to the main Git repository below
docs/source/notebooks.Before submitting a pull request, contributors are required to build the documentation locally and resolve and warnings or errors.
Writing documentation¶
We adopt the following guidelines for writing documentation:
Documentation source files must formatted in reStructuredText (reST). A reference format specification is available on the Sphinx reST usage guide. Usage of Cross-referencing syntax in general and for Python objects in particular is recommended. We also support cross-referencing external documentation via sphinx.ext.intersphinx, when referring for example to
pandas.DataFrame.To document Python objects, we also adopt the NumPy Docstring style. Examples are available here.
We support also the Markdown format through the MyST-Parser.
Jupyter notebooks placed below
docs/source/notebooksare automatically rendered to HTML pages by the nbsphinx extension.
Building documentation¶
Scripts and tools to build documentation are located below docs/. To build
documentation, sphinx and a couple of additional Python packages are
required. You can get all the needed dependencies by running:
$ cd legend-lh5io
$ pip install '.[docs]'
Pandoc is also required to render Jupyter notebooks. To build documentation, run the following commands:
$ cd docs
$ make clean
$ make
Documentation can be then displayed by opening build/html/index.html with a
web browser. Documentation for the legend-lh5io website is built and deployed by
Read the Docs.
Versioning¶
Collaborators with push access to the GitHub repository that wish to release a new project version must implement the following procedures:
Semantic versioning is adopted. The version string uses the
MAJOR.MINOR.PATCHformat.To release a new minor or major version, the following procedure should be followed:
A new branch with name
releases/vMAJOR.MINOR(note thev) containing the code at the intended stage is createdThe commit is tagged with a descriptive message:
git tag vMAJOR.MINOR.0 -m 'short descriptive message here'(note thev)Changes are pushed to the remote:
$ git push origin releases/vMAJOR.MINOR $ git push origin refs/tags/vMAJOR.MINOR.0
To release a new patch version, the following procedure should be followed:
A commit with the patch is created on the relevant release branch
releases/vMAJOR.MINORThe commit is tagged:
git tag vMAJOR.MINOR.PATCH(note thev)Changes are pushed to the remote:
$ git push origin releases/vMAJOR.MINOR $ git push origin refs/tags/vMAJOR.MINOR.PATCH
To upload the release to the Python Package Index, a new release must be created through the GitHub interface, associated to the just created tag. Usage of the “Generate release notes” option is recommended.