From 029ce5916399dd45f2f15887bf356d5c733e39d5 Mon Sep 17 00:00:00 2001 From: rafsaf Date: Sun, 5 Feb 2023 08:37:15 +0100 Subject: [PATCH] Add pre-commit hooks, removed pre-push script --- hooks/post_gen_project.py | 3 +- setup.cfg | 13 ++++ {{cookiecutter.project_name}}/.flake8 | 2 +- .../.pre-commit-config.yaml | 56 ++++++++++++++++++ {{cookiecutter.project_name}}/alembic/env.py | 11 ++-- ...2023020440_init_user_model_07c71f4389b6.py | 4 +- {{cookiecutter.project_name}}/app/api/deps.py | 2 +- .../app/tests/conftest.py | 2 +- {{cookiecutter.project_name}}/pre-push.sh | 16 ----- .../requirements-dev.txt | 59 ------------------- 10 files changed, 81 insertions(+), 87 deletions(-) create mode 100644 setup.cfg create mode 100644 {{cookiecutter.project_name}}/.pre-commit-config.yaml delete mode 100644 {{cookiecutter.project_name}}/pre-push.sh delete mode 100644 {{cookiecutter.project_name}}/requirements-dev.txt diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 268613a..7317d52 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -1,11 +1,12 @@ """ -Copy content from env template (autogenerated passwords etc) +Copy content from env template (autogenerated passwords etc) to .env which is gitignored, then remove template env file as it is redundant. """ from pathlib import Path PROJECT_NAME = "{{ cookiecutter.project_name }}" + def create_env_file_and_remove_env_template(): env_template_file = Path(".env.template") env_file = Path(".env") diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..5bc90fc --- /dev/null +++ b/setup.cfg @@ -0,0 +1,13 @@ +[flake8] +max-line-length = 88 +select = C,E,F,W,B,B9 +ignore = E203, E501, W503 +exclude = + __init__.py, + .venv, + venv, + __pycache__, + .github, + .vscode, +[isort] +profile = black \ No newline at end of file diff --git a/{{cookiecutter.project_name}}/.flake8 b/{{cookiecutter.project_name}}/.flake8 index 6b8856f..5ac6b4d 100644 --- a/{{cookiecutter.project_name}}/.flake8 +++ b/{{cookiecutter.project_name}}/.flake8 @@ -2,7 +2,7 @@ max-line-length = 88 select = C,E,F,W,B,B9 ignore = E203, E501, W503 -exclude = +exclude = __init__.py, .venv, venv, diff --git a/{{cookiecutter.project_name}}/.pre-commit-config.yaml b/{{cookiecutter.project_name}}/.pre-commit-config.yaml new file mode 100644 index 0000000..c1e26c6 --- /dev/null +++ b/{{cookiecutter.project_name}}/.pre-commit-config.yaml @@ -0,0 +1,56 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 + hooks: + - id: check-yaml + - id: trailing-whitespace + + - repo: https://github.com/myint/autoflake + rev: "v2.0.1" + hooks: + - id: autoflake + args: + [ + "--recursive", + "--in-place", + "--remove-unused-variables", + "--remove-all-unused-imports", + "--ignore-init-module-imports", + ] + + - repo: https://github.com/asottile/pyupgrade + rev: v3.3.1 + hooks: + - id: pyupgrade + args: + [ + "--py3-plus", + "--py36-plus", + "--py37-plus", + "--py38-plus", + "--py39-plus", + "--py310-plus", + "--py311-plus", + ] + files: ".*" + + - repo: https://github.com/psf/black + rev: "23.1.0" + hooks: + - id: black + + - repo: https://github.com/pycqa/isort + rev: "5.12.0" + hooks: + - id: isort + + - repo: https://github.com/PyCQA/flake8 + rev: "6.0.0" + hooks: + - id: flake8 + + - repo: https://github.com/python-poetry/poetry + rev: "1.3.0" + hooks: + - id: poetry-export + args: ["-o", "requirements.txt", "--without-hashes"] diff --git a/{{cookiecutter.project_name}}/alembic/env.py b/{{cookiecutter.project_name}}/alembic/env.py index d48071a..b8bb0cc 100644 --- a/{{cookiecutter.project_name}}/alembic/env.py +++ b/{{cookiecutter.project_name}}/alembic/env.py @@ -1,12 +1,11 @@ +import asyncio from logging.config import fileConfig -from sqlalchemy import engine_from_config -from sqlalchemy import pool +from sqlalchemy import engine_from_config, pool from sqlalchemy.ext.asyncio import AsyncEngine -import asyncio -from app.core import config as app_config -from alembic import context +from alembic import context +from app.core import config as app_config # this is the Alembic Config object, which provides # access to the values within the .ini file in use. @@ -20,7 +19,7 @@ # for 'autogenerate' support # from myapp import mymodel # target_metadata = mymodel.Base.metadata -from app.models import Base +from app.models import Base # noqa target_metadata = Base.metadata diff --git a/{{cookiecutter.project_name}}/alembic/versions/2023020440_init_user_model_07c71f4389b6.py b/{{cookiecutter.project_name}}/alembic/versions/2023020440_init_user_model_07c71f4389b6.py index 72da967..90302fc 100644 --- a/{{cookiecutter.project_name}}/alembic/versions/2023020440_init_user_model_07c71f4389b6.py +++ b/{{cookiecutter.project_name}}/alembic/versions/2023020440_init_user_model_07c71f4389b6.py @@ -1,13 +1,13 @@ """init_user_model Revision ID: 07c71f4389b6 -Revises: +Revises: Create Date: 2023-02-04 23:40:00.426237 """ -from alembic import op import sqlalchemy as sa +from alembic import op # revision identifiers, used by Alembic. revision = "07c71f4389b6" diff --git a/{{cookiecutter.project_name}}/app/api/deps.py b/{{cookiecutter.project_name}}/app/api/deps.py index 2e13701..cbb7c39 100644 --- a/{{cookiecutter.project_name}}/app/api/deps.py +++ b/{{cookiecutter.project_name}}/app/api/deps.py @@ -1,5 +1,5 @@ import time -from typing import AsyncGenerator +from collections.abc import AsyncGenerator import jwt from fastapi import Depends, HTTPException, status diff --git a/{{cookiecutter.project_name}}/app/tests/conftest.py b/{{cookiecutter.project_name}}/app/tests/conftest.py index de0a9d8..c7d816f 100644 --- a/{{cookiecutter.project_name}}/app/tests/conftest.py +++ b/{{cookiecutter.project_name}}/app/tests/conftest.py @@ -1,5 +1,5 @@ import asyncio -from typing import AsyncGenerator +from collections.abc import AsyncGenerator import pytest import pytest_asyncio diff --git a/{{cookiecutter.project_name}}/pre-push.sh b/{{cookiecutter.project_name}}/pre-push.sh deleted file mode 100644 index 8631794..0000000 --- a/{{cookiecutter.project_name}}/pre-push.sh +++ /dev/null @@ -1,16 +0,0 @@ -echo "export requirements.txt" -poetry export -o requirements.txt --without-hashes -poetry export -o requirements-dev.txt --dev --without-hashes -echo "autoflake" -autoflake --recursive --in-place \ - --remove-unused-variables \ - --remove-all-unused-imports \ - --ignore-init-module-imports \ - app -echo "black" -black app -echo "isort" -isort app -echo "flake8" -flake8 app --count --statistics -echo "OK" \ No newline at end of file diff --git a/{{cookiecutter.project_name}}/requirements-dev.txt b/{{cookiecutter.project_name}}/requirements-dev.txt deleted file mode 100644 index 474ef7f..0000000 --- a/{{cookiecutter.project_name}}/requirements-dev.txt +++ /dev/null @@ -1,59 +0,0 @@ -alembic==1.9.2 ; python_version >= "3.11" and python_version < "4.0" -anyio==3.6.2 ; python_version >= "3.11" and python_version < "4.0" -asyncpg==0.27.0 ; python_version >= "3.11" and python_version < "4.0" -attrs==22.2.0 ; python_version >= "3.11" and python_version < "4.0" -autoflake==2.0.1 ; python_version >= "3.11" and python_version < "4.0" -bcrypt==4.0.1 ; python_version >= "3.11" and python_version < "4.0" -black==23.1.0 ; python_version >= "3.11" and python_version < "4.0" -certifi==2022.12.7 ; python_version >= "3.11" and python_version < "4.0" -cffi==1.15.1 ; python_version >= "3.11" and python_version < "4.0" -cfgv==3.3.1 ; python_version >= "3.11" and python_version < "4.0" -click==8.1.3 ; python_version >= "3.11" and python_version < "4.0" -colorama==0.4.6 ; python_version >= "3.11" and python_version < "4.0" and sys_platform == "win32" or python_version >= "3.11" and python_version < "4.0" and platform_system == "Windows" -coverage==7.1.0 ; python_version >= "3.11" and python_version < "4.0" -cryptography==39.0.0 ; python_version >= "3.11" and python_version < "4.0" -distlib==0.3.6 ; python_version >= "3.11" and python_version < "4.0" -dnspython==2.3.0 ; python_version >= "3.11" and python_version < "4.0" -email-validator==1.3.1 ; python_version >= "3.11" and python_version < "4.0" -fastapi==0.89.1 ; python_version >= "3.11" and python_version < "4.0" -filelock==3.9.0 ; python_version >= "3.11" and python_version < "4.0" -flake8==6.0.0 ; python_version >= "3.11" and python_version < "4.0" -greenlet==2.0.2 ; python_version >= "3.11" and python_version < "4.0" and platform_machine == "aarch64" or python_version >= "3.11" and python_version < "4.0" and platform_machine == "ppc64le" or python_version >= "3.11" and python_version < "4.0" and platform_machine == "x86_64" or python_version >= "3.11" and python_version < "4.0" and platform_machine == "amd64" or python_version >= "3.11" and python_version < "4.0" and platform_machine == "AMD64" or python_version >= "3.11" and python_version < "4.0" and platform_machine == "win32" or python_version >= "3.11" and python_version < "4.0" and platform_machine == "WIN32" -h11==0.14.0 ; python_version >= "3.11" and python_version < "4.0" -httpcore==0.16.3 ; python_version >= "3.11" and python_version < "4.0" -httpx==0.23.3 ; python_version >= "3.11" and python_version < "4.0" -identify==2.5.17 ; python_version >= "3.11" and python_version < "4.0" -idna==3.4 ; python_version >= "3.11" and python_version < "4.0" -iniconfig==2.0.0 ; python_version >= "3.11" and python_version < "4.0" -isort==5.12.0 ; python_version >= "3.11" and python_version < "4.0" -mako==1.2.4 ; python_version >= "3.11" and python_version < "4.0" -markupsafe==2.1.2 ; python_version >= "3.11" and python_version < "4.0" -mccabe==0.7.0 ; python_version >= "3.11" and python_version < "4.0" -mypy-extensions==1.0.0 ; python_version >= "3.11" and python_version < "4.0" -nodeenv==1.7.0 ; python_version >= "3.11" and python_version < "4.0" -packaging==23.0 ; python_version >= "3.11" and python_version < "4.0" -passlib[bcrypt]==1.7.4 ; python_version >= "3.11" and python_version < "4.0" -pathspec==0.11.0 ; python_version >= "3.11" and python_version < "4.0" -platformdirs==2.6.2 ; python_version >= "3.11" and python_version < "4.0" -pluggy==1.0.0 ; python_version >= "3.11" and python_version < "4.0" -pre-commit==3.0.4 ; python_version >= "3.11" and python_version < "4.0" -pycodestyle==2.10.0 ; python_version >= "3.11" and python_version < "4.0" -pycparser==2.21 ; python_version >= "3.11" and python_version < "4.0" -pydantic==1.10.4 ; python_version >= "3.11" and python_version < "4.0" -pydantic[dotenv,email]==1.10.4 ; python_version >= "3.11" and python_version < "4.0" -pyflakes==3.0.1 ; python_version >= "3.11" and python_version < "4.0" -pyjwt[crypto]==2.6.0 ; python_version >= "3.11" and python_version < "4.0" -pytest-asyncio==0.20.3 ; python_version >= "3.11" and python_version < "4.0" -pytest==7.2.1 ; python_version >= "3.11" and python_version < "4.0" -python-dotenv==0.21.1 ; python_version >= "3.11" and python_version < "4.0" -python-multipart==0.0.5 ; python_version >= "3.11" and python_version < "4.0" -pyyaml==6.0 ; python_version >= "3.11" and python_version < "4.0" -rfc3986[idna2008]==1.5.0 ; python_version >= "3.11" and python_version < "4.0" -setuptools==67.1.0 ; python_version >= "3.11" and python_version < "4.0" -six==1.16.0 ; python_version >= "3.11" and python_version < "4.0" -sniffio==1.3.0 ; python_version >= "3.11" and python_version < "4.0" -sqlalchemy==2.0.1 ; python_version >= "3.11" and python_version < "4.0" -starlette==0.22.0 ; python_version >= "3.11" and python_version < "4.0" -typing-extensions==4.4.0 ; python_version >= "3.11" and python_version < "4.0" -uvicorn==0.20.0 ; python_version >= "3.11" and python_version < "4.0" -virtualenv==20.17.1 ; python_version >= "3.11" and python_version < "4.0"