Skip to content

Commit

Permalink
Ditch flake8 and black for ruff; add Django 5 to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
philgyford committed Dec 8, 2023
1 parent 2d89dcd commit b951f3d
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 88 deletions.
35 changes: 18 additions & 17 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

name: Tests

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

Expand All @@ -17,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12-dev"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13-dev"]
django-version: ["3.2", "4.1", "4.2", "main"]
exclude:
# Django 5.0 isn't compatible with python < 3.10
Expand All @@ -29,9 +28,14 @@ jobs:
- python-version: "3.11"
django-version: "3.2"

- python-version: "3.12-dev"
- python-version: "3.12"
django-version: "3.2"
- python-version: "3.12"
django-version: "4.1"

- python-version: "3.13-dev"
django-version: "3.2"
- python-version: "3.12-dev"
- python-version: "3.13-dev"
django-version: "4.1"

steps:
Expand All @@ -51,8 +55,7 @@ jobs:
uses: actions/cache@v3
with:
path: ${{ steps.pip-cache.outputs.dir }}
key:
${{ matrix.python-version }}-v2-${{ hashFiles('**/pyproject.toml') }}-${{ hashFiles('**/tox.ini') }}
key: ${{ matrix.python-version }}-v2-${{ hashFiles('**/pyproject.toml') }}-${{ hashFiles('**/tox.ini') }}
restore-keys: |
${{ matrix.python-version }}-v2-
Expand All @@ -69,15 +72,14 @@ jobs:
- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v3

# The flake8 test in tox.ini won't run with the test job so we need to add it here.
lint:
name: "Lint: ${{ matrix.toxenv }}"
ruff:
name: "Run ruff: ${{ matrix.toxenv }}"
runs-on: ubuntu-latest

strategy:
matrix:
toxenv:
- flake8
- ruff

steps:
- name: Git clone
Expand All @@ -86,7 +88,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
python-version: "3.11"

- name: Get pip cache dir
id: pip-cache
Expand All @@ -96,8 +98,7 @@ jobs:
uses: actions/cache@v3
with:
path: ${{ steps.pip-cache.outputs.dir }}
key:
${{ matrix.python-version }}-v2-${{ hashFiles('**/pyproject.toml') }}-${{ hashFiles('**/tox.ini') }}
key: ${{ matrix.python-version }}-v2-${{ hashFiles('**/pyproject.toml') }}-${{ hashFiles('**/tox.ini') }}
restore-keys: |
${{ matrix.python-version }}-v2-
Expand All @@ -113,11 +114,11 @@ jobs:
# https://github.com/8398a7/action-slack/issues/72#issuecomment-649910353
name: Slack notification
runs-on: ubuntu-latest
needs: [test, lint]
needs: [test, ruff]

# this is required, otherwise it gets skipped if any needed jobs fail.
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idneeds
if: always() # Pick up events even if the job fails or is cancelled.
if: always() # Pick up events even if the job fails or is cancelled.

steps:
- uses: technote-space/workflow-conclusion-action@v3
Expand Down
34 changes: 14 additions & 20 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_language_version:
python: python3.10
python: python3.11

exclude: |
(?x)^(
Expand All @@ -8,34 +8,28 @@ exclude: |
)$
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
rev: v3.0.3
hooks:
- id: prettier
types_or:
- css
- javascript
- json
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.6
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
- repo: https://github.com/asottile/pyupgrade
rev: v3.9.0
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py38-plus]
51 changes: 25 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
[![Tests](https://github.com/philgyford/django-spectator/actions/workflows/tests.yml/badge.svg)](https://github.com/philgyford/django-spectator/actions/workflows/tests.yml)
[![codecov](https://codecov.io/gh/philgyford/django-spectator/branch/main/graph/badge.svg?token=T7TIJ5XNWH)](https://codecov.io/gh/philgyford/django-spectator)
[![Code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)

Two Django apps:

- One to track book and periodical reading, including start and end dates, authors, and cover images.
- One to track events attended (movie, plays, gigs, exhibitions, comedy, dance, classical), including date, venue (with maps), people/organisations involved, and images of tickets.
- One to track book and periodical reading, including start and end dates, authors, and cover images.
- One to track events attended (movie, plays, gigs, exhibitions, comedy, dance, classical), including date, venue (with maps), people/organisations involved, and images of tickets.

For Django 3.2, 4.1, and 4.2, running on Python 3.8 upwards.

Expand All @@ -20,26 +19,25 @@ There are also template tags for displaying data in your own templates (see belo

This is used on my personal website (with custom templates): [reading](https://www.gyford.com/phil/reading/) and [events](https://www.gyford.com/phil/events/).

Contents
--------
## Contents

1. [Installation](#installation)
- [Settings](#settings)
- [Settings](#settings)
2. [Overview](#overview)
- [Creators](#creators)
- [Reading](#reading)
- [Events](#events)
- [Creators](#creators)
- [Reading](#reading)
- [Events](#events)
3. [Template tags](#template-tags)
- [Core template tags](#core-template-tags)
- [Reading template tags](#reading-template-tags)
- [Events template tags](#events-template-tags)
- [Core template tags](#core-template-tags)
- [Reading template tags](#reading-template-tags)
- [Events template tags](#events-template-tags)
4. [Local development](#local-development)
- [Environment variables](#environment-variables)
- [pre-commit](#pre-commit)
- [Running tests locally](#running-tests-locally)
- [Making a new release](#making-a-new-release)
- [Adding a new Event kind](#adding-a-new-event-kind)
- [Adding a new Work kind](#adding-a-new-work-kind)
- [Environment variables](#environment-variables)
- [pre-commit](#pre-commit)
- [Running tests locally](#running-tests-locally)
- [Making a new release](#making-a-new-release)
- [Adding a new Event kind](#adding-a-new-event-kind)
- [Adding a new Work kind](#adding-a-new-work-kind)
5. [Contact](#contact)

## 1. Installation
Expand Down Expand Up @@ -122,13 +120,14 @@ To use [Mapbox](https://www.mapbox.com) sign up and get an API key for [Mapbox G
"tile_style": "mapbox://styles/mapbox/streets-v11",
"api_key": "YOUR-API-KEY"
}

The `tile_style` value can be one of the pre-defined map styles, [listed under options.styles](https://docs.mapbox.com/mapbox-gl-js/api/#map).

Setting `"enable"` to `False` in the above dicts will prevent maps appearing.

#### Slug settings

URLs for all objects include automatically-generated slugs, which are based on [Hashids](<http://hashids.org>) of the object's ID. You can change which characters are used in these slugs with this setting. e.g.:
URLs for all objects include automatically-generated slugs, which are based on [Hashids](http://hashids.org) of the object's ID. You can change which characters are used in these slugs with this setting. e.g.:

SPECTATOR_SLUG_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

Expand Down Expand Up @@ -193,7 +192,7 @@ Each Event can have zero or more Creators associated directly with it. e.g. the
- Order: 1
- Buffalo Tom
- Role: "Support"
- Order: 2
- Order: 2

Events can be different kinds, e.g. "gig", "cinema", "theatre". This is only used for categorising Events into different lists - it doesn't restrict the kinds of Works that can be associated with it. You could have a "cinema" Event that has a movie, play and dance piece associated with it.

Expand Down Expand Up @@ -452,7 +451,7 @@ I set things up locally, having installed virtualenv and pyenv, like this:
$ cd devproject
$ virtualenv --prompt spectator-devproject venv
$ source venv/bin/activate
(spectator-devproject)$ pyenv local 3.10.5
(spectator-devproject)$ pyenv local 3.11
(spectator-devproject)$ python -m pip install -r requirements.txt

Then run migrations and start the server:
Expand All @@ -465,16 +464,16 @@ Then run migrations and start the server:
You can add a `.env` file in `devproject/` and its environment variables will be
read in `devproject/devproject/settings.py`. e.g.:

SPECTATOR_MAPBOX_API_KEY="your-api-key-here"
DJANGO_SECRET_KEY="your-secret-key"
DJANGO_LOG_LEVEL="INFO"
SPECTATOR_MAPBOX_API_KEY="your-api-key-here"
DJANGO_SECRET_KEY="your-secret-key"
DJANGO_LOG_LEVEL="INFO"

### pre-commit

pre-commit will run flake8, black, isort and prettier across all files on commit.
I think you just need to do this first:

$ pre-commit install
$ pre-commit install

### Running tests locally

Expand Down
2 changes: 1 addition & 1 deletion devproject/.python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.10.5
3.11.5
38 changes: 33 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[tool.black]
target-version = ["py310"]

[tool.coverage.report]
omit = ["*/migrations/*"]

Expand All @@ -9,5 +6,36 @@ branch = true
include = ["spectator/*"]
omit = ["*/migrations/*.py"]

[tool.isort]
profile = "black"
[tool.ruff]
extend-exclude = [
"*/migrations/*",
]
line-length = 88
select = [
# Reference: https://docs.astral.sh/ruff/rules/
"B", # flake8-bugbear
"E", # pycodestyle
"F", # Pyflakes
"G", # flake8-logging-format
"I", # isort
"N", # pip8-naming
"Q", # flake8-quotes
"BLE", # flake8-blind-except
"DJ", # flake8-django
"DTZ", # flake8-datetimez
"EM", # flake8-errmsg
"INP", # flake8-no-pep420
"FBT", # flake8-boolean-trap
"PIE", # flake8-pie
"RSE", # flake-raise
"SIM", # flake8-simplify
"T20", # flake8-print
"TID", # flake8-tidy-imports
"UP", # pyupgrade
"RUF100", # unused-noqa
"RUF200", # invalid-pyproject-toml
]
target-version = "py311"

[tool.ruff.lint]
ignore = []
4 changes: 0 additions & 4 deletions setup.cfg

This file was deleted.

10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ def get_entity(package, entity):
eg, get_entity('spectator', 'version') returns `__version__` value in
`__init__.py`.
"""
init_py = open(os.path.join(package, "__init__.py")).read()
find = "__%s__ = ['\"]([^'\"]+)['\"]" % entity
return re.search(find, init_py).group(1)
with open(os.path.join(package, "__init__.py")) as f:
init_py = f.read()
find = "__%s__ = ['\"]([^'\"]+)['\"]" % entity
return re.search(find, init_py).group(1)


def get_version():
Expand Down Expand Up @@ -66,11 +67,10 @@ def get_author_email():

dev_require = [
"django-debug-toolbar",
"flake8",
"black",
"pre-commit",
"python-dotenv",
"pyupgrade",
"ruff",
"unittest-parametrize",
]
tests_require = dev_require + [
Expand Down
21 changes: 11 additions & 10 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ minversion = 1.8

; Should match the strategy matrix in the GitHub Action
envlist =
py38-django{32,41,42,main}
py39-django{32,41,42,main}
py310-django{32,41,42,main}
py311-django{41,42,main}
py312-django{42,main}
flake8
ruff
py38-django{32,41,42}
py39-django{32,41,42}
py310-django{32,41,42,50,main}
py311-django{41,42,50,main}
py312-django{42,50,main}

[gh-actions]
; Maps GitHub Actions python version numbers to tox env vars:
Expand All @@ -26,6 +26,7 @@ DJANGO =
3.2: django32
4.1: django41
4.2: django42
5.0: django40
main: djangomain

; Dependencies and ENV things we need for all environments:
Expand All @@ -41,6 +42,7 @@ deps =
django32: Django >= 3.2, < 3.3
django41: Django >= 4.1, < 4.2
django42: Django >= 4.2, < 4.3
django50: Django >= 5.0, < 5.1
djangomain: https://github.com/django/django/archive/master.tar.gz
extras =
{[base]extras}
Expand All @@ -53,8 +55,7 @@ commands =
coverage run --branch {envbindir}/django-admin test {posargs:}
coverage report -m

[testenv:flake8]
basepython = python3
[testenv:ruff]
skip_install = true
deps = flake8
commands = flake8 {posargs:spectator}
deps = ruff
commands = ruff check {posargs:--show-source .}

0 comments on commit b951f3d

Please sign in to comment.