Open-Source Research Project in Python: A Template
February 19, 2025 · View on GitHub
Note
This repo is continuously updating with more tools. Any contribution is welcome.
✨ Motivation
To ensure high standards in engineering projects, we offer a standardized template specifically designed for open-source Python research projects. This template is an excellent choice if you:
- Want to facilitate seamless collaboration and extension of your project by other researchers.
- Aim to bridge communication gaps among collaborators effectively.
- Seek to make rapid iterations with assurance that small code modifications won’t disrupt the overall project.
- Wish to reduce the frequency of frustrating runtime errors during experiments.
🔨 Continuous Integration (CI) Workflow
Here's a clearer and more straightforward guideline of the steps for working with your codebase. If working in a small group or working on a simple project, some of the steps can be skipped.
-
Create Issue
Before starting, open a new issue in the repository detailing what you plan to implement. Assign the issue to yourself.
-
Sync Repo
Update your local repository to match the latest version of the remote repository.
-
Create Branch
Create a new branch for your task. Name it appropriately based on the type of task, such as
feature/feature-name,bug/bug-name, orexp/exp-name. -
Implement Code
Work on your task and make necessary changes to the codebase.
-
Test Locally
Run tests using tools like mypy, pytest, and pre-commit. Ensure all tests pass before proceeding.
-
Change Commit
Add and commit your changes to the branch, then push the branch to the repository.
-
Create PR
Open a Pull Request (PR) for the branch you've pushed.
-
Link PR to Issue
In your PR, include "Closes #ISSUE_NUM" to link it to the original issue.
-
Pass Continuous Integration
Ensure all GitHub Actions checks pass. If they fail, revise your code based on the errors reported.
-
Review PR Checklist
Verify that all items in the PR checklist are completed, such as updating documentation or adding package requirements.
-
Ask for Code Review
Invite a colleague to review your PR. One approved, Use the "Squash and Merge" option to merge your PR, ensuring a clean commit history.
-
Troubleshooting
If you break down the commit history or main branch, contact the repository owner for assistance with
rebaseor other needed actions.
💼 Template Structure
The current project template supports the final package release of our codebase.
Template/
│
├── .github/ # Contains GitHub related files like workflows
├── docs/ # Documentation for the project
├── src/ # Main package directory
├── stubs/ # Type stubs for static typing (for mypy strict mode)
├── tests/ # Test scripts and resources
│
├── .gitignore # Specifies untracked files to ignore
├── .pre-commit-config.yaml # Configurations for pre-commit hooks
├── poetry.lock # Lock file generated by poetry for dependencies
├── pyproject.toml # Project metadata and tool configurations
❓ Issue & Pull Request
An issue typically describes a new feature (feature), fixing an old bug (bug), launching a group of experiments (exp), or refactoring part of the code (refactor). Using different issue templates for different issues.
A PR typically implements the content mentioned in one issue.
Notice about the development:
- When creating an issue, assign the responsible member for fixing that if possible
- When creating a PR, make sure you uses
feature/feature-name,bug/bug-name,exp/exp-namefor its branch - When finishing one PR, make sure all the github action is passed and all the checks are done.
- When merging one PR, make sure using
squash and mergeinstead ofmerge a pull request. - Avoid making any direct commit to the
mainbranch and try to avoid any--forcepush to any branch unless you are pretty sure about that.
👷 Type Checking
-
Tools
-
static type checking (
mypy) -
dynamic type checking (
beartype)
-
-
Guidelines
- Run
mypy --strict ./under the root of the current repo to test the static type.
- Run
🏅️ Unit Testing
-
Tools
- testing code components based on testing function (
pytest)
- testing code components based on testing function (
-
Guidelines
- Run
pytestunder the root of the current repo to check unit test results.
- Run
🧐 Code Spell Checking
-
Tools
- code spell checking (
codespell)
- code spell checking (
-
Guidelines
- Commonly need to ignore part of the files in the repository like
/data.
- Commonly need to ignore part of the files in the repository like
🪝 Pre-commit Hook
-
Tools
-
code formatting (
prettier) -
import package sorting (
isort) -
ipynb output clear (
nbstripout) -
code bug checking (
ruff)
-
-
Guidelines
-
Run
python -m pip install pre-committo installpre-commit -
Run
pre-commit installto allow hooking pre-commit with anygit commitcommands.
-
🧑💼 Dependency Management
-
Tools
- We utilize
poetryto support the dependency requirements. Dependency for different usage of the repo can be defined separately inpyproject.toml.
- We utilize
-
Guidelines
-
Run
pip install poetryto finish the installation of poetry. -
Create
conda environmentwith a specified Python version -
Run
poetry installto install required dependencies.
-
❤️ Contribution
I welcome all kinds of contributions, e.g. adding more tools, better practices, and discussion on trade-offs.