From ef2291485e83133fd5317fd36ea5ed619d84a10f Mon Sep 17 00:00:00 2001 From: Ilya Bushmakin Date: Fri, 18 Aug 2023 17:32:54 +0400 Subject: [PATCH 01/10] feat(project-dev): :art: add ruff lint --- project/config/ruff.toml.jinja | 98 ++++++++++++++++++++++++++++++++++ project/noxfile.py.jinja | 6 +++ 2 files changed, 104 insertions(+) create mode 100644 project/config/ruff.toml.jinja diff --git a/project/config/ruff.toml.jinja b/project/config/ruff.toml.jinja new file mode 100644 index 0000000..27837b2 --- /dev/null +++ b/project/config/ruff.toml.jinja @@ -0,0 +1,98 @@ +target-version = "py38" +line-length = 132 +exclude = [ + "fixtures", + "site", +] +select = [ + "A", + "ANN", + "ARG", + "B", + "BLE", + "C", + "C4", + "COM", + "D", + "DTZ", + "E", + "ERA", + "EXE", + "F", + "FBT", + "G", + "I", + "ICN", + "INP", + "ISC", + "N", + "PGH", + "PIE", + "PL", + "PLC", + "PLE", + "PLR", + "PLW", + "PT", + "PYI", + "Q", + "RUF", + "RSE", + "RET", + "S", + "SIM", + "SLF", + "T", + "T10", + "T20", + "TCH", + "TID", + "TRY", + "UP", + "W", + "YTT", +] +ignore = [ + "A001", # Variable is shadowing a Python builtin + "ANN101", # Missing type annotation for self + "ANN102", # Missing type annotation for cls + "ANN204", # Missing return type annotation for special method __str__ + "ANN401", # Dynamically typed expressions (typing.Any) are disallowed + "ARG005", # Unused lambda argument + "D105", # Missing docstring in magic method + "D417", # Missing argument description in the docstring + "E501", # Line too long + "ERA001", # Commented out code + "G004", # Logging statement uses f-string + "PLR0911", # Too many return statements + "PLR0915", # Too many statements + "SLF001", # Private member accessed + "TRY003", # Avoid specifying long messages outside the exception class +] + +[per-file-ignores] +"src/*/cli.py" = [ + "T201", # Print statement +] +"scripts/*.py" = [ + "INP001", # File is part of an implicit namespace package + "T201", # Print statement +] +"tests/*.py" = [ + "ARG005", # Unused lambda argument + "FBT001", # Boolean positional arg in function definition + "PLR2004", # Magic value used in comparison + "S101", # Use of assert detected +] + +[flake8-quotes] +docstring-quotes = "double" + +[flake8-tidy-imports] +ban-relative-imports = "all" + +[isort] +known-first-party = ["{{ python_package_import_name }}"] + +[pydocstyle] +convention = "google" \ No newline at end of file diff --git a/project/noxfile.py.jinja b/project/noxfile.py.jinja index 1a1b0f8..0c27db3 100644 --- a/project/noxfile.py.jinja +++ b/project/noxfile.py.jinja @@ -9,3 +9,9 @@ os.environ.update(PDM_IGNORE_SAVED_PYTHON="1", PDM_USE_VENV="1") def test(session): session.run_always("pdm", "install", "-G", "ci-tests", external=True) session.run("pytest", "tests/") + + +@nox.session +def lint(session): + session.run_always("pdm", "install", "-G", "ci-quality", external=True) + session.run("ruff", "check", "--config", "config/ruff.toml", "./") From 469c1511c9540e84861fbd03291f6ab94673afed Mon Sep 17 00:00:00 2001 From: Ilya Bushmakin Date: Fri, 18 Aug 2023 17:34:14 +0400 Subject: [PATCH 02/10] ci(template-workflow): :construction_worker: add initial project code quality checks into workflow --- .github/workflows/ci.yml | 1 - tests/test_generate.sh | 15 ++++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ffbad3d..0bfc7ff 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -42,7 +42,6 @@ jobs: git config --global user.name "GitHub Action" git config --global user.email "action@github.com" git config --global init.defaultBranch main - pdm config python.use_venv false - name: Install dependencies run: pip install copier==${{ matrix.copier-version }} copier-templates-extensions diff --git a/tests/test_generate.sh b/tests/test_generate.sh index d202c8e..b4288ae 100755 --- a/tests/test_generate.sh +++ b/tests/test_generate.sh @@ -2,8 +2,21 @@ source tests/setup.sh -pdm install +echo +echo "///////////////////////////////////////////" +echo " TESTING PROJECT" +echo "///////////////////////////////////////////" +echo + pdm run nox -e test +echo +echo "///////////////////////////////////////////" +echo " INITIAL CODE QUALITY CHECKS" +echo "///////////////////////////////////////////" +echo + +pdm run nox -e lint + popd rm -rf $DEST \ No newline at end of file From 84fb48b2f3ab8ba1eb864069956958e7a9f20325 Mon Sep 17 00:00:00 2001 From: Ilya Bushmakin Date: Fri, 18 Aug 2023 17:41:46 +0400 Subject: [PATCH 03/10] feat(project-dev): :sparkles: add format job into nox (using black and ruff) --- project/config/black.toml | 3 +++ project/noxfile.py.jinja | 7 +++++++ 2 files changed, 10 insertions(+) create mode 100644 project/config/black.toml diff --git a/project/config/black.toml b/project/config/black.toml new file mode 100644 index 0000000..7e199b4 --- /dev/null +++ b/project/config/black.toml @@ -0,0 +1,3 @@ +[tool.black] +line-length = 120 +exclude = "tests/fixtures" \ No newline at end of file diff --git a/project/noxfile.py.jinja b/project/noxfile.py.jinja index 0c27db3..6f42cdd 100644 --- a/project/noxfile.py.jinja +++ b/project/noxfile.py.jinja @@ -11,6 +11,13 @@ def test(session): session.run("pytest", "tests/") +@nox.session +def format(session): + session.run_always("pdm", "install", "-G", "ci-quality", external=True) + session.run("ruff", "check", "--config", "config/ruff.toml", "--fix", "./") + session.run("black", "--config", "config/black.toml", "./") + + @nox.session def lint(session): session.run_always("pdm", "install", "-G", "ci-quality", external=True) From 8a628c1f8a8ba527640dd97b2707cdfc4d08f510 Mon Sep 17 00:00:00 2001 From: Ilya Bushmakin Date: Fri, 18 Aug 2023 17:46:53 +0400 Subject: [PATCH 04/10] feat(project-dev): :sparkles: add mypy job into nox --- project/config/mypy.ini | 5 +++++ project/noxfile.py.jinja | 7 +++++++ 2 files changed, 12 insertions(+) create mode 100644 project/config/mypy.ini diff --git a/project/config/mypy.ini b/project/config/mypy.ini new file mode 100644 index 0000000..8627370 --- /dev/null +++ b/project/config/mypy.ini @@ -0,0 +1,5 @@ +[mypy] +ignore_missing_imports = true +exclude = tests/fixtures/ +warn_unused_ignores = true +show_error_codes = true \ No newline at end of file diff --git a/project/noxfile.py.jinja b/project/noxfile.py.jinja index 6f42cdd..d4e1d85 100644 --- a/project/noxfile.py.jinja +++ b/project/noxfile.py.jinja @@ -22,3 +22,10 @@ def format(session): def lint(session): session.run_always("pdm", "install", "-G", "ci-quality", external=True) session.run("ruff", "check", "--config", "config/ruff.toml", "./") + + +@nox.session +def check_types(session): + session.run_always("pdm", "install", "-G", "ci-quality", external=True) + session.run("mypy", "--config-file", "config/mypy.ini", "./") + From b44eb2d4f8ef378e2ea3c5f6e7ab9dd57c4d9f29 Mon Sep 17 00:00:00 2001 From: Ilya Bushmakin Date: Fri, 18 Aug 2023 17:49:27 +0400 Subject: [PATCH 05/10] ci(template-workflow): :construction_worker: add linting check into ci --- tests/test_generate.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_generate.sh b/tests/test_generate.sh index b4288ae..c7df40b 100755 --- a/tests/test_generate.sh +++ b/tests/test_generate.sh @@ -16,6 +16,8 @@ echo " INITIAL CODE QUALITY CHECKS" echo "///////////////////////////////////////////" echo +pdm run nox -e format +pdm run nox -e check_types pdm run nox -e lint popd From f272616f3c09d2ba43290debacc50edb06b46db3 Mon Sep 17 00:00:00 2001 From: Ilya Bushmakin Date: Fri, 18 Aug 2023 17:52:23 +0400 Subject: [PATCH 06/10] fix(template-workflow): :ambulance: add nox back to the ci tests --- tests/test_generate.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_generate.sh b/tests/test_generate.sh index c7df40b..fecadef 100755 --- a/tests/test_generate.sh +++ b/tests/test_generate.sh @@ -2,6 +2,8 @@ source tests/setup.sh +pdm install -G nox + echo echo "///////////////////////////////////////////" echo " TESTING PROJECT" From 08f4e19fe092b4bd5938009b8271116ee513b6d5 Mon Sep 17 00:00:00 2001 From: Ilya Bushmakin Date: Fri, 18 Aug 2023 17:55:51 +0400 Subject: [PATCH 07/10] fix(template-workflow): :ambulance: fix test_generate.sh --- tests/test_generate.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_generate.sh b/tests/test_generate.sh index fecadef..0086051 100755 --- a/tests/test_generate.sh +++ b/tests/test_generate.sh @@ -2,7 +2,7 @@ source tests/setup.sh -pdm install -G nox +pdm install echo echo "///////////////////////////////////////////" From 7358998bf6c73908b876573e32cb733ca4533bb6 Mon Sep 17 00:00:00 2001 From: Ilya Bushmakin Date: Fri, 18 Aug 2023 18:11:39 +0400 Subject: [PATCH 08/10] style(project-automation): :art: fix linting errors --- project/noxfile.py.jinja | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/project/noxfile.py.jinja b/project/noxfile.py.jinja index d4e1d85..10f83eb 100644 --- a/project/noxfile.py.jinja +++ b/project/noxfile.py.jinja @@ -1,31 +1,53 @@ +"""File with common jobs definitions. Run them with 'pdm run nox -e [job_name]'.""" import os import nox +from nox.sessions import Session os.environ.update(PDM_IGNORE_SAVED_PYTHON="1", PDM_USE_VENV="1") @nox.session -def test(session): +def test(session: Session) -> None: + """Run tests. + + Args: + session (Session): nox session object + """ session.run_always("pdm", "install", "-G", "ci-tests", external=True) session.run("pytest", "tests/") @nox.session -def format(session): +def format(session: Session) -> None: + """Run autoformatters. + + Args: + session (Session): nox session object + """ session.run_always("pdm", "install", "-G", "ci-quality", external=True) session.run("ruff", "check", "--config", "config/ruff.toml", "--fix", "./") session.run("black", "--config", "config/black.toml", "./") @nox.session -def lint(session): +def lint(session: Session) -> None: + """Run ruff checks. + + Args: + session (Session): nox session object + """ session.run_always("pdm", "install", "-G", "ci-quality", external=True) session.run("ruff", "check", "--config", "config/ruff.toml", "./") @nox.session -def check_types(session): +def check_types(session: Session) -> None: + """Run type checking. + + Args: + session (Session): nox session object + """ session.run_always("pdm", "install", "-G", "ci-quality", external=True) session.run("mypy", "--config-file", "config/mypy.ini", "./") - + From 654b9389970c2b41215b1fb512ee268eeca8b5f2 Mon Sep 17 00:00:00 2001 From: Ilya Bushmakin Date: Fri, 18 Aug 2023 18:19:56 +0400 Subject: [PATCH 09/10] ci(project-automation): :zap: limit linting and formatting scopes limit linting and formating scopes to src, noxfile and tests --- project/.gitignore | 3 ++- project/noxfile.py.jinja | 13 +++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/project/.gitignore b/project/.gitignore index 7e46def..ccb196c 100644 --- a/project/.gitignore +++ b/project/.gitignore @@ -15,4 +15,5 @@ pdm.toml .pdm-python __pypackages__/ .venv/ -.cache/ \ No newline at end of file +.cache/ +.nox/ \ No newline at end of file diff --git a/project/noxfile.py.jinja b/project/noxfile.py.jinja index 10f83eb..084d799 100644 --- a/project/noxfile.py.jinja +++ b/project/noxfile.py.jinja @@ -6,6 +6,11 @@ from nox.sessions import Session os.environ.update(PDM_IGNORE_SAVED_PYTHON="1", PDM_USE_VENV="1") +PYSRC = [ + "src/", + "noxfile.py", + "tests/" +] @nox.session def test(session: Session) -> None: @@ -26,8 +31,8 @@ def format(session: Session) -> None: session (Session): nox session object """ session.run_always("pdm", "install", "-G", "ci-quality", external=True) - session.run("ruff", "check", "--config", "config/ruff.toml", "--fix", "./") - session.run("black", "--config", "config/black.toml", "./") + session.run("ruff", "check", "--config", "config/ruff.toml", "--fix", *PYSRC) + session.run("black", "--config", "config/black.toml", *PYSRC) @nox.session @@ -38,7 +43,7 @@ def lint(session: Session) -> None: session (Session): nox session object """ session.run_always("pdm", "install", "-G", "ci-quality", external=True) - session.run("ruff", "check", "--config", "config/ruff.toml", "./") + session.run("ruff", "check", "--config", "config/ruff.toml", *PYSRC) @nox.session @@ -49,5 +54,5 @@ def check_types(session: Session) -> None: session (Session): nox session object """ session.run_always("pdm", "install", "-G", "ci-quality", external=True) - session.run("mypy", "--config-file", "config/mypy.ini", "./") + session.run("mypy", "--config-file", "config/mypy.ini", *PYSRC) From e1ee97492a964738656823dd77db2b7dfe1ddb56 Mon Sep 17 00:00:00 2001 From: Ilya Bushmakin Date: Fri, 18 Aug 2023 18:26:43 +0400 Subject: [PATCH 10/10] docs(template-docs): :memo: update README.md --- README.md | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b28bc79..c1610f1 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,15 @@ The template is heavily inspired by [Pawamoy's Copier PDM](https://github.com/pa ### Existing features +- Python 3.9 or above +- Pre-configured tools for code formatting, quality analysis and testing: + - [black](https://github.com/psf/black), + - [ruff](https://github.com/charliermarsh/ruff), + - [mypy](https://github.com/python/mypy), +- Tests run with [pytest](https://github.com/pytest-dev/pytest) and plugins +- [Nox](https://github.com/wntrblm/nox) as task runner +- All licenses from [choosealicense.com](https://choosealicense.com/appendix/) + ### Planned Features - VSCode Dev Containers as development environments @@ -19,32 +28,27 @@ The template is heavily inspired by [Pawamoy's Copier PDM](https://github.com/pa ([Material theme](https://github.com/squidfunk/mkdocs-material) and "autodoc" [mkdocstrings plugin](https://github.com/mkdocstrings/mkdocstrings)) - Pre-configured tools for code formatting, quality analysis and testing: - - [black](https://github.com/psf/black), - [blacken-docs](https://github.com/adamchainz/blacken-docs), - [ssort](https://github.com/bwhmather/ssort), - - [ruff](https://github.com/charliermarsh/ruff), - - [mypy](https://github.com/python/mypy), - [safety](https://github.com/pyupio/safety) -- Tests run with [pytest](https://github.com/pytest-dev/pytest) and plugins, with [coverage](https://github.com/nedbat/coveragepy) support -- [Nox](https://github.com/wntrblm/nox) as task runner -- Python 3.9 or above +- [coverage](https://github.com/nedbat/coveragepy) support for tests - Auto-generated `CHANGELOG.md` from git commits -- All licenses from [choosealicense.com](https://choosealicense.com/appendix/) -- Makefile for convenience - Support for GitHub workflows and GitLab CI/CD ## Quick setup and usage ### Requirements +You need `copier` installed in order to use this template. + ### Starting a project ```bash -copier copy "https://github.com/ilbumi/copier-python-vscode.git" /path/to/your/new/project +copier copy --trust "https://github.com/ilbumi/copier-python-vscode.git" /path/to/your/new/project ``` Or even shorter: ```bash -copier copy "gh:ilbumi/copier-python-vscode" /path/to/your/new/project +copier copy --trust "gh:ilbumi/copier-python-vscode" /path/to/your/new/project ```