Skip to content

Commit

Permalink
Merge pull request #2 from ilbumi/add_linting
Browse files Browse the repository at this point in the history
Add static code analysis into the the project
  • Loading branch information
ilbumi authored Aug 18, 2023
2 parents 78eddff + e1ee974 commit c32b3ae
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 13 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```
3 changes: 2 additions & 1 deletion project/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ pdm.toml
.pdm-python
__pypackages__/
.venv/
.cache/
.cache/
.nox/
3 changes: 3 additions & 0 deletions project/config/black.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool.black]
line-length = 120
exclude = "tests/fixtures"
5 changes: 5 additions & 0 deletions project/config/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[mypy]
ignore_missing_imports = true
exclude = tests/fixtures/
warn_unused_ignores = true
show_error_codes = true
98 changes: 98 additions & 0 deletions project/config/ruff.toml.jinja
Original file line number Diff line number Diff line change
@@ -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"
49 changes: 48 additions & 1 deletion project/noxfile.py.jinja
Original file line number Diff line number Diff line change
@@ -1,11 +1,58 @@
"""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")

PYSRC = [
"src/",
"noxfile.py",
"tests/"
]

@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: 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", *PYSRC)
session.run("black", "--config", "config/black.toml", *PYSRC)


@nox.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", *PYSRC)


@nox.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", *PYSRC)

17 changes: 17 additions & 0 deletions tests/test_generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,24 @@
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 format
pdm run nox -e check_types
pdm run nox -e lint

popd
rm -rf $DEST

0 comments on commit c32b3ae

Please sign in to comment.