Open billbrod opened 4 years ago
As part of this, also:
snake_case
for variables, etc)After everything else has stabilized, run this on everything.
Chiming in here from the pyOpenSci review.
We do require this--indirectly--by asking that linting checks pass.
As you comment above, I would address other issues first and then add linting.
There are many ways to skin this cat, see https://learn.scientific-python.org/development/guides/style/
I use a nox session so anyone can run linting checks locally without learning the pre-commit CLI but you could also just let contributors remain mostly blissfully ignorant of linting, and run pre-commit in CI on PRs, etc.
How you do it is up to you but there should be some linting checks.
I'm also looking into ruff for this, which might be nicer.
As mentioned above, will need linters to be added to CI as well (see #190 )
We are currently looking at Ruff, nox and pre-commit hook to introduce formatting conventions. Below is a list of considerations aimed at guiding the decision process, as well as a list of their respective configurations and a brief explanation of each.
(in pyproject.toml
; Legend: ✅: common and included, ❓ common but not included)
TL;DR:
More details:
flake8-bugbear
): ✅
Checks for common programming errors, style issues, or patterns that are error-proneisort
): ✅
Helps in managing import statements and ensuring they are organized according to a specified convention.flake8-unused-arguments
): ❓
Flags unused function arguments.flake8-comprehensions
): ❓
Checks for issues in list comprehensions and generator expressions.flake8-errmsg
): ❓
Lints error message formatting.flake8-import-conventions
): ❓
Ensures imports are formatted according to best practices.flake8-use-pathlib
): ❓
Encourages the use of pathlib
instead of os.path
.flake8-return
):❓
Checks for issues related to return statements.flake8-simplify
): ❓
Suggests simplifications for code.flake8-print
): ❓
Flags print statements.pyupgrade
): ❓
Upgrades syntax to modern Python standards.flake8-2020
):❓
Enforces Python 3.8+ compatibility.TL;DR:
noxfile.py
.Nox is a Python-based task runner that is used to automate various development workflows, such as testing, building, and deploying applications. It allows you to define sessions, which are Python functions in the noxfile.py
decorated with @nox.session
with various parameters, such as name
, by which these sessions can then be executed via the command line. This easily allows to test code by running pre-defined tasks with a single command across multiple Python versions without manually setting up environments.
Example: Based on the following noxfile.py
, the command nox -s tests
would run the session named "tests” below. nox
without any commands would run all the sessions defined in the noxfile.py
.
@nox.session(name="tests", python=["3.10", "3.11", "3.12"])
def tests(session):
# run tests
session.install("pytest")
session.run("pytest")
Hook:
A pre-commit hook can be added to run Ruff linting on code before it is committed. This ensures that developers catch and fix linting issues early in the development process. It only runs when installed and can be bypassed with git commit -m <my commit message> --no-verify
.
Considered Configurations: (in .pre-commit.yaml
:
(Legend: ✅: included, ❓: recommended, but not included)
check-added-large-files
✅
check-case-conflict
✅
File.txt
and file.txt
would be considered the samecheck-merge-conflict
✅
<<<<<<<
, =======
, >>>>>>>
) in files.check-symlinks
❓
check-yaml
✅
debug-statements
✅
print
, console.log
) left in code.end-of-file-fixer
✅
mixed-line-ending
❓
requirements-txt-fixer
❓
requirements.txt
files, ensuring they follow conventions and are easy to manage.trailing-whitespace
✅
Finally, the CI configuration ci.yml
has been updated to include a step each for running Ruff formatting and linting check on all new commits and pull requests. Two separate actions were chosen to signal the developer which combination of the two might be causing an error.
Now, that the ruff linter and formatter have been introduced (see PR #266), possible next steps to consider are as follows:
Note that if additional linting rules are to be introduced (e.g., B), each rule should be introduced in a separate PR. Therefore, we may want to consider closing this big issue and using my notes below and putting them in separate smaller issues.
Include the following linting rules (in pyproject.toml
) and resolve linting errors:
flake8-bugbear
):
Checks for common programming errors, style issues, or patterns that are error-proneflake8-unused-arguments
):
Flags unused function arguments.flake8-comprehensions
):
Checks for issues in list comprehensions and generator expressions.flake8-errmsg
):
Lints error message formatting.flake8-import-conventions
):
Ensures imports are formatted according to best practices.flake8-use-pathlib
):
Encourages the use of pathlib
instead of os.path
.flake8-return
):
Checks for issues related to return statements.flake8-simplify
):
Suggests simplifications for code.flake8-print
):
Flags print statements.pyupgrade
):
Upgrades syntax to modern Python standards.flake8-2020
):
Enforces Python 3.8+ compatibility.We might want to consider to include nox sessions for:
mypy
or a similar tool to perform static type checking, to catch type-related errors early.We might want to add the following to .pre-commit-config.yaml
:
mixed-line-ending
requirements-txt-fixer
requirements.txt
files, ensuring they follow conventions and are easy to manage.
Given that we have different authors with different styles, our formatting/conventions are not consistent. We should look into using something like yapf or black to make our code consistent and maybe pydocstyle for our documentation.