Skip to content

Commit

Permalink
Merge pull request #355 from lincc-frameworks/sean/ruff
Browse files Browse the repository at this point in the history
Add Ruff linting support
  • Loading branch information
smcguire-cmu authored Jan 30, 2024
2 parents c5b6541 + b375cd8 commit 9a4f6a9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions copier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ preferred_linter:
choices:
black: black
pylint: pylint
ruff: ruff
none: none
when: "{{ custom_install }}"

Expand Down
16 changes: 15 additions & 1 deletion docs/practices/linting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ the code they're looking at adheres to that agreed-upon coding standard.
A code reviewer won't be distracted by improper spacing, and can focus their
reviewing effort on the meat of the code.

There are two main linters suggested by this template: pylint and black. While
There are three main linters suggested by this template: pylint, black, and ruff. While
they have a lot of the same opinions, we recommend picking a single standard for
your project and sticking to it.
If some folks use one linter, this may cause undue churn in your source files as
Expand Down Expand Up @@ -72,6 +72,20 @@ the ``./src`` and ``./tests`` directories. This allows separate configurations
for source versus test code. Take a look at the configuration documentation
for pylint here: https://pylint.readthedocs.io/en/latest/user_guide/configuration/index.html


Modifying ruff
.................

`Ruff <https://docs.astral.sh/ruff/>`_ is a very performant and highly customizable linting
tool. The configuration for ruff is maintained in the ``pyproject.toml`` file.
Ruff has many rules split into groups that can be selected to use when checking code.
By default, we mostly follow the set of rules suggested by the
`ruff documentation <https://docs.astral.sh/ruff/linter/#rule-selection>`_, with a few extra
rules as suggested by
`Rubin Data Management <https://developer.lsst.io/python/style.html#ruff-configuration-files>`_.
For more information on configuration, see ruff's documentation here:
https://docs.astral.sh/ruff/configuration/

How to switch or remove linters
-------------------------------

Expand Down
5 changes: 3 additions & 2 deletions docs/source/new_project.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ questions:
* - *What tooling would you like to use to enforce code style?*
- A linter is a tool to automatically format for consistency (see :doc:`Linting <../practices/linting>`).
We provide options for `black <https://black.readthedocs.io/en/stable/>`_,
`pylint <https://pypi.org/project/pylint/>`_, or no linter. Choosing a linter will include it as a
project dependency and include it in the :doc:`pre-commit <../practices/precommit>` hooks.
`pylint <https://pypi.org/project/pylint/>`_, `ruff <https://docs.astral.sh/ruff/>`_ or no linter.
Choosing a linter will include it as a project dependency and include it in the
:doc:`pre-commit <../practices/precommit>` hooks.
Defaults to ``pylint`` during simple installation.
* - *Do you want to use isort to maintain a specific ordering for module imports?*
- `isort <https://pycqa.github.io/isort/>`_ is a tool for ordering imports in a standard order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ jobs:
uses: psf/black@stable
with:
src: ./src
{%- elif preferred_linter == 'ruff' %}
run: |
ruff check --output-format=github .
{%- endif %}
7 changes: 7 additions & 0 deletions python-project-template/.pre-commit-config.yaml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ repos:
# pre-commit's default_language_version, see
# https://pre-commit.com/#top_level-default_language_version
language_version: python3.10
{% elif preferred_linter == 'ruff' %}
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.3
hooks:
- id: ruff
types_or: [ python, pyi, jupyter ]
{% endif %}

{% if mypy_type_checking != 'none' %}
Expand Down
25 changes: 25 additions & 0 deletions python-project-template/pyproject.toml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ dev = [
"pylint", # Used for static linting of files
{%- elif preferred_linter == 'black' %}
"black", # Used for static linting of files
{%- elif preferred_linter == 'ruff' %}
"ruff", # Used for static linting of files
{%- endif %}
{%- if mypy_type_checking != 'none' %}
"mypy", # Used for static type checking of files
Expand Down Expand Up @@ -82,6 +84,29 @@ target-version = ["py38"]
profile = "black"
line_length = 110

{%- if preferred_linter == 'ruff' %}

[tool.ruff]
line-length = 110
target-version = "py38"
select = [
# pycodestyle
"E",
"W",
# Pyflakes
"F",
# pep8-naming
"N",
# pyupgrade
"UP",
# flake8-bugbear
"B",
# flake8-simplify
"SIM",
# isort
"I",
]
{%- endif %}
{%- if mypy_type_checking != 'none' %}
[tool.setuptools.package-data]
{{package_name}} = ["py.typed"]
Expand Down

0 comments on commit 9a4f6a9

Please sign in to comment.