Skip to content

Commit 63d7f21

Browse files
authored
Flake8 replaced with ruff. (#211)
1 parent f23c99d commit 63d7f21

28 files changed

Lines changed: 592 additions & 730 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ jobs:
2323
if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
2424
runs-on: ubuntu-latest
2525
steps:
26-
- uses: actions/checkout@v2
26+
- uses: actions/checkout@v4
2727
- name: Set up Python
2828
uses: actions/setup-python@v2
2929
with:
30-
python-version: '3.9'
30+
python-version: '3.11'
3131
- name: Install deps
3232
run: |
3333
pip install -U pip
34-
pip install poetry==1.4.2
34+
pip install poetry==1.8.2
3535
poetry install
3636
env:
37-
POETRY_VIRTUALENVS_CREATE: false
37+
POETRY_VIRTUALENVS_CREATE: "False"
3838
- name: Setup GIT
3939
run: |
4040
git config --global user.name "fastapi_template"
4141
git config --global user.email "fastapi_template@pytest.python"
4242
- name: Run tests
43-
run: poetry run pytest -vv
43+
run: pytest -vv

‎fastapi_template/template/{{cookiecutter.project_name}}/.pre-commit-config.yaml‎

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,22 @@
22
# See https://pre-commit.com for more information
33
# See https://pre-commit.com/hooks.html for more hooks
44
repos:
5-
- repo: https://github.com/pre-commit/pre-commit-hooks
6-
rev: v2.4.0
7-
hooks:
8-
- id: check-ast
9-
- id: trailing-whitespace
10-
- id: check-toml
11-
- id: end-of-file-fixer
12-
13-
- repo: https://github.com/asottile/add-trailing-comma
14-
rev: v2.1.0
15-
hooks:
16-
- id: add-trailing-comma
17-
18-
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
19-
rev: v2.1.0
20-
hooks:
21-
- id: pretty-format-yaml
22-
args:
23-
- --autofix
24-
- --preserve-quotes
25-
- --indent=2
26-
275
- repo: local
286
hooks:
29-
- id: autoflake
30-
name: autoflake
31-
entry: poetry run autoflake
32-
language: system
33-
types: [python]
34-
args: [--in-place, --remove-all-unused-imports, --remove-duplicate-keys]
357

368
- id: black
379
name: Format with Black
3810
entry: poetry run black
3911
language: system
4012
types: [python]
4113

42-
- id: isort
43-
name: isort
44-
entry: poetry run isort
45-
language: system
46-
types: [python]
47-
48-
- id: flake8
49-
name: Check with Flake8
50-
entry: poetry run flake8
14+
- id: ruff
15+
name: Check with Ruff
16+
entry: poetry run ruff
5117
language: system
5218
pass_filenames: false
53-
types: [python]
54-
args: [--count, .]
19+
always_run: true
20+
args: ["check", "{{cookiecutter.project_name}}", "tests", "--fix"]
5521

5622
- id: mypy
5723
name: Validate types with MyPy

fastapi_template/template/{{cookiecutter.project_name}}/deploy/Dockerfile renamed to fastapi_template/template/{{cookiecutter.project_name}}/Dockerfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ RUN apt-get update && apt-get install -y \
1616
{%- endif %}
1717

1818

19-
RUN pip install poetry==1.4.2
19+
RUN pip install poetry==1.8.2
2020

2121
# Configuring poetry
2222
RUN poetry config virtualenvs.create false
23+
RUN poetry config cache-dir /tmp/poetry_cache
2324

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

2829
# Installing requirements
29-
RUN poetry install --only main
30+
RUN --mount=type=cache,target=/tmp/poetry_cache poetry install --only main
3031

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

3839
# Copying actuall application
3940
COPY . /app/src/
40-
RUN poetry install --only main
41+
RUN --mount=type=cache,target=/tmp/poetry_cache poetry install --only main
4142

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

4445
FROM prod as dev
4546

46-
RUN poetry install
47+
RUN --mount=type=cache,target=/tmp/poetry_cache poetry install

‎fastapi_template/template/{{cookiecutter.project_name}}/conditional_files.json‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"{{cookiecutter.project_name}}/web/api/redis",
2222
"{{cookiecutter.project_name}}/web/gql/redis",
2323
"{{cookiecutter.project_name}}/services/redis",
24-
"{{cookiecutter.project_name}}/tests/test_redis.py",
24+
"tests/test_redis.py",
2525
"deploy/kube/redis.yml"
2626
]
2727
},
@@ -31,7 +31,7 @@
3131
"{{cookiecutter.project_name}}/web/api/rabbit",
3232
"{{cookiecutter.project_name}}/web/gql/rabbit",
3333
"{{cookiecutter.project_name}}/services/rabbit",
34-
"{{cookiecutter.project_name}}/tests/test_rabbit.py",
34+
"tests/test_rabbit.py",
3535
"deploy/kube/redis.yml"
3636
]
3737
},
@@ -41,7 +41,7 @@
4141
"{{cookiecutter.project_name}}/web/api/kafka",
4242
"{{cookiecutter.project_name}}/web/gql/kafka",
4343
"{{cookiecutter.project_name}}/services/kafka",
44-
"{{cookiecutter.project_name}}/tests/test_kafka.py"
44+
"tests/test_kafka.py"
4545
]
4646
},
4747
"Kubernetes": {
@@ -56,7 +56,7 @@
5656
"alembic.ini",
5757
"{{cookiecutter.project_name}}/web/api/dummy",
5858
"{{cookiecutter.project_name}}/web/gql/dummy",
59-
"{{cookiecutter.project_name}}/tests/test_dummy.py",
59+
"tests/test_dummy.py",
6060
"deploy/kube/db.yml"
6161
]
6262
},
@@ -119,10 +119,10 @@
119119
"{{cookiecutter.project_name}}/web/gql/kafka",
120120
"{{cookiecutter.project_name}}/web/api/rabbit",
121121
"{{cookiecutter.project_name}}/web/gql/rabbit",
122-
"{{cookiecutter.project_name}}/tests/test_echo.py",
123-
"{{cookiecutter.project_name}}/tests/test_dummy.py",
124-
"{{cookiecutter.project_name}}/tests/test_redis.py",
125-
"{{cookiecutter.project_name}}/tests/test_rabbit.py"
122+
"tests/test_echo.py",
123+
"tests/test_dummy.py",
124+
"tests/test_redis.py",
125+
"tests/test_rabbit.py"
126126
]
127127
},
128128
"Users model": {
@@ -145,7 +145,7 @@
145145
"{{cookiecutter.project_name}}/db_tortoise/models/dummy_model.py",
146146
"{{cookiecutter.project_name}}/db_psycopg/dao",
147147
"{{cookiecutter.project_name}}/db_psycopg/models/dummy_model.py",
148-
"{{cookiecutter.project_name}}/tests/test_dummy.py",
148+
"tests/test_dummy.py",
149149
"{{cookiecutter.project_name}}/db_piccolo/dao",
150150
"{{cookiecutter.project_name}}/db_piccolo/models/dummy_model.py",
151151
"{{cookiecutter.project_name}}/db_beanie/models/dummy_model.py",

‎fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.dev.yml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3.9'
2-
31
services:
42
api:
53
ports:

‎fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
version: '3.9'
2-
31
services:
42
api: &main_app
53
build:
64
context: .
7-
dockerfile: ./deploy/Dockerfile
5+
dockerfile: ./Dockerfile
86
target: prod
97
image: {{cookiecutter.project_name}}:{{"${" }}{{cookiecutter.project_name | upper }}_VERSION:-latest{{"}"}}
108
restart: always

‎fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml‎

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,10 @@ pyzmq = "^25"
160160

161161
[tool.poetry.dev-dependencies]
162162
pytest = "^7.2.1"
163-
flake8 = "~4.0.1"
163+
ruff = "^0"
164164
mypy = "^1.1.1"
165-
isort = "^5.11.4"
166165
pre-commit = "^3.0.1"
167-
wemake-python-styleguide = "^0.17.0"
168166
black = "^22.12.0"
169-
autoflake = "^1.6.1"
170167
pytest-cov = "^4.0.0"
171168
anyio = "^3.6.2"
172169
pytest-env = "^0.8.1"
@@ -247,6 +244,74 @@ src_folder = "./{{cookiecutter.project_name}}"
247244
{%- endif %}
248245
{%- endif %}
249246

247+
[tool.ruff]
248+
# List of enabled rulsets.
249+
# See https://docs.astral.sh/ruff/rules/ for more information.
250+
lint.select = [
251+
"E", # Error
252+
"F", # Pyflakes
253+
"W", # Pycodestyle
254+
"C90", # McCabe complexity
255+
"I", # Isort
256+
"N", # pep8-naming
257+
"D", # Pydocstyle
258+
"ANN", # Pytype annotations
259+
"S", # Bandit
260+
"B", # Bugbear
261+
"COM", # Commas
262+
"C4", # Comprehensions
263+
"ISC", # Implicit string concat
264+
"PIE", # Unnecessary code
265+
"T20", # Catch prints
266+
"PYI", # validate pyi files
267+
"Q", # Checks for quotes
268+
"RSE", # Checks raise statements
269+
"RET", # Checks return statements
270+
"SLF", # Self checks
271+
"SIM", # Simplificator
272+
"PTH", # Pathlib checks
273+
"ERA", # Checks for commented out code
274+
"PL", # PyLint checks
275+
"RUF", # Specific to Ruff checks
276+
]
277+
lint.ignore = [
278+
"D105", # Missing docstring in magic method
279+
"D107", # Missing docstring in __init__
280+
"B008", # Do not perform function calls in argument defaults
281+
"D211", # No blank lines allowed before class docstring
282+
"D212", # Multi-line docstring summary should start at the first line
283+
"D401", # First line should be in imperative mood
284+
"D104", # Missing docstring in public package
285+
"D100", # Missing docstring in public module
286+
"D202", # No blank lines allowed after function docstring
287+
"ANN102", # Missing type annotation for self in method
288+
"ANN101", # Missing type annotation for argument
289+
"ANN401", # typing.Any are disallowed in `**kwargs
290+
"PLR0913", # Too many arguments for function call
291+
"D106", # Missing docstring in public nested class
292+
]
293+
exclude = [
294+
{%- if cookiecutter.orm in ["ormar", "sqlalchemy", "piccolo", "tortoise"] %}
295+
"{{ cookiecutter.project_name }}/db/migrations",
296+
{%- endif %}
297+
".venv/"
298+
]
299+
lint.mccabe = { max-complexity = 10 }
300+
line-length = 88
301+
302+
[tool.ruff.lint.per-file-ignores]
303+
"tests/*" = [
304+
"S101", # Use of assert detected
305+
]
306+
307+
[tool.ruff.lint.pydocstyle]
308+
convention = "pep257"
309+
ignore-decorators = ["typing.overload"]
310+
311+
[tool.ruff.lint.pylint]
312+
allow-magic-value-types = ["int", "str", "float", "bytes"]
313+
314+
250315
[fastapi-template.options]
251316
{%- for key, value in cookiecutter.items() %}
252317
{%- if not key.startswith("_") and not key == "db_info" %}

fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/tests/__init__.py renamed to fastapi_template/template/{{cookiecutter.project_name}}/tests/__init__.py

File renamed without changes.

fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/conftest.py renamed to fastapi_template/template/{{cookiecutter.project_name}}/tests/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,18 +341,18 @@ async def setup_db() -> AsyncGenerator[None, None]:
341341
async def setup_db() -> AsyncGenerator[None, None]:
342342
"""
343343
Fixture to create database connection.
344-
344+
345345
:yield: nothing.
346346
"""
347347
client = AsyncIOMotorClient(settings.db_url.human_repr()) # type: ignore
348348
from {{cookiecutter.project_name}}.db.models import load_all_models # noqa: WPS433
349349
await beanie.init_beanie(
350350
database=client[settings.db_base],
351-
document_models=load_all_models(),
351+
document_models=load_all_models(), # type: ignore
352352
)
353353
yield
354354

355-
355+
356356
{%- endif %}
357357

358358
{%- if cookiecutter.enable_rmq == 'True' %}
@@ -509,7 +509,7 @@ def fastapi_app(
509509
{%- if cookiecutter.enable_kafka == "True" %}
510510
application.dependency_overrides[get_kafka_producer] = lambda: test_kafka_producer
511511
{%- endif %}
512-
return application # noqa: WPS331
512+
return application # noqa: RET504
513513

514514

515515
@pytest.fixture

fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/tests/test_dummy.py renamed to fastapi_template/template/{{cookiecutter.project_name}}/tests/test_dummy.py

File renamed without changes.

0 commit comments

Comments
 (0)