From d2912400da704c306b094b004a8257bfc49ba646 Mon Sep 17 00:00:00 2001 From: Matt Penny Date: Fri, 6 Dec 2024 12:04:35 +0000 Subject: [PATCH] Configure linting tools --- .flake8 | 10 +++++ Makefile | 15 ++++--- README.md | 8 ++-- mypy.ini | 9 ++++ pyproject.toml | 11 +++++ requirements.txt | 10 +++-- src/octotest/asgi.py | 2 +- src/octotest/settings.py | 96 +++++++++++++++++++++++----------------- src/octotest/urls.py | 2 +- src/octotest/wsgi.py | 2 +- src/task/admin.py | 2 +- src/task/apps.py | 4 +- src/task/models.py | 2 +- src/task/tests.py | 2 +- src/task/urls.py | 2 +- 15 files changed, 112 insertions(+), 65 deletions(-) create mode 100644 .flake8 create mode 100644 mypy.ini create mode 100644 pyproject.toml diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..885895a --- /dev/null +++ b/.flake8 @@ -0,0 +1,10 @@ +[flake8] +ignore = D203, W503 +exclude = + .git, + __pycache__, + */migrations/*, +max-complexity = 10 +ban-relative-imports = true +per-file-ignores = + */__init__.py: F401,E501 diff --git a/Makefile b/Makefile index 7a98a22..e710123 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,7 @@ usage: @echo "build....................Builds the django docker image" @echo "chown....................Change ownership of files to own user" @echo "clear_pyc................Remove all pyc files" + @echo "down.....................Stop the Django server" @echo "help.....................Display available commands" @echo "isort....................Sort Python imports" @echo "isort_check..............Checks Python import are sorted correctly without making changes" @@ -44,9 +45,6 @@ usage: @echo "up.......................Start the Django server" @echo "usage....................Display available commands" -build: - COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose build django - black: @docker-compose run --rm django black src ${ARGS} ifneq ($(OS),Darwin) @@ -56,6 +54,9 @@ endif black_check: $(MAKE) black ARGS="--check" +build: + COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose build django + chown: @docker-compose run --rm django chown -R "`id -u`:`id -u`" "/usr/src/app/${ARGS}" @@ -98,11 +99,11 @@ PG_DB_NAME=postgres PG_DB_USER=postgres PG_DB_PASSWORD=postgres -db: - @docker-compose run --rm -e PGPASSWORD=$(PG_DB_PASSWORD) db psql -h $(PG_DB_HOST) -p $(PG_DB_PORT) -U $(PG_DB_USER) -d $(PG_DB_NAME) $(ARGS) - prep: - @docker-compose run --no-deps --rm django /bin/sh -c "isort src && black src && mypy --cache-dir=/dev/null src && flake8 src" + @docker-compose run --no-deps --rm django /bin/sh -c "isort src && black src && flake8 src && mypy --cache-dir=/dev/null src" + +psql: + @docker-compose run --rm -e PGPASSWORD=$(PG_DB_PASSWORD) db psql -h $(PG_DB_HOST) -p $(PG_DB_PORT) -U $(PG_DB_USER) -d $(PG_DB_NAME) $(ARGS) shell: $(MAKE) manage ARGS="shell ${ARGS}" diff --git a/README.md b/README.md index a28955d..01551f0 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Additional arguments may be provided to some commands by providing the `ARGS=""` ### How do I run the project locally? * Run `make build` to build the container(s) needed. * Run `make up` to start the Django application and any dependent services defined in the compose file. -* Run `make manage ARGS="migrate"` to prepare the database (this runs Django's `python manage.py ${ARGS}` command inside the container). +* Run `make manage ARGS="migrate"` to prepare the database (this runs Django's `python manage.py ${ARGS}` command inside the container). This can be run in another shell window if you didn't run `make up` with the `ARGS="--detach"` flag. * You'll need a user to log into the admin console. To create an admin user for the project run `make manage ARGS="createsuperuser"`. * You should now be able to access the Django admin console from `http://localhost:8000/admin`, and a "Hello, world" page at `http://localhost:8000/task` @@ -40,6 +40,6 @@ Additional arguments may be provided to some commands by providing the `ARGS=""` - `make prep` - run code formatting and linting tools: `black`, `isort`, `mypy`, `lint`. These can also be run individually with the corresponding make command. ### Examples -- Instead of `make test ARGS="path/to/test.py"` -- Instead of running `make test ARGS="-k test_specific_test_case"` -- Instead of `make manage ARGS="createsuperuser` +- Rebuild and re-run the application in detached mode: `make up ARGS="--build --detach"` +- Running a specific test file: `make test ARGS="path/to/test.py"` +- Running tests matching a keyword: `make test ARGS="-k test_specific_test_case"` diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..a200c66 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,9 @@ +[mypy] +plugins = mypy_django_plugin.main +ignore_missing_imports = True + +[mypy-*.migrations.*] +ignore_errors = True + +[mypy.plugins.django-stubs] +django_settings_module = "src.octotest.settings" diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..71f4659 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,11 @@ +[tool.black] +line-length = 79 +skip-numeric-underscore-normalization = 1 + +[tool.isort] +multi_line_output = 3 +include_trailing_comma = "True" +force_grid_wrap = 0 +use_parentheses = "True" +line_length = 79 +skip_glob="**/migrations/**" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index adb5783..a8ec595 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,12 +1,14 @@ asgiref==3.8.1 black==22.3.0 Django==5.1.3 -psycopg2-binary>=2.8 -sqlparse==0.5.2 -typing_extensions==4.12.2 +django-stubs==1.8.0 +django-stubs-ext==0.3.1 flake8==7.0.0 isort==5.6.4 mypy==1.10.0 mypy-extensions==1.0.0 +psycopg2-binary>=2.8 pytest>=7.2.1 -pytest-django>=4.5.2 \ No newline at end of file +pytest-django>=4.5.2 +sqlparse==0.5.2 +typing_extensions==4.12.2 diff --git a/src/octotest/asgi.py b/src/octotest/asgi.py index ea77fde..244f302 100644 --- a/src/octotest/asgi.py +++ b/src/octotest/asgi.py @@ -11,6 +11,6 @@ from django.core.asgi import get_asgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'src.octotest.settings') +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.octotest.settings") application = get_asgi_application() diff --git a/src/octotest/settings.py b/src/octotest/settings.py index 83c4667..31cd7ad 100644 --- a/src/octotest/settings.py +++ b/src/octotest/settings.py @@ -12,6 +12,7 @@ import os from pathlib import Path +from typing import Iterable # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -21,67 +22,69 @@ # See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-8c#%3-*$+ifulrb*$786w@xb$!vg2*db98%g2d+(kh!h-1su((' +SECRET_KEY = ( + "django-insecure-8c#%3-*$+ifulrb*$786w@xb$!vg2*db98%g2d+(kh!h-1su((" +) # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS: Iterable[str] = [] # Application definition INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", ] MIDDLEWARE = [ - 'django.middleware.security.SecurityMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", ] -ROOT_URLCONF = 'src.octotest.urls' +ROOT_URLCONF = "src.octotest.urls" TEMPLATES = [ { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", ], }, }, ] -WSGI_APPLICATION = 'src.octotest.wsgi.application' +WSGI_APPLICATION = "src.octotest.wsgi.application" # Database # https://docs.djangoproject.com/en/5.1/ref/settings/#databases DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql', - 'NAME': os.environ.get('POSTGRES_NAME'), - 'USER': os.environ.get('POSTGRES_USER'), - 'PASSWORD': os.environ.get('POSTGRES_PASSWORD'), - 'HOST': 'db', - 'PORT': 5432, + "default": { + "ENGINE": "django.db.backends.postgresql", + "NAME": os.environ.get("POSTGRES_NAME"), + "USER": os.environ.get("POSTGRES_USER"), + "PASSWORD": os.environ.get("POSTGRES_PASSWORD"), + "HOST": "db", + "PORT": 5432, } } @@ -91,16 +94,27 @@ AUTH_PASSWORD_VALIDATORS = [ { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + "NAME": ( + "django.contrib.auth.password_validation." + "UserAttributeSimilarityValidator" + ), }, { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + "NAME": ( + "django.contrib.auth.password_validation." "MinimumLengthValidator" + ), }, { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + "NAME": ( + "django.contrib.auth.password_validation." + "CommonPasswordValidator" + ), }, { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + "NAME": ( + "django.contrib.auth.password_validation." + "NumericPasswordValidator" + ), }, ] @@ -108,9 +122,9 @@ # Internationalization # https://docs.djangoproject.com/en/5.1/topics/i18n/ -LANGUAGE_CODE = 'en-us' +LANGUAGE_CODE = "en-us" -TIME_ZONE = 'UTC' +TIME_ZONE = "UTC" USE_I18N = True @@ -120,9 +134,9 @@ # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.1/howto/static-files/ -STATIC_URL = 'static/' +STATIC_URL = "static/" # Default primary key field type # https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field -DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" diff --git a/src/octotest/urls.py b/src/octotest/urls.py index ba2af4a..b937a47 100644 --- a/src/octotest/urls.py +++ b/src/octotest/urls.py @@ -19,5 +19,5 @@ urlpatterns = [ path("task/", include("src.task.urls")), - path('admin/', admin.site.urls), + path("admin/", admin.site.urls), ] diff --git a/src/octotest/wsgi.py b/src/octotest/wsgi.py index 41e1ea1..5cd2957 100644 --- a/src/octotest/wsgi.py +++ b/src/octotest/wsgi.py @@ -11,6 +11,6 @@ from django.core.wsgi import get_wsgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'src.octotest.settings') +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.octotest.settings") application = get_wsgi_application() diff --git a/src/task/admin.py b/src/task/admin.py index 8c38f3f..4185d36 100644 --- a/src/task/admin.py +++ b/src/task/admin.py @@ -1,3 +1,3 @@ -from django.contrib import admin +# from django.contrib import admin # Register your models here. diff --git a/src/task/apps.py b/src/task/apps.py index c574e55..a014c1f 100644 --- a/src/task/apps.py +++ b/src/task/apps.py @@ -2,5 +2,5 @@ class TaskConfig(AppConfig): - default_auto_field = 'django.db.models.BigAutoField' - name = 'src.task' + default_auto_field = "django.db.models.BigAutoField" + name = "src.task" diff --git a/src/task/models.py b/src/task/models.py index 71a8362..0b4331b 100644 --- a/src/task/models.py +++ b/src/task/models.py @@ -1,3 +1,3 @@ -from django.db import models +# from django.db import models # Create your models here. diff --git a/src/task/tests.py b/src/task/tests.py index 7ce503c..eb2d433 100644 --- a/src/task/tests.py +++ b/src/task/tests.py @@ -1,3 +1,3 @@ -from django.test import TestCase +# import pytest # Create your tests here. diff --git a/src/task/urls.py b/src/task/urls.py index 2981186..7729f88 100644 --- a/src/task/urls.py +++ b/src/task/urls.py @@ -4,4 +4,4 @@ urlpatterns = [ path("", views.index, name="index"), -] \ No newline at end of file +]