Skip to content

Commit

Permalink
Adds pydocstyle styling with ruff.
Browse files Browse the repository at this point in the history
  • Loading branch information
jevandezande committed Apr 19, 2024
1 parent a1221cf commit da74058
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 131 deletions.
19 changes: 9 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@ repos:
name: ruff-format
stages: [commit]
language: system
entry: poetry run ruff format .
entry: poetry run ruff format . --config pyproject.toml
types: [python]

- id: ruff
name: ruff-check
stages: [commit]
language: system
entry: poetry run ruff check . --fix
entry: poetry run ruff check hooks --fix --config pyproject.toml
types: [python]
exclude: setup.py

- id: mypy
name: mypy
stages: [commit]
language: system
entry: poetry run mypy
types: [python]
require_serial: true

- id: pytest
name: pytest
stages: [commit, push]
language: system
entry: poetry run pytest --cov
types: [python]
pass_filenames: false

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.9.0
hooks:
- id: mypy
args: ['--config-file', 'pyproject.toml']
exclude: '\{\{cookiecutter.package_name\}\}'
40 changes: 18 additions & 22 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Hooks for setting up project once generated."""

import logging
import shutil
import subprocess
Expand All @@ -18,6 +20,7 @@
def call(*inputs: str, **kwargs: Any) -> None:
"""
Call shell commands.
Warning: strings with spaces are not yet supported.
"""
for input in inputs:
Expand All @@ -26,9 +29,7 @@ def call(*inputs: str, **kwargs: Any) -> None:


def set_python_version() -> None:
"""
Set the python version in pyproject.toml and .github/workflows/test.yml
"""
"""Set the python version in pyproject.toml and .github/workflows/test.yml."""
python_version = f"{sys.version_info.major}.{sys.version_info.minor}"
logger.info(f"Settting {python_version=}")
if sys.version_info.minor < 9:
Expand All @@ -48,7 +49,7 @@ def set_python_version() -> None:

def set_license(license: str | None = "MIT") -> None:
"""
Copy the licese file to LICENSE (if any)
Copy the licese file to LICENSE (if any).
:param license: name of the license (or None for no license)
"""
Expand All @@ -73,16 +74,18 @@ def set_license(license: str | None = "MIT") -> None:


def remove_license_dir() -> None:
"""Remove the licenses directory."""
rmtree("licenses")


def git_init() -> None:
"""Initialize a git repository."""
call("git init")


def process_dependency(dependency: str) -> str:
"""
Process a poetry format dependency
Process a poetry format dependency.
>>> process_dependency("pytest")
'pytest = "*"'
Expand All @@ -103,13 +106,13 @@ def process_dependency(dependency: str) -> str:


def process_dependencies(deps: str) -> str:
"""
Process a space separated list of poetry format dependencies
r"""
Process a space separated list of poetry format dependencies.
>>> process_dependencies(' ')
''
>>> process_dependencies("pytest matplotlib@~3.7 black@!=1.2.3")
'pytest = "*"\\nmatplotlib = "~3.7"\\nblack = "!=1.2.3"\\n'
'pytest = "*"\nmatplotlib = "~3.7"\nblack = "!=1.2.3"\n'
"""
if not deps.strip():
return ""
Expand All @@ -118,9 +121,7 @@ def process_dependencies(deps: str) -> str:


def update_dependencies() -> None:
"""
Add and update the dependencies in pyproject.toml and poetry.lock
"""
"""Add and update the dependencies in pyproject.toml and poetry.lock."""
# Extra space and .strip() avoids accidentally creating """"
dependencies = process_dependencies("""{{cookiecutter.poetry_dependencies}} """.strip())
dev_dependencies = process_dependencies("""{{cookiecutter.poetry_dev_dependencies}} """.strip())
Expand All @@ -139,32 +140,26 @@ def update_dependencies() -> None:


def install() -> None:
"""
Install dependencies
"""
"""Install dependencies."""
call("poetry install")


def git_hooks() -> None:
"""
Install pre-commit and pre-push hooks
"""
"""Install pre-commit and pre-push hooks."""
call(
"poetry run pre-commit install -t pre-commit",
"poetry run pre-commit install -t pre-push",
)


def git_initial_commit() -> None:
"""
Make the initial commit
"""
"""Make the initial commit."""
call("git add .", "git commit -m Setup")


def git_add_remote(name: str, url: str, protocol: PROTOCOL = "git") -> None:
"""
Add a remote to the git repository
Add a remote to the git repository.
:param name: name for the remote
:param url: url of remote
Expand All @@ -179,7 +174,7 @@ def git_add_remote(name: str, url: str, protocol: PROTOCOL = "git") -> None:

def github_setup(privacy: str) -> None:
"""
Make a repository on GitHub (requires GitHub CLI)
Make a repository on GitHub (requires GitHub CLI).
:param privacy: privacy of the repository ("private", "internal", "public")
"""
Expand All @@ -201,6 +196,7 @@ def github_setup(privacy: str) -> None:


def main() -> None:
"""Run the post generation hooks."""
set_python_version()
set_license("{{cookiecutter.license}}")
remove_license_dir()
Expand Down
7 changes: 7 additions & 0 deletions hooks/pre_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
"""Hooks to run before generating the project."""

from re import match


def main() -> None:
"""Run the hooks."""
check_module_name("{{cookiecutter.package_name}}")


def check_module_name(module_name: str) -> None:
"""
Check if the module name is a valid Python module name.
:param module_name: the name of the module to check.
:raises ValueError: if the module name is not a valid Python module name.
>>> check_module_name("valid_module_name")
>>> check_module_name("valid_module_name2")
>>> check_module_name("invalid module name")
Expand Down
Loading

0 comments on commit da74058

Please sign in to comment.