Skip to content

Commit

Permalink
Upgrade to Python 3.12 and switch to ruff (#220)
Browse files Browse the repository at this point in the history
* upgrade to python 3.12

* run poetry lock

* poetry export

* switch to ruff

* run ruff-format before ruff

* apply pre-commit and fix new warnings

* unignore all ruff rules

* include notebooks in ruff

* opt into the pep257 docstyle

* add ignores for current ruff violations

* apply ruff

* pre-commit autoupdate

* switch template to use ruff

* make CI repo URL independant

* upgrade requests

* clear poetry cache and lock again

* fix cookicutter typo

* upgrade pyenv action for 3.12.1 support

* specify pyenv-action commit

* upgrade to pyenv-action that supports 3.12.1

* add newsfragment
  • Loading branch information
graingert-coef authored Apr 21, 2024
1 parent d9d8ea0 commit 1b232c9
Show file tree
Hide file tree
Showing 14 changed files with 925 additions and 1,373 deletions.
3 changes: 3 additions & 0 deletions .changelog/220.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Upgraded to Python 3.12.1
Switched from flake8 to ruff
Regenerated poetry.lock
8 changes: 4 additions & 4 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
timeout-minutes: 30
strategy:
matrix:
python-version: ["3.11"]
python-version: ["3.12"]

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -90,7 +90,7 @@ jobs:
timeout-minutes: 30
strategy:
matrix:
python-version: [3.11]
python-version: [3.12]

steps:
- uses: actions/checkout@v3
Expand All @@ -110,7 +110,7 @@ jobs:
run: |
cd ~
pwd
cookiecutter /home/runner/work/coefficient-cookiecutter/coefficient-cookiecutter --no-input
cookiecutter $GITHUB_WORKSPACE --no-input
echo "cc_python_version=$(cat ~/coefficient-project/.python-version)"
echo "cc_python_version=$(cat ~/coefficient-project/.python-version)" >> $GITHUB_ENV
echo "cc_venv=$(cat ~/coefficient-project/.venv)"
Expand All @@ -119,7 +119,7 @@ jobs:
# TODO: This can be replaced I think with actions/setup-python?

- name: Install pyenv
uses: "gabrielfalcao/pyenv-action@v14"
uses: gabrielfalcao/pyenv-action@91f80eb59affae16a948d6ace29b1fada06d8e4c # https://github.com/gabrielfalcao/pyenv-action/pull/441
with:
default: ${{ env.cc_python_version }}

Expand Down
80 changes: 15 additions & 65 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# $ pre-commit autoupdate
minimum_pre_commit_version: 3.3.2
default_language_version:
python: python3.11
python: python3.12
exclude: "{{cookiecutter.repo_name}}"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: end-of-file-fixer
name: Check for a blank line at the end of scripts (auto-fixes)
Expand All @@ -32,60 +32,19 @@ repos:
- id: check-docstring-first
- id: name-tests-test
args: ["--django"]
- repo: https://github.com/myint/autoflake
rev: v2.1.1
hooks:
- id: autoflake
args: &autoflake
- --in-place
- --remove-all-unused-imports
- --expand-star-imports
- --remove-duplicate-keys
- --remove-unused-variables
- repo: https://github.com/pycqa/flake8
rev: 5.0.4
hooks:
- &flake8
id: flake8
additional_dependencies:
- flake8-bugbear==23.3.12
- flake8-comprehensions==3.12.0
- flake8-docstrings==1.7.0
- flake8-eradicate==1.4.0
- flake8-fixme==1.1.1
- flake8-implicit-str-concat==0.4.0
- flake8-mutable==1.2.0
- flake8-no-pep420==2.4.0
- flake8-print==5.0.0
- flake8-return==1.2.0
- flake8-simplify==0.19.3
args: ["--config=setup.cfg"]
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
- repo: https://github.com/asottile/add-trailing-comma
rev: v2.4.0
hooks:
- id: add-trailing-comma
args: [--py36-plus]
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
types: [python]
- repo: https://github.com/asottile/pyupgrade
rev: v3.4.0
hooks:
- id: pyupgrade
args:
- "--py311-plus"
- repo: https://github.com/pycqa/bandit
rev: 1.7.5

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.14
hooks:
- id: bandit
args: ["-ii", "-ll"]
# Run the formatter.
- id: ruff-format
types_or: [python, pyi, jupyter]

# Run the linter.
- id: ruff
types_or: [python, pyi, jupyter]
args: [--fix]

- repo: https://github.com/Yelp/detect-secrets
rev: v1.4.0
hooks:
Expand Down Expand Up @@ -126,23 +85,14 @@ repos:
# "--without-hashes",
# ]
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.0-alpha.9-for-vscode
rev: v4.0.0-alpha.8
hooks:
- id: prettier
types_or: [yaml]
additional_dependencies:
- "[email protected]"
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint {{cookiecutter.repo_name}} tests
# entry: bash -c 'pylint ./path/package1/; pylint ./path/package2/'
language: system
types: [python]
always_run: true
pass_filenames: false
stages: [manual]
- id: pip-audit
name: pip-audit
entry: pip-audit
Expand Down
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.11.3
3.12.1
15 changes: 3 additions & 12 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,20 @@
"files.trimTrailingWhitespace": true,
"prettier.enable": true,

// python - black
"python.formatting.provider": "black",
"python.formatting.blackArgs": [],

// python - other
"python.languageServer": "Pylance",

// python - linting & static analysis
"python.analysis.typeCheckingMode": "off",
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
"python.linting.flake8Args": [],
"python.linting.mypyEnabled": true,
"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": [],

// Python files only
"[python]": {
// isort on save
"editor.codeActionsOnSave": {"source.organizeImports": true},
"editor.codeActionsOnSave": {"source.organizeImports": "explicit"},
// Stop the 'Black does not support "Format Selection"' message every
// time you paste something (https://stackoverflow.com/a/63612401/3279076)
"editor.formatOnPaste": false
"editor.formatOnPaste": false,
"editor.defaultFormatter": "charliermarsh.ruff"
},

// python - pytest (https://code.visualstudio.com/docs/python/testing)
Expand Down
2 changes: 1 addition & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"repo_name": "{{ cookiecutter.project_name.lower().replace(' ', '-') }}",
"package_name": "{{ cookiecutter.project_name.lower().replace(' ', '_').replace('-', '_') }}",
"virtualenv": "{{ cookiecutter.project_name.lower().replace(' ', '-') }}",
"python_version": "3.11.1",
"python_version": "3.12.1",
"version": "0.1.0",
"line_length": "100",
"use_towncrier": "y",
Expand Down
19 changes: 8 additions & 11 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,33 @@
from __future__ import annotations

import logging
import os
import shutil
from pathlib import Path

PROJECT_DIRECTORY = Path(".")

PROJECT_DIRECTORY = Path()

if __name__ == "__main__":
# Rename templated files
os.rename(PROJECT_DIRECTORY / "pyproject.template.toml", PROJECT_DIRECTORY / "pyproject.toml")
os.rename(
PROJECT_DIRECTORY / ".pre-commit-config.template.yaml",
(PROJECT_DIRECTORY / "pyproject.template.toml").rename(PROJECT_DIRECTORY / "pyproject.toml")
(PROJECT_DIRECTORY / ".pre-commit-config.template.yaml").rename(
PROJECT_DIRECTORY / ".pre-commit-config.yaml",
)

# Copy .env.template -> .env
shutil.copy(PROJECT_DIRECTORY / ".env.template", PROJECT_DIRECTORY / ".env")

if "{{ cookiecutter.use_towncrier }}" != "y":
os.remove(PROJECT_DIRECTORY / "docs" / "using_towncrier.md")
(PROJECT_DIRECTORY / "docs" / "using_towncrier.md").unlink()

if "{{ cookiecutter.use_github_actions }}" != "y":
try:
shutil.rmtree(PROJECT_DIRECTORY / ".github" / "workflows")
except Exception as error_message: # pylint: disable=broad-except
logging.error(error_message)
except Exception: # pylint: disable=broad-except
logging.exception("failed to delete .github/workflows")

# Don't need .github if using neither GitHub Actions nor dependabot
if "{{ cookiecutter.use_dependabot }}" != "y":
try:
shutil.rmtree(PROJECT_DIRECTORY / ".github")
except Exception as error_message: # pylint: disable=broad-except
logging.error(error_message)
except Exception: # pylint: disable=broad-except
logging.exception("failed to delete .github")
Loading

0 comments on commit 1b232c9

Please sign in to comment.