Skip to content

Commit

Permalink
Merge pull request #56 from unicef/ruff
Browse files Browse the repository at this point in the history
Django upgrade & Ruff
  • Loading branch information
domdinicola authored Jan 20, 2025
2 parents c0fa9ee + 7c14d50 commit f35f9bb
Show file tree
Hide file tree
Showing 309 changed files with 2,526 additions and 3,216 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ jobs:
docs: ${{ steps.changed_files.outputs.docs }}
steps:
- name: Checkout code
uses: actions/checkout@v4.1.7
uses: actions/checkout@v4
- id: changed_files
name: Check for file changes
uses: dorny/paths-filter@0bc4621a3135347011ad047f9ecf449bf72ce2bd # v3.0.0
uses: dorny/paths-filter@v3
with:
base: ${{ github.ref }}
token: ${{ github.token }}
Expand Down
82 changes: 25 additions & 57 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ name: Lint
on:
push:
branches:
- develop
- master
- staging
- release/*
- feature/*
- bugfix/*
- hotfix/*
- '**'
# pull_request:
# branches: [develop, master]
# types: [synchronize, opened, reopened, ready_for_review]
Expand All @@ -19,70 +13,44 @@ defaults:


concurrency:
group: "${{ github.workflow }}-${{ github.ref }}-lint"
group: "${{ github.workflow }}-${{ github.ref }}"
cancel-in-progress: true


permissions:
contents: read

jobs:
# changes:
## if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
# name: check files
# runs-on: ubuntu-latest
# timeout-minutes: 3
# outputs:
# lint: ${{ steps.changes.outputs.lint }}
# docker: ${{ steps.changes.outputs.docker_base }}
# steps:
# - run: git config --global --add safe.directory $(realpath .)
# - uses: actions/checkout@v4
# - id: changes
# name: Check for backend file changes
# uses: dorny/paths-filter@v3 # v3.0.0
# with:
# base: ${{ github.ref }}
# token: ${{ github.token }}
# filters: .github/file-filters.yml

flake8:
# needs: changes
# if: github.event.pull_request.draft == false && needs.changes.outputs.lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12.6'
- name: Install requirements
run: pip install flake8 pycodestyle
- name: Check syntax
# Stop the build if there are Python syntax errors or undefined names
run: flake8 src/
isort:
# needs: changes
# if: github.event.pull_request.draft == false && needs.changes.outputs.lint
changes:
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
name: check files
runs-on: ubuntu-latest
timeout-minutes: 3
outputs:
lint: ${{ steps.changes.outputs.lint }}
docker: ${{ steps.changes.outputs.docker_base }}
steps:
- run: git config --global --add safe.directory $(realpath .)
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- id: changes
name: Check for backend file changes
uses: dorny/paths-filter@v3
with:
python-version: '3.12.6'
- name: Install requirements
run: pip install isort
- name: iSort
run: isort src/ --check-only
black:
# needs: changes
# if: github.event.pull_request.draft == false && needs.changes.outputs.lint
base: ${{ github.ref }}
token: ${{ github.token }}
filters: .github/file-filters.yml

ruff:
needs: changes
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false && needs.changes.outputs.lint
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12.6'
python-version: '3.12'
- name: Install requirements
run: pip install "black==22.12.0"
- name: Black
run: black src/ --check --config pyproject.toml
run: pip install ruff
- name: Check syntax
# Stop the build if there are Python syntax errors or undefined names
run: ruff check -e
88 changes: 22 additions & 66 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,74 +1,30 @@
exclude: '^$'
fail_fast: false
repos:
- repo: local
hooks:
# Configuration for black exists in pyproject.toml,
# but we let pre-commit take care of the file filtering.
- id: black
name: black
args: [ '--check', '--config', 'pyproject.toml' ]
entry: black
language: python
types: [ python ]
require_serial: true
additional_dependencies: [black]

# Configuration for isort exists in pyproject.toml,
# but we let pre-commit take care of the file filtering.
- id: isort
name: isort
args: [ '--version']
# args: [ '--src', 'src/','--check-only' ]
entry: isort
language: python
types: [ python ]
additional_dependencies: [isort]
# Configuration for black exists in .flake8,
# but we let pre-commit take care of the file filtering.
- id: flake8
name: flake8
args: ["--config", ".flake8"]
entry: flake8
language: python
types: [ python ]
additional_dependencies: [flake8]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
rev: v5.0.0
hooks:

- id: debug-statements
stages: [pre-commit]

- id: end-of-file-fixer
exclude: .bumpversion.cfg
stages: [pre-commit]

- id: check-merge-conflict
stages: [pre-commit]

- id: check-case-conflict
stages: [pre-commit]

- repo: https://github.com/saxix/pch
rev: fc387f44c7c1a51b762a5866ce9d954900e5af68
- id: trailing-whitespace
- repo: https://github.com/adamchainz/djade-pre-commit
rev: 1.3.2
hooks:
- id: check-missed-migrations
- id: djade
args:
- src
stages: [pre-commit]

- id: check-untracked
- --target-version
- '5.1'
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.4
hooks:
- id: ruff
- id: ruff-format
args:
- src
- tests
stages: [pre-push]

# - id: check-forbidden
- --check
# - repo: https://github.com/saxix/pch
# rev: v0.1
# hooks:
# - id: check-missed-migrations
# args:
# - -p
# - /\.showbrowser\(/
# - -p
# - /print\(111/
# stages: [commit]
# - src
# stages:
# - pre-commit
# additional_dependencies:
# - setuptools
8 changes: 6 additions & 2 deletions docs/_hooks/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ def on_pre_build(config: MkDocsConfig) -> None:
pass


def on_page_markdown(markdown: str, page: Page, config: MkDocsConfig, files: Files) -> None:
def on_page_markdown(
markdown: str, page: Page, config: MkDocsConfig, files: Files
) -> None:
pass


def on_page_context(context: TemplateContext, nav: Navigation, page: Page, config: MkDocsConfig) -> None:
def on_page_context(
context: TemplateContext, nav: Navigation, page: Page, config: MkDocsConfig
) -> None:
context["build_date"] = datetime.now().strftime("%a, %d, %b %Y - %H:%M")
5 changes: 4 additions & 1 deletion docs/_scripts/get_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
for entry, cfg in sorted(env.config.items()):
f.write(
MD_LINE.format(
key=entry, default_value=cfg["default"], develop_value=env.get_develop_value(entry), help=cfg["help"]
key=entry,
default_value=cfg["default"],
develop_value=env.get_develop_value(entry),
help=cfg["help"],
)
)
if env.get_develop_value(entry):
Expand Down
File renamed without changes.
23 changes: 17 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,17 @@ dependencies = [
"django-simple-captcha",
"django-simple-math-captcha",
"django-smart-admin",
"django-smart-env>=0.1.0",
"django-smart-env",
"django-strategy-field",
"django-sysinfo",
"django-tinymce",
"django<5",
"django",
"djangorestframework",
"djangorestframework-datatables",
"htmlmin",
"jmespath",
"jsonpickle",
"mini-racer",
"natural-keys",
"psycopg2-binary",
"py-mini-racer",
Expand All @@ -76,13 +77,16 @@ dependencies = [
"uwsgi",
]

[uv]
package = true

[project.optional-dependencies]
docs = [
"mkdocs>=1.6.1",
"mkdocs-material>=9.5.36",
"mkdocs-awesome-pages-plugin>=2.9.3",
"mkdocs",
"mkdocs-material",
"mkdocs-awesome-pages-plugin",
"mkdocstrings-python",
"mkdocs-gen-files>=0.5.0",
"mkdocs-gen-files",
]

[tool.uv]
Expand Down Expand Up @@ -149,3 +153,10 @@ known_django = "django"
sections = ["FUTURE","STDLIB","DJANGO","THIRDPARTY","FIRSTPARTY","LOCALFOLDER"]
include_trailing_comma = true
skip = ["migrations", "snapshots", ".venv"]


[tool.nitpick]
style = [
"github://unicef/hope-code-conventions@main/django/django.toml"
]
cache = "1 day"
91 changes: 91 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
target-version = "py313"
line-length = 120

[lint.isort]
case-sensitive = true

[lint]
select = [
"A", # prevent using keywords that clobber python builtins
# "ANN", # flake8 annotations
"B", # bugbear: security warnings
"BLE", # blind exceptions
"C4", # flake8-comprehensions
"C90", # McCabe complexity
"COM", # flake8-commas
"D", # pydocstyle
"DJ", # flake8-django
"E", # pycodestylex
"E4", "E7", "E9",
"ERA", # eradicate
"F", # pyflakes
"FLY", # flynt
"FURB", # refurb
"I", # isort
"ICN", # flake8-import-conventions
"ISC", # implicit string concatenation
"N", # Pep* naming
"PERF", # perflint
"PIE", # flake8-pie
"PL", # PyLint
"PT", # flake8-pytest-style
"Q", # flake8-quotes
"R", # PyLint Refactor
"RET", # flake8-return
"S", # bandit,
"SIM", # flake8-simplify
"T10", # flake8-debugger
"T20", # flake8-print
"TC", # flake8-type-checking
"UP", # pyupgrade
"W", # pycodestyle warnings
"YTT", # flake8 2020
]
extend-select = ["UP", ]
ignore = [
"ANN401",
"B904", # raise-without-from-inside-except: syntax not compatible with py2
"COM812",
"D100", # Missing docstring in public module
"D101", # Missing docstring in public class
"D102", # Missing docstring in public method
"D103", # Missing docstring in public function
"D104", # Missing docstring in public package
"D105", # Missing docstring in magic method
"D106", # Missing docstring in public nested class
"D107", # Missing docstring in `__init__`
"D203", # one-blank-line-before-class
"D212", # multi-line-summary-first-line
"D213", # multi-line-summary-second-line
"E731", # lambda-assignment: lambdas are substential in maintenance of py2/3 codebase
"ISC001", # conflicts with ruff format command
"RUF005", # collection-literal-concatenation: syntax not compatible with py2
"RUF012", # mutable-class-default: typing is not available for py2
"I001", # unsorted imports https://docs.astral.sh/ruff/rules/unsorted-imports/#unsorted-imports-i001
"UP037", # [*] Remove quotes from type annotation
"UP035", # Import from `collections.abc` instead: `Sequence`
"UP031", # Use format specifiers instead of percent format
"SIM108", # Use ternary operator instead of...
"PLR2004", # Magic value used in comparison
"DJ001", # Avoid using `null=True` on string-based fields such as `CharField`

# todos
"BLE001", # blind exception
"S324", # insecure md5
"C901", # too complex
"PLR0912",
"PLR0913",
"PLR0915",
"PLW0603"
]

[format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"

[lint.per-file-ignores]
"tests/**.py" = ["S101", "PLR2004", "S", "SIM117", "D", "UP", "PLR0913", "ANN", "N999"]
"src/dbtemplates/test**.py" = ["S101", "PLR2004", "S", "SIM117", "D", "UP", "PLR0913", "ANN", "N999", "PT009"]
"src/**/versioning/**.py" = ["N999", ]
Loading

0 comments on commit f35f9bb

Please sign in to comment.