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

Adding ruff into the template #129

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.6.0
hooks:
- id: check-added-large-files
- id: check-merge-conflict
Expand Down
11 changes: 6 additions & 5 deletions cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"ci_pipeline": ["none", "gitlab"],
"create_cli": ["no", "yes"],
"config_file": ["none", "hocon", "yaml"],
"code_formatter": ["none", "black"],
"code_formatter": ["none", "black", "ruff"],
"editor_settings": ["none", "pycharm", "vscode"],
"__prompts__": {
"full_name": "What's your [bold yellow]name[/]?",
Expand Down Expand Up @@ -55,10 +55,11 @@
"yaml": "YAML"
},
"code_formatter": {
"__prompt__": "What [bold yellow]code formatter[/] would you like to use?",
"none": "None",
"black": "Black"
},
"__prompt__": "What [bold yellow]code formatter[/] would you like to use (Ruff includes linting)?",
"none": "None",
"black": "Black",
"ruff": "Ruff (includes linting)"
},
"editor_settings": {
"__prompt__": "Which [bold yellow]editor settings[/] do you want to include?",
"none": "None",
Expand Down
4 changes: 3 additions & 1 deletion hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
'requirements-dev.txt',
'setup.py',
'tests/pytest.ini',
'.ruff.toml'
}

files_conda = {
'environment.yml',
'environment-dev.yml',
'setup.py',
'tests/pytest.ini',
'.ruff.toml'
}

files_poetry = {
Expand Down Expand Up @@ -145,7 +147,7 @@ def handle_config():

def handle_formatter():
code_formatter = '{{ cookiecutter.code_formatter }}'
if code_formatter in ['none', 'black']:
if code_formatter in ['none', 'black', 'ruff']:
# no action necessary
pass
else:
Expand Down
2 changes: 1 addition & 1 deletion hooks/pre_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
sys.exit(1)
# check cookiecutter version (1.7.2 or higher)
if StrictVersion(cookiecutter.__version__) < StrictVersion('1.7.2'):
print(f"ERROR: You are using cookiecutter {cookiecutter.__version__}"
print(f"ERROR: You are using cookiecutter {cookiecutter.__version__}"
"but cookiecutter 1.7.2 or higher is required to use this template")
sys.exit(1)

Expand Down
325 changes: 154 additions & 171 deletions poetry.lock

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions {{cookiecutter.project_slug}}/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ variables:

stages:
- build
- lint
- test
- deploy-nonprod
- deploy-prod
Expand Down Expand Up @@ -51,6 +52,59 @@ build-wheel:
script:
- python setup.py bdist_wheel{%- endif %}

{% if cookiecutter.code_formatter == 'ruff' and cookiecutter.package_manager == 'poetry' -%}
linter-happiness:
stage: lint
image: python:3.11
cache:
key:
files:
- poetry.lock
paths:
- $PIP_CACHE_DIR
- $POETRY_CACHE_DIR
before_script:
- pip install poetry==$POETRY_VERSION
- poetry install --no-interaction --no-ansi --only lint
script:
- poetry run ruff check . --config pyproject.toml
{% elif cookiecutter.code_formatter == 'ruff' and cookiecutter.package_manager == 'conda' -%}
linter-happiness:
stage: lint
image: continuumio/miniconda3
cache:
key:
files:
- environment.yml
- environment-dev.yml
paths:
- $CONDA_PKGS_DIRS/*.tar.bz2
- $CONDA_PKGS_DIRS/urls.txt
before_script:
- export PATH="/opt/conda/bin:$PATH"
- conda env create -n .venv -f environment-dev.yml environment.yml
- source activate .venv
script:
- ruff check . --config .ruff.toml
{% elif cookiecutter.code_formatter == 'ruff' and cookiecutter.package_manager == 'pip' -%}
linter-happiness:
stage: lint
image: python:3.11
cache:
key:
files:
- requirements.txt
- requirements-dev.txt
paths:
- $PIP_CACHE_DIR
before_script:
- python -m venv .venv
- source .venv/bin/activate
- pip install ruff
script:
- ruff check . --config .ruff.toml
{% endif %}

{% if cookiecutter.package_manager == 'poetry' -%}
test-unit:
stage: test
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.project_slug}}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 'v4.4.0'
rev: 'v4.5.0'
hooks:
- id: check-added-large-files
- id: check-ast
Expand Down
28 changes: 28 additions & 0 deletions {{cookiecutter.project_slug}}/.ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Ruff configuration file
target-version = "py311"
line-length = 100
exclude = ["__init__.py"]
extend-exclude = ["notebooks/*"]

[lint]
select = [
"E", # Pycodestyle errors
"F", # Pyflakes errors
"UP", # pyupgrade checks
"B", # Bugbear checks
"SIM",# Similarity checks
"I", # Import order checks (isort)
"N" # Naming convention checks
]
task-tags = ["TODO", "FIXME", "NOTE"]

[lint.isort]
known-first-party = ["{{ cookiecutter.project_slug }}"]

[lint.pycodestyle]
max-line-length = 100

[lint.flake8-quotes]
inline-quotes = "double"
multiline-quotes = "double"
docstring-quotes = "double"
16 changes: 14 additions & 2 deletions {{cookiecutter.project_slug}}/environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,17 @@ dependencies:
- python=3.10.*
- pytest=7.*
- pytest-cov=4.*
- pre-commit=2.*{% if cookiecutter.code_formatter == 'black' %}
- black=22.*{% endif %}
- pre-commit=2.*
{%- if cookiecutter.code_formatter == 'black' %}
- black=22.*
- isort=5.*
{%- elif cookiecutter.code_formatter == 'ruff' %}
- ruff=0.4.*
{%- else %}
- isort=5.*
{%- endif %}
- pip
{% if cookiecutter.code_formatter == 'none' %}
pip:
- pyupgrade~=3.15.0
{% endif %}
42 changes: 31 additions & 11 deletions {{cookiecutter.project_slug}}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,44 @@ pytest = "^7.0"
pytest-cov = "^4.0"

[tool.poetry.group.linter.dependencies]{% if cookiecutter.code_formatter == 'black' %}
black = "^23.11"{% else %}
ruff = "^0.1.7"{% endif %}
isort = "^5.12.0"
black = "^23.11"
isort = "^5.12.0"{% else %}
ruff = "^0.4.1"{% endif %}

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.0"{% if cookiecutter.use_notebooks == 'yes' %}
jupyterlab = "^3.5"{% endif %}
{% if cookiecutter.code_formatter != 'black' %}

{% if cookiecutter.code_formatter == 'ruff' %}
[tool.ruff]
target-version = "py311"
line-length = 100
src = ["src", "tests"]
ignore = ["F401"]
exclude = ["__init__.py"]
extend-exclude = ["notebooks/*"]

[tool.ruff.lint]
select = [
"E", # Pycodestyle errors
"F", # Pyflakes errors
"UP", # pyupgrade checks
"B", # Bugbear checks
"SIM",# Simplifcation checks
"I", # Import order checks (isort)
"N" # Naming convention checks
]
task-tags = ["TODO", "FIXME", "NOTE"]

[tool.ruff.lint.isort]
known-first-party = ["{{ cookiecutter.project_slug }}"]

[tool.ruff.lint.pycodestyle]
max-line-length = 100

[tool.ruff.lint.flake8-quotes]
inline-quotes = "double"
multiline-quotes = "double"
docstring-quotes = "double"
{% endif %}
[tool.isort]{% if cookiecutter.code_formatter == 'black' %}
profile = "black"{% else %}
py_version = 310
line_length = 100
multi_line_output = 3{% endif %}

[tool.pytest.ini_options]
minversion = "7.0"
Expand Down
12 changes: 10 additions & 2 deletions {{cookiecutter.project_slug}}/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# This file defines the additional requirements for a developer working
# with this project (e.g. for testing or useful development tools).
# The regular project dependencies are defined in requirements.txt{% if cookiecutter.code_formatter == 'black' %}
black~=22.10{% endif %}
# The regular project dependencies are defined in requirements.txt
{% if cookiecutter.code_formatter == 'black' %}
black~=22.10
{% elif cookiecutter.code_formatter == 'ruff' %}
ruff~=0.4.1
{% endif %}
pre-commit~=2.20
pytest~=7.2
pytest-cov~=4.0
wheel~=0.37
{% if cookiecutter.code_formatter == 'none' or cookiecutter.code_formatter == 'black' %}
isort~=5.12.0
pyupgrade~=3.15.0
{% endif %}
10 changes: 8 additions & 2 deletions {{cookiecutter.project_slug}}/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# This file defines the requirements for a productive project deployment.
# It should not contain dependencies for tests, developer tools, etc. (see requirements-dev.txt)
{% if cookiecutter.config_file == 'hocon' %}pyhocon~=0.3.59{% elif cookiecutter.config_file == 'yaml' %}PyYAML~=6.0{% endif %}
{% if cookiecutter.create_cli == 'yes' %}typer[all]~=0.7.0{% endif %}
{% if cookiecutter.config_file == 'hocon' %}
pyhocon~=0.3.59
{% elif cookiecutter.config_file == 'yaml' %}
PyYAML~=6.0
{% endif %}
{% if cookiecutter.create_cli == 'yes' %}
typer[all]~=0.7.0
{% endif %}
Loading