Skip to content

Commit

Permalink
Merge pull request #86 from saritasa-nest/feature/migrate-to-ruff
Browse files Browse the repository at this point in the history
Migrate to ruff
  • Loading branch information
TheSuperiorStanislav authored May 23, 2024
2 parents 34b5e86 + fcad949 commit adfebfc
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 161 deletions.
49 changes: 5 additions & 44 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,12 @@ repos:
- id: poetry-check
args: ["--lock"]

- repo: https://github.com/pycqa/isort
rev: 5.13.2
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.7
hooks:
- id: isort
name: isort (python)

- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
exclude: .venv|__init__.py
additional_dependencies: [
# A plugin for Flake8 finding likely bugs and design problems in your program.
# https://github.com/PyCQA/flake8-bugbear
flake8-bugbear,
# A flake8 plugin checking common style issues or inconsistencies with pytest-based tests.
# https://github.com/m-burst/flake8-pytest-style
flake8-pytest-style,
# A flake8 plugin that warn about backslashes usage.
# https://github.com/wemake-services/flake8-broken-line
flake8-broken-line,
# A simple module that adds an extension for the fantastic pydocstyle tool to flake8.
# https://github.com/PyCQA/flake8-docstrings
flake8-docstrings,
# A flake8 plugin loading the configuration from pyproject.toml
# https://github.com/john-hen/Flake8-pyproject
flake8-pyproject,
# flake8 plugin to validate type annotations according to modern practices.
# https://github.com/plinss/flake8-modern-annotations
flake8-modern-annotations,
# McCabe complexity checker.
# https://github.com/PyCQA/mccabe
mccabe,
]

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

- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black
language_version: python3
- id: ruff
args: [ --fix ]
- id: ruff-format

- repo: https://github.com/asottile/add-trailing-comma
rev: v3.1.0
Expand Down
45 changes: 22 additions & 23 deletions .vscode/recommended_settings.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
{
"files.exclude": {
"**/__pycache__": true,
"**/.mypy_cache": true
},
"files.exclude": {
"**/__pycache__": true,
"**/.pytest_cache": true,
"**/.mypy_cache": true,
"**/.ruff_cache": true,
"**/localstack_volume": true,
"**/htmlcov": true,
},

"editor.rulers": [79],
"editor.rulers": [79],

"editor.bracketPairColorization.enabled": true,
"editor.bracketPairColorization.enabled": true,

"python.analysis.typeCheckingMode": "basic",
"python.analysis.diagnosticSeverityOverrides": {
"reportPrivateImportUsage": "none"
},
"python.analysis.typeCheckingMode": "basic",
"python.analysis.diagnosticSeverityOverrides": {
"reportPrivateImportUsage": "none"
},

"python.analysis.inlayHints.functionReturnTypes": true,
"python.analysis.inlayHints.functionReturnTypes": true,
"mypy.enabled": false,

"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"mypy.enabled": true,
"mypy.dmypyExecutable": "${workspaceFolder}/.venv/bin/dmypy",
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,

"[python]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"editor.defaultFormatter": "ms-python.black-formatter"
},
"isort.args": ["--settings-path", "${workspaceFolder}/pyproject.toml"],
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff"
}
}
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/saritasa-invocations)
![PyPI - License](https://img.shields.io/pypi/l/saritasa-invocations)
![PyPI - Downloads](https://img.shields.io/pypi/dm/saritasa-invocations)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

Collection of [invoke](https://www.pyinvoke.org/) commands used by Saritasa

Expand Down
159 changes: 104 additions & 55 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,66 +89,115 @@ mypy = "^1.10.0"
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.isort]
profile = "black"
line_length = 79
multi_line_output = 3
skip = [
"_tmp",
"src",
".venv",
]
sections = [
"FUTURE",
"STDLIB",
"THIRDPARTY",
"FIRSTPARTY",
"LOCALFOLDER",
]
include_trailing_comma = true
default_section = "THIRDPARTY"
[tool.ruff]
line-length = 79
indent-width = 4
target-version = "py310"

[tool.flake8]
[tool.ruff.lint]
extend-select = [
# https://docs.astral.sh/ruff/rules/#pycodestyle-e-w
"W",
"E",
# https://docs.astral.sh/ruff/rules/#mccabe-c90
"C90",
# https://docs.astral.sh/ruff/rules/#isort-i
"I",
# https://docs.astral.sh/ruff/rules/#pep8-naming-n
"N",
# https://docs.astral.sh/ruff/rules/#pydocstyle-d
"D",
# https://docs.astral.sh/ruff/rules/#pyupgrade-up
"UP",
# https://docs.astral.sh/ruff/rules/#flake8-annotations-ann
"ANN",
# https://docs.astral.sh/ruff/rules/#flake8-bandit-s
"S",
# https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
"B",
# https://docs.astral.sh/ruff/rules/#flake8-builtins-a
"A",
# https://docs.astral.sh/ruff/rules/#flake8-commas-com
"COM",
# https://docs.astral.sh/ruff/rules/#flake8-comprehensions-c4
"C4",
# https://docs.astral.sh/ruff/rules/#flake8-datetimez-dtz
"DTZ",
# https://docs.astral.sh/ruff/rules/#flake8-debugger-t10
"T10",
# https://docs.astral.sh/ruff/rules/#flake8-print-t20
"T20",
# https://docs.astral.sh/ruff/rules/#flake8-pytest-style-pt
"PT",
# https://docs.astral.sh/ruff/rules/#flake8-simplify-sim
"SIM",
# https://docs.astral.sh/ruff/rules/#flake8-use-pathlib-pth
"PTH",
# https://docs.astral.sh/ruff/rules/#flake8-todos-td
"TD",
# https://docs.astral.sh/ruff/rules/#eradicate-era
"ERA",
# https://docs.astral.sh/ruff/rules/#ruff-specific-rules-ruf
"RUF"
]
ignore = [
# https://www.flake8rules.com/rules/E126.html
"E126",
# https://www.flake8rules.com/rules/W503.html
"W503",
# https://www.flake8rules.com/rules/W504.html
"W504",
# https://github.com/m-burst/flake8-pytest-style/blob/master/docs/rules/PT004.md
"PT004",
# https://www.pydocstyle.org/en/latest/error_codes.html
# Missing docstring in public module
"D100",
# Missing docstring in public nested class
"D106",
# Missing docstring in __init__
"D107",
# Use r””” if any backslashes in a docstring
"D301",
# https://docs.astral.sh/ruff/rules/ANN101
"ANN101",
# https://docs.astral.sh/ruff/rules/ANN102
"ANN102",
# https://docs.astral.sh/ruff/rules/ANN401
"ANN401",
# https://docs.astral.sh/ruff/rules/ANN003
"ANN003",
# https://docs.astral.sh/ruff/rules/D100
"D100",
# https://docs.astral.sh/ruff/rules/D104
"D104",
# https://docs.astral.sh/ruff/rules/D106
"D106",
# https://docs.astral.sh/ruff/rules/D107
"D107",
]
statistics = true
count = true
max-complexity = 10
pytest-fixture-no-parentheses = true
pytest-parametrize-names-type = "list"
pytest-parametrize-values-type = "list"
pytest-parametrize-values-row-type = "list"
inline-quotes = "double"
docstring-quotes = "double"
docstring-convention = "pep257"
exclude = [
".venv",
"__init__.py",
"docs",
[tool.ruff.lint.per-file-ignores]
"__init__.py" = [
# https://docs.astral.sh/ruff/rules/F401
"F401",
]

[tool.black]
line-length = 79
target-version = [
"py311",
"**/tests/*" = [
# https://docs.astral.sh/ruff/rules/S101
"S101",
# https://docs.astral.sh/ruff/rules/S106
"S106",
# https://docs.astral.sh/ruff/rules/S311
"S311",
]
"**/test_*" = [
# https://docs.astral.sh/ruff/rules/ANN201
"ANN201",
]
[tool.ruff.lint.isort]
force-wrap-aliases = true
split-on-trailing-comma = true
section-order = [
"future",
"standard-library",
"third-party",
"first-party",
"local-folder",
]

[tool.ruff.lint.flake8-pytest-style]
fixture-parentheses = false
parametrize-names-type = "list"
parametrize-values-type = "list"
parametrize-values-row-type = "list"

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"


[tool.mypy]
# https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Expand Down
10 changes: 5 additions & 5 deletions saritasa_invocations/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ class DockerSettings:
class GitHubActionsSettings:
"""Settings for github actions module."""

hosts: collections.abc.Sequence[str] = tuple()
hosts: collections.abc.Sequence[str] = ()


@dataclasses.dataclass
class DjangoSettings:
"""Settings for django module."""

runserver_command: str = "runserver_plus"
runserver_host: str = "0.0.0.0"
runserver_host: str = "0.0.0.0" # noqa: S104
runserver_port: str = "8000"
runserver_params: str = ""
runserver_docker_params: str = "--rm --service-ports"
Expand Down Expand Up @@ -149,7 +149,7 @@ class FastAPISettings:

uvicorn_command: str = "-m uvicorn"
app: str = "config:fastapi_app"
host: str = "0.0.0.0"
host: str = "0.0.0.0" # noqa: S104
port: str = "8000"
params: str = "--reload"
docker_params: str = "--rm --service-ports"
Expand Down Expand Up @@ -220,7 +220,7 @@ class DBSettings:
class K8SSettingsMeta(type):
"""Meta class for K8SSettings."""

def __call__(cls, *args, **kwargs) -> "K8SSettings":
def __call__(cls, *args, **kwargs) -> "K8SSettings": # noqa: ANN002
"""Update mapping of environments."""
instance: K8SSettings = super().__call__(*args, **kwargs)
if instance.name in _K8S_CONFIGS:
Expand All @@ -243,7 +243,7 @@ class K8SDBSettings:
"--output jsonpath='{{.items[0].metadata.name}}'"
)
exec_command: str = (
"kubectl exec -ti --namespace {db_pod_namespace} " "$({db_pod})"
"kubectl exec -ti --namespace {db_pod_namespace} $({db_pod})"
)
dump_command: str = (
"pg_dump "
Expand Down
6 changes: 3 additions & 3 deletions saritasa_invocations/alembic.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def wait_for_database(context: invoke.Context) -> None:
)
wait_for_database._called = True # type: ignore
return
except invoke.UnexpectedExit:
except invoke.UnexpectedExit as error:
printing.print_error(
"Failed to connect to db, "
f"after {config.alembic.connect_attempts} attempts",
)
raise invoke.Exit(code=1)
raise invoke.Exit(code=1) from error


@invoke.task
Expand Down Expand Up @@ -160,7 +160,7 @@ def check_for_adjust_messages(
for filepath in _get_migration_files_paths(
config.alembic.migrations_folder,
):
with open(filepath) as migration_file:
with pathlib.Path(filepath).open() as migration_file:
file_text = migration_file.read()
for adjust_message in config.alembic.adjust_messages:
if adjust_message in file_text:
Expand Down
2 changes: 1 addition & 1 deletion saritasa_invocations/cruft.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def check_for_cruft_files(context: invoke.Context) -> None:
found_files = tuple(
filter(
lambda filepath: not filepath.startswith(".venv"),
map(str, pathlib.Path(".").glob("**/*.rej")),
map(str, pathlib.Path().glob("**/*.rej")),
),
)
if not found_files:
Expand Down
4 changes: 2 additions & 2 deletions saritasa_invocations/db_k8s.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
import pathlib

import invoke

Expand Down Expand Up @@ -55,7 +55,7 @@ def get_dump(
pod_namespace=config.namespace,
get_pod_name_command=_generate_get_pod_name_command(context),
path_to_file_in_pod=f"tmp/{file}",
path_to_where_save_file=f"{os.getcwd()}/{file}",
path_to_where_save_file=f"{pathlib.Path.cwd()}/{file}",
)
k8s.success(context, f"Downloaded dump ({file}) from pod. Clean up")
context.run(f"{_generate_exec_command(context)} -- rm tmp/{file}")
Expand Down
Loading

0 comments on commit adfebfc

Please sign in to comment.