Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flake8 replaced with ruff. #211

Merged
merged 5 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ jobs:
if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.9'
python-version: '3.11'
- name: Install deps
run: |
pip install -U pip
pip install poetry==1.4.2
pip install poetry==1.8.2
poetry install
env:
POETRY_VIRTUALENVS_CREATE: false
POETRY_VIRTUALENVS_CREATE: "False"
- name: Setup GIT
run: |
git config --global user.name "fastapi_template"
git config --global user.email "[email protected]"
- name: Run tests
run: poetry run pytest -vv
run: pytest -vv
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,22 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: check-ast
- id: trailing-whitespace
- id: check-toml
- id: end-of-file-fixer

- repo: https://github.com/asottile/add-trailing-comma
rev: v2.1.0
hooks:
- id: add-trailing-comma

- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.1.0
hooks:
- id: pretty-format-yaml
args:
- --autofix
- --preserve-quotes
- --indent=2

- repo: local
hooks:
- id: autoflake
name: autoflake
entry: poetry run autoflake
language: system
types: [python]
args: [--in-place, --remove-all-unused-imports, --remove-duplicate-keys]

- id: black
name: Format with Black
entry: poetry run black
language: system
types: [python]

- id: isort
name: isort
entry: poetry run isort
language: system
types: [python]

- id: flake8
name: Check with Flake8
entry: poetry run flake8
- id: ruff
name: Check with Ruff
entry: poetry run ruff
language: system
pass_filenames: false
types: [python]
args: [--count, .]
always_run: true
args: ["check", "{{cookiecutter.project_name}}", "tests", "--fix"]

- id: mypy
name: Validate types with MyPy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ RUN apt-get update && apt-get install -y \
{%- endif %}


RUN pip install poetry==1.4.2
RUN pip install poetry==1.8.2

# Configuring poetry
RUN poetry config virtualenvs.create false
RUN poetry config cache-dir /tmp/poetry_cache

# Copying requirements of a project
COPY pyproject.toml poetry.lock /app/src/
WORKDIR /app/src

# Installing requirements
RUN poetry install --only main
RUN --mount=type=cache,target=/tmp/poetry_cache poetry install --only main

{%- if cookiecutter.db_info.name == "mysql" or cookiecutter.db_info.name == "postgresql" %}
# Removing gcc
Expand All @@ -37,10 +38,10 @@ RUN apt-get purge -y \

# Copying actuall application
COPY . /app/src/
RUN poetry install --only main
RUN --mount=type=cache,target=/tmp/poetry_cache poetry install --only main

CMD ["/usr/local/bin/python", "-m", "{{cookiecutter.project_name}}"]

FROM prod as dev

RUN poetry install
RUN --mount=type=cache,target=/tmp/poetry_cache poetry install
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"{{cookiecutter.project_name}}/web/api/redis",
"{{cookiecutter.project_name}}/web/gql/redis",
"{{cookiecutter.project_name}}/services/redis",
"{{cookiecutter.project_name}}/tests/test_redis.py",
"tests/test_redis.py",
"deploy/kube/redis.yml"
]
},
Expand All @@ -31,7 +31,7 @@
"{{cookiecutter.project_name}}/web/api/rabbit",
"{{cookiecutter.project_name}}/web/gql/rabbit",
"{{cookiecutter.project_name}}/services/rabbit",
"{{cookiecutter.project_name}}/tests/test_rabbit.py",
"tests/test_rabbit.py",
"deploy/kube/redis.yml"
]
},
Expand All @@ -41,7 +41,7 @@
"{{cookiecutter.project_name}}/web/api/kafka",
"{{cookiecutter.project_name}}/web/gql/kafka",
"{{cookiecutter.project_name}}/services/kafka",
"{{cookiecutter.project_name}}/tests/test_kafka.py"
"tests/test_kafka.py"
]
},
"Kubernetes": {
Expand All @@ -56,7 +56,7 @@
"alembic.ini",
"{{cookiecutter.project_name}}/web/api/dummy",
"{{cookiecutter.project_name}}/web/gql/dummy",
"{{cookiecutter.project_name}}/tests/test_dummy.py",
"tests/test_dummy.py",
"deploy/kube/db.yml"
]
},
Expand Down Expand Up @@ -119,10 +119,10 @@
"{{cookiecutter.project_name}}/web/gql/kafka",
"{{cookiecutter.project_name}}/web/api/rabbit",
"{{cookiecutter.project_name}}/web/gql/rabbit",
"{{cookiecutter.project_name}}/tests/test_echo.py",
"{{cookiecutter.project_name}}/tests/test_dummy.py",
"{{cookiecutter.project_name}}/tests/test_redis.py",
"{{cookiecutter.project_name}}/tests/test_rabbit.py"
"tests/test_echo.py",
"tests/test_dummy.py",
"tests/test_redis.py",
"tests/test_rabbit.py"
]
},
"Users model": {
Expand All @@ -145,7 +145,7 @@
"{{cookiecutter.project_name}}/db_tortoise/models/dummy_model.py",
"{{cookiecutter.project_name}}/db_psycopg/dao",
"{{cookiecutter.project_name}}/db_psycopg/models/dummy_model.py",
"{{cookiecutter.project_name}}/tests/test_dummy.py",
"tests/test_dummy.py",
"{{cookiecutter.project_name}}/db_piccolo/dao",
"{{cookiecutter.project_name}}/db_piccolo/models/dummy_model.py",
"{{cookiecutter.project_name}}/db_beanie/models/dummy_model.py",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.9'

services:
api:
ports:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
version: '3.9'

services:
api: &main_app
build:
context: .
dockerfile: ./deploy/Dockerfile
dockerfile: ./Dockerfile
target: prod
image: {{cookiecutter.project_name}}:{{"${" }}{{cookiecutter.project_name | upper }}_VERSION:-latest{{"}"}}
restart: always
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,10 @@ pyzmq = "^25"

[tool.poetry.dev-dependencies]
pytest = "^7.2.1"
flake8 = "~4.0.1"
ruff = "^0"
mypy = "^1.1.1"
isort = "^5.11.4"
pre-commit = "^3.0.1"
wemake-python-styleguide = "^0.17.0"
black = "^22.12.0"
autoflake = "^1.6.1"
pytest-cov = "^4.0.0"
anyio = "^3.6.2"
pytest-env = "^0.8.1"
Expand Down Expand Up @@ -247,6 +244,74 @@ src_folder = "./{{cookiecutter.project_name}}"
{%- endif %}
{%- endif %}

[tool.ruff]
# List of enabled rulsets.
# See https://docs.astral.sh/ruff/rules/ for more information.
lint.select = [
"E", # Error
"F", # Pyflakes
"W", # Pycodestyle
"C90", # McCabe complexity
"I", # Isort
"N", # pep8-naming
"D", # Pydocstyle
"ANN", # Pytype annotations
"S", # Bandit
"B", # Bugbear
"COM", # Commas
"C4", # Comprehensions
"ISC", # Implicit string concat
"PIE", # Unnecessary code
"T20", # Catch prints
"PYI", # validate pyi files
"Q", # Checks for quotes
"RSE", # Checks raise statements
"RET", # Checks return statements
"SLF", # Self checks
"SIM", # Simplificator
"PTH", # Pathlib checks
"ERA", # Checks for commented out code
"PL", # PyLint checks
"RUF", # Specific to Ruff checks
]
lint.ignore = [
"D105", # Missing docstring in magic method
"D107", # Missing docstring in __init__
"B008", # Do not perform function calls in argument defaults
"D211", # No blank lines allowed before class docstring
"D212", # Multi-line docstring summary should start at the first line
"D401", # First line should be in imperative mood
"D104", # Missing docstring in public package
"D100", # Missing docstring in public module
"D202", # No blank lines allowed after function docstring
"ANN102", # Missing type annotation for self in method
"ANN101", # Missing type annotation for argument
"ANN401", # typing.Any are disallowed in `**kwargs
"PLR0913", # Too many arguments for function call
"D106", # Missing docstring in public nested class
]
exclude = [
{%- if cookiecutter.orm in ["ormar", "sqlalchemy", "piccolo", "tortoise"] %}
"{{ cookiecutter.project_name }}/db/migrations",
{%- endif %}
".venv/"
]
lint.mccabe = { max-complexity = 10 }
line-length = 88

[tool.ruff.lint.per-file-ignores]
"tests/*" = [
"S101", # Use of assert detected
]

[tool.ruff.lint.pydocstyle]
convention = "pep257"
ignore-decorators = ["typing.overload"]

[tool.ruff.lint.pylint]
allow-magic-value-types = ["int", "str", "float", "bytes"]


[fastapi-template.options]
{%- for key, value in cookiecutter.items() %}
{%- if not key.startswith("_") and not key == "db_info" %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,18 @@ async def setup_db() -> AsyncGenerator[None, None]:
async def setup_db() -> AsyncGenerator[None, None]:
"""
Fixture to create database connection.
:yield: nothing.
"""
client = AsyncIOMotorClient(settings.db_url.human_repr()) # type: ignore
from {{cookiecutter.project_name}}.db.models import load_all_models # noqa: WPS433
await beanie.init_beanie(
database=client[settings.db_base],
document_models=load_all_models(),
document_models=load_all_models(), # type: ignore
)
yield


{%- endif %}

{%- if cookiecutter.enable_rmq == 'True' %}
Expand Down Expand Up @@ -509,7 +509,7 @@ def fastapi_app(
{%- if cookiecutter.enable_kafka == "True" %}
application.dependency_overrides[get_kafka_producer] = lambda: test_kafka_producer
{%- endif %}
return application # noqa: WPS331
return application # noqa: RET504


@pytest.fixture
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import shutil
from pathlib import Path

import uvicorn

Expand All @@ -25,8 +26,8 @@ def set_multiproc_dir() -> None:
to avoid undefined behaviour.
"""
shutil.rmtree(settings.prometheus_dir, ignore_errors=True)
os.makedirs(settings.prometheus_dir, exist_ok=True)
os.environ["prometheus_multiproc_dir"] = str(
Path(settings.prometheus_dir).mkdir(parents=True)
os.environ["prometheus_multiproc_dir"] = str( # noqa: SIM112
settings.prometheus_dir.expanduser().absolute(),
)
os.environ["PROMETHEUS_MULTIPROC_DIR"] = str(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
from {{cookiecutter.project_name}}.db.models.dummy_model import DummyModel
{%- endif %}

from beanie import Document
from typing import Type, Sequence

def load_all_models(): # type: ignore
def load_all_models() -> Sequence[Type[Document]]:
"""Load all models from this folder.""" # noqa: DAR201
return [
{%- if cookiecutter.add_dummy == "True" %}
DummyModel, # type: ignore
DummyModel,
{%- endif %}
]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os

from pathlib import Path
from sqlalchemy import text
from sqlalchemy.engine import URL, create_engine, make_url
from {{cookiecutter.project_name}}.settings import settings
Expand Down Expand Up @@ -81,6 +82,6 @@ def create_database() -> None:
def drop_database() -> None:
"""Drop current database."""
if settings.db_file.exists():
os.remove(settings.db_file)
Path(settings.db_file).unlink()

{%- endif %}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import os

from piccolo.conf.apps import AppConfig, table_finder
from pathlib import Path

CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
CURRENT_DIRECTORY = Path(__file__).parent


APP_CONFIG = AppConfig(
app_name='{{cookiecutter.project_name}}_db',
migrations_folder_path=os.path.join(
CURRENT_DIRECTORY,
'migrations'
),
migrations_folder_path=str(CURRENT_DIRECTORY / 'migrations'),
table_classes=table_finder(modules=[
{%- if cookiecutter.add_dummy == "True" %}
"{{cookiecutter.project_name}}.db.models.dummy_model"
Expand Down
Loading
Loading