Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include an option to use ruff as the linter #188

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions _shared/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def remove_conditional_files():
paths_to_remove.extend([".github/workflows/pypi.yml"])
{% endif %}

{% if cookiecutter.get("linter") == "ruff" %}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ruff config is more compact, no need for two files, remove the test one in case is generated.

paths_to_remove.extend(["tests/pyproject.toml"])
{% endif %}

{% if cookiecutter.get("console_script") != "yes" %}
paths_to_remove.extend(["src/{{ cookiecutter.package_name }}/__main__.py"])
paths_to_remove.extend(["src/{{ cookiecutter.package_name }}/cli.py"])
Expand Down
87 changes: 87 additions & 0 deletions _shared/project/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,90 @@ filterwarnings = [
{% endif %}
]

{% if cookiecutter.get("linter") == "ruff" %}
[tool.ruff]
target-version = "py311"
line-length = 88
exclude = [
"tests/bdd/steps/_compiled_feature_steps.py",
]

[tool.ruff.lint]
select = [
"E", "W", # https://docs.astral.sh/ruff/rules/#pycodestyle-e-w
"D", # https://docs.astral.sh/ruff/rules/#pydocstyle-d
"ARG", # https://docs.astral.sh/ruff/rules/#flake8-unused-arguments-arg
"BLE001", # https://docs.astral.sh/ruff/rules/blind-except/
"R", "PLR", # https://docs.astral.sh/ruff/rules/#refactor-r
"C", "PLC", # https://docs.astral.sh/ruff/rules/#convention-c
"SLF", # flake-8-self
"N", # https://docs.astral.sh/ruff/rules/#pep8-naming-n
"F", # https://docs.astral.sh/ruff/rules/unused-import/

"RUF100", # unused-noqa
]

ignore = [
# Missing docstrings.
"D100","D101","D102","D103","D104","D105","D106","D107",

# "No blank lines allowed after function docstring" conflicts with the
# Black code formatter which insists on inserting blank lines after
# function docstrings.
"D202",

# "1 blank line required before class docstring" conflicts with another
# pydocstyle rule D211 "No blank lines allowed before class docstring".
"D203",

# "Multi-line docstring summary should start at the first line"
# and "Multi-line docstring summary should start at the second line".
# These two rules conflict with each other so you have to disable one of them.
# How about we disable them both? PEP 257 says either approach is okay:
#
# > The summary line may be on the same line as the opening quotes or on
# > the next line.
# >
# > https://peps.python.org/pep-0257/#multi-line-docstrings
"D212",
"D213",

# We use Black to format our code automatically, so we don't need PyLint to
# check formatting for us.
"E501", # line-too-long

"PLR2004", # Magic values, we mostly get it on HTTP status codes

# Disabled during the pylint migration, ideally we'll enable this after we are settled in ruff
"RET504",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having these here allows to remove them from the template and apply to all projects.

"RET501",
"PLR6301",
]

[tool.ruff.lint.per-file-ignores]
"tests/*" = [
# Just disable name style checking for the tests, because we
# frequently use lots of argument names that don't conform.
# For example we frequently create pytest fixtures that aren't named in
# snake_case, such as a fixture that returns a mock of the FooBar class would
# be named FooBar in CamelCase.
"N",
# We are more lax about comment formatting in the tests
"D",

"PLR0913",

# Lots of test methods don't use self, but we still want to group our tests
# into classes.
"PLR6301",

"PLR0917", # too-many-arguments
"PLC2701", # private import
"PLR0904", # too-many-public-methods
]
# Ignore unused import errors on __init__ files to avoid having to add either a noqa stament or an __all__ declaration.
"__init__.py" = ["F401"]
{% else %}
[tool.pydocstyle]
ignore = [
# Missing docstrings.
Expand Down Expand Up @@ -89,6 +173,7 @@ ignore = [
{{ include("pydocstyle/ignores", indent=4) -}}
{% endif %}
]
{% endif %}

[tool.coverage.run]
branch = true
Expand Down Expand Up @@ -127,6 +212,7 @@ exclude_also = [
{% endif %}
]

{% if cookiecutter.get("linter") != "ruff" %}
[tool.isort]
multi_line_output = 3
include_trailing_comma = true
Expand Down Expand Up @@ -221,6 +307,7 @@ good-names = [
[tool.pylint.reports]
output-format = "colorized"
score = "no"
{% endif %}

[tool.mypy]
allow_untyped_globals = true
Expand Down
4 changes: 4 additions & 0 deletions _shared/project/requirements/checkformatting.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
pip-tools
pip-sync-faster
{% if cookiecutter.get("linter") == "ruff" %}
ruff
{% else %}
black
isort
{% endif %}
{% if include_exists("requirements/checkformatting.in") %}
{{- include("requirements/checkformatting.in") -}}
{% endif %}
4 changes: 4 additions & 0 deletions _shared/project/requirements/format.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
pip-tools
pip-sync-faster
{% if cookiecutter.get("linter") == "ruff" %}
ruff
{% else %}
black
isort
{% endif %}
{% if include_exists("requirements/format.in") %}
{{- include("requirements/format.in") -}}
{% endif %}
4 changes: 4 additions & 0 deletions _shared/project/requirements/lint.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ pip-tools
pip-sync-faster
-r tests.txt
-r functests.txt
{% if cookiecutter.get("linter") == "ruff" %}
ruff
{% else %}
toml # Needed for pydocstyle to support pyproject.toml.
pylint>=3.0.0
pydocstyle
pycodestyle
{% endif %}
{% if include_exists("requirements/lint.in") %}
{{- include("requirements/lint.in") -}}
{% endif %}
8 changes: 8 additions & 0 deletions _shared/project/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ commands =
{% endif %}
{% if cookiecutter._directory in ['pyapp', 'pyramid-app'] %}
dev: {posargs:supervisord -c conf/supervisord-dev.conf}
{% if cookiecutter.get("linter") == "ruff" %}
format: ruff check --select I --fix {{ cookiecutter.package_name}} tests bin
format: ruff format {{ cookiecutter.package_name}} tests bin
checkformatting: ruff check --select I {{ cookiecutter.package_name}} tests bin
checkformatting: ruff format --check {{ cookiecutter.package_name}} tests bin
lint: ruff check --preview -q {{ cookiecutter.package_name}} tests bin
{% else %}
format: black {{ cookiecutter.package_name }} tests bin
format: isort --atomic {{ cookiecutter.package_name }} tests bin
checkformatting: black --check {{ cookiecutter.package_name }} tests bin
Expand All @@ -125,6 +132,7 @@ commands =
lint: pylint --rcfile=tests/pyproject.toml tests
lint: pydocstyle {{ cookiecutter.package_name }} tests bin
lint: pycodestyle {{ cookiecutter.package_name }} tests bin
{% endif %}
{% else %}
dev: {posargs:ipython --classic --no-banner --no-confirm-exit}
format: black src tests bin
Expand Down
1 change: 1 addition & 0 deletions pyramid-app/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"__frontend_typechecking": "{{ cookiecutter.frontend }}",
"postgres": ["no", "yes"],
"docker": ["no", "yes"],
"linter": ["pylint", "ruff"],
"__postgres_version": "15.3-alpine",
"__postgres_port": "{{ random_port_number() }}",
"__docker_namespace": "{{ cookiecutter.github_owner }}",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#!/usr/bin/env python3
# mypy: disable-error-code="attr-defined"
"""

Initialize the DB.

Usage:

python3 -m {{ cookiecutter.package_name }}.scripts.init_db --help

"""

{% if cookiecutter.get("linter") == "pylint" %}
# pylint:disable=import-outside-toplevel,unused-import
{% else %}
# ruff: noqa: PLC0415, F401
{% endif %}
import argparse
import logging
from os import environ
Expand Down Expand Up @@ -117,7 +123,10 @@ def main():
stamped = is_stamped(engine)

if args.create:
if stamped: # pylint:disable=possibly-used-before-assignment
{% if cookiecutter.get("linter") == "pylint" %}
# pylint:disable=possibly-used-before-assignment
{% endif %}
if stamped:
log.warning("Not creating tables because the DB is stamped by Alembic")
else:
create(engine)
Expand Down
Loading