Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospri committed Nov 20, 2024
1 parent 6590297 commit 1c30ee0
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 3 deletions.
4 changes: 4 additions & 0 deletions _shared/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def remove_conditional_files():
paths_to_remove.extend([".github/workflows/pypi.yml"])
{% endif %}

{% if cookiecutter.get("linter") != "ruff" %}
paths_to_remove.extend(["tests/pyporject.toml"])
{% endif %}

{% if cookiecutter.get("console_script") != "yes" %}
paths_to_remove.extend(["src/{{ cookiecutter.package_name }}/__main__.py"])
paths_to_remove.extend(["src/{{ cookiecutter.package_name }}/cli.py"])
Expand Down
94 changes: 93 additions & 1 deletion _shared/project/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,87 @@ filterwarnings = [
{% endif %}
]

{% if cookiecutter.get("linter") == "ruff" %}
[tool.ruff]
target-version = "py311"
line-length = 88
exclude = [
"tests/bdd/steps/_compiled_feature_steps.py",
]

[tool.ruff.lint]
select = [
"E", "W", # https://docs.astral.sh/ruff/rules/#pycodestyle-e-w
"D", # https://docs.astral.sh/ruff/rules/#pydocstyle-d
"ARG", # https://docs.astral.sh/ruff/rules/#flake8-unused-arguments-arg
"BLE001", # https://docs.astral.sh/ruff/rules/blind-except/
"R", "PLR", # https://docs.astral.sh/ruff/rules/#refactor-r
"C", "PLC", # https://docs.astral.sh/ruff/rules/#convention-c
"SLF", # flake-8-self
"N", # https://docs.astral.sh/ruff/rules/#pep8-naming-n

"RUF100", # unused-noqa
]

ignore = [
# Missing docstrings.
"D100","D101","D102","D103","D104","D105","D106","D107",

# "No blank lines allowed after function docstring" conflicts with the
# Black code formatter which insists on inserting blank lines after
# function docstrings.
"D202",

# "1 blank line required before class docstring" conflicts with another
# pydocstyle rule D211 "No blank lines allowed before class docstring".
"D203",

# "Multi-line docstring summary should start at the first line"
# and "Multi-line docstring summary should start at the second line".
# These two rules conflict with each other so you have to disable one of them.
# How about we disable them both? PEP 257 says either approach is okay:
#
# > The summary line may be on the same line as the opening quotes or on
# > the next line.
# >
# > https://peps.python.org/pep-0257/#multi-line-docstrings
"D212",
"D213",

# We use Black to format our code automatically, so we don't need PyLint to
# check formatting for us.
"E501", # line-too-long

"PLR2004", # Magic values, we mostly get it on HTTP status codes

# Disabled during the pylint migration, ideally we'll enable this after we are settled in ruff
"RET504",
"RET501",
"PLR6301",
]

[tool.ruff.lint.per-file-ignores]
"tests/*" = [
# Just disable name style checking for the tests, because we
# frequently use lots of argument names that don't conform.
# For example we frequently create pytest fixtures that aren't named in
# snake_case, such as a fixture that returns a mock of the FooBar class would
# be named FooBar in CamelCase.
"N",
# We are more lax about comment formatting in the tests
"D",

"PLR0913",

# Lots of test methods don't use self, but we still want to group our tests
# into classes.
"PLR6301",

"PLR0917", # too-many-arguments
"PLC2701", # private import
"PLR0904", # too-many-public-methods
]
{% else %}
[tool.pydocstyle]
ignore = [
# Missing docstrings.
Expand Down Expand Up @@ -51,6 +132,7 @@ ignore = [
{{ include("pydocstyle/ignores", indent=4) -}}
{% endif %}
]
{% endif %}

[tool.coverage.run]
branch = true
Expand All @@ -77,7 +159,12 @@ show_missing = true
precision = 2
fail_under = 100.00
skip_covered = true
exclude_also = [
# # TYPE_CHECKING block is only executed while running mypy
"if TYPE_CHECKING:"
]

{% if cookiecutter.get("linter") != "ruff" %}
[tool.isort]
multi_line_output = 3
include_trailing_comma = true
Expand Down Expand Up @@ -172,6 +259,7 @@ good-names = [
[tool.pylint.reports]
output-format = "colorized"
score = "no"
{% endif %}

[tool.mypy]
allow_untyped_globals = true
Expand All @@ -180,6 +268,7 @@ pretty = true
warn_unused_configs = true
warn_redundant_casts = true
warn_unused_ignores = true
check_untyped_defs = true

disable_error_code = [
# https://mypy.readthedocs.io/en/stable/error_code_list.html#code-import-untyped
Expand All @@ -190,9 +279,12 @@ disable_error_code = [
]

[[tool.mypy.overrides]]
module= [
module = [
# Don't try to typecheck the tests for now
"tests.*",
{% if include_exists("mypy/ignored_modules") %}
{{- include("mypy/ignored_modules", indent=4) -}}
{% endif %}
]
ignore_errors = true
{% if include_exists("pyproject.toml") %}
Expand Down
4 changes: 4 additions & 0 deletions _shared/project/requirements/checkformatting.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
pip-tools
pip-sync-faster
{% if cookiecutter.get("linter") == "ruff" %}
ruff
{% else %}
black
isort
{% endif %}
{% if include_exists("requirements/checkformatting.in") %}
{{- include("requirements/checkformatting.in") -}}
{% endif %}
4 changes: 4 additions & 0 deletions _shared/project/requirements/format.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
pip-tools
pip-sync-faster
{% if cookiecutter.get("linter") == "ruff" %}
ruff
{% else %}
black
isort
{% endif %}
{% if include_exists("requirements/format.in") %}
{{- include("requirements/format.in") -}}
{% endif %}
4 changes: 4 additions & 0 deletions _shared/project/requirements/lint.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ pip-tools
pip-sync-faster
-r tests.txt
-r functests.txt
{% if cookiecutter.get("linter") == "ruff" %}
ruff
{% else %}
toml # Needed for pydocstyle to support pyproject.toml.
pylint>=3.0.0
pydocstyle
pycodestyle
{% endif %}
{% if include_exists("requirements/lint.in") %}
{{- include("requirements/lint.in") -}}
{% endif %}
8 changes: 8 additions & 0 deletions _shared/project/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ commands =
{% endif %}
{% if cookiecutter._directory in ['pyapp', 'pyramid-app'] %}
dev: {posargs:supervisord -c conf/supervisord-dev.conf}
{% if cookiecutter.get("linter") == "ruff" %}
format: ruff check --select I --fix lms tests bin
format: ruff format lms tests bin
checkformatting: ruff check --select I lms tests bin
checkformatting: ruff format --check lms tests bin
lint: ruff check --preview -q lms tests bin
{% else %}
format: black {{ cookiecutter.package_name }} tests bin
format: isort --atomic {{ cookiecutter.package_name }} tests bin
checkformatting: black --check {{ cookiecutter.package_name }} tests bin
Expand All @@ -125,6 +132,7 @@ commands =
lint: pylint --rcfile=tests/pyproject.toml tests
lint: pydocstyle {{ cookiecutter.package_name }} tests bin
lint: pycodestyle {{ cookiecutter.package_name }} tests bin
{% endif %}
{% else %}
dev: {posargs:ipython --classic --no-banner --no-confirm-exit}
format: black src tests bin
Expand Down
1 change: 1 addition & 0 deletions pyramid-app/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"__frontend_typechecking": "{{ cookiecutter.frontend }}",
"postgres": ["no", "yes"],
"docker": ["no", "yes"],
"linter": ["pylint", "ruff"],
"__postgres_version": "15.3-alpine",
"__postgres_port": "{{ random_port_number() }}",
"__docker_namespace": "{{ cookiecutter.github_owner }}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
python3 -m {{ cookiecutter.package_name }}.scripts.init_db --help
"""
{% if cookiecutter.get("linter") == "pylint" %}
# pylint:disable=import-outside-toplevel,unused-import
{% else %}
# ruff: noqa: PLC0415
{% endif %}
import argparse
import logging
from os import environ
Expand Down Expand Up @@ -48,7 +52,13 @@ def delete(engine: Engine) -> None:
else:
pre_delete(engine)

Base.metadata.drop_all(engine)
with engine.connect() as connection:
# Delete the DB "public" schema directly.
# We do this instead of using SQLAlchemy's drop_all because we want to delete all tables in the current DB.
# For example, this will delete tables created by migrations in other branches, not only the ones SQLAlchemy know about in the current code base.
connection.execute(text("DROP SCHEMA PUBLIC CASCADE;"))
connection.execute(text("CREATE SCHEMA PUBLIC;"))
connection.execute(text("COMMIT;"))

try:
from {{ cookiecutter.package_name }}.db import post_delete
Expand Down Expand Up @@ -108,7 +118,10 @@ def main():
stamped = is_stamped(engine)

if args.create:
if stamped: # pylint:disable=possibly-used-before-assignment
if stamped:
{% if cookiecutter.get("linter") == "pylint" %}
# pylint:disable=possibly-used-before-assignment
{% endif %}
log.warning("Not creating tables because the DB is stamped by Alembic")
else:
create(engine)
Expand Down

0 comments on commit 1c30ee0

Please sign in to comment.