Skip to content

Commit 6c84595

Browse files
chore(checks): Init wemake-python-styleguide configuration (#905)
### Description of your changes * Integrate wemake-python-styleguide and ruff * Add ignores for deprecated hook * Make initial setup of wemake-python-styleguide, by disabling a few too strict rules --------- Co-authored-by: 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) <[email protected]>
1 parent 2f8bda1 commit 6c84595

File tree

9 files changed

+143
-19
lines changed

9 files changed

+143
-19
lines changed

.flake8

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
[flake8]
2+
3+
# Print the total number of errors:
4+
count = true
5+
6+
# Don't even try to analyze these:
7+
extend-exclude =
8+
# GitHub configs
9+
.github,
10+
# Cache files of MyPy
11+
.mypy_cache,
12+
# Cache files of pytest
13+
.pytest_cache,
14+
# Countless third-party libs in venvs
15+
.tox,
16+
# Occasional virtualenv dir
17+
.venv,
18+
# VS Code
19+
.vscode,
20+
# Metadata of `pip wheel` cmd is autogenerated
21+
pip-wheel-metadata,
22+
23+
# IMPORTANT: avoid using ignore option, always use extend-ignore instead
24+
# Completely and unconditionally ignore the following errors:
25+
extend-ignore =
26+
# Legitimate cases, no need to "fix" these violations:
27+
# D202: No blank lines allowed after function docstring, conflicts with `ruff format`
28+
D202,
29+
# E203: whitespace before ':', conflicts with `ruff format`
30+
E203,
31+
# E501: "line too long", its function is replaced by `flake8-length`
32+
E501,
33+
# W505: "doc line too long", its function is replaced by `flake8-length`
34+
W505,
35+
# I: flake8-isort is drunk + we have isort integrated into pre-commit
36+
I,
37+
# WPS305: "Found f string" -- nothing bad about this
38+
WPS305,
39+
# WPS322: "Found incorrect multi-line string" -- false-positives with
40+
# attribute docstrings. Ref:
41+
# https://github.com/wemake-services/wemake-python-styleguide/issues/3056
42+
WPS322,
43+
# WPS326: "Found implicit string concatenation" -- nothing bad about this
44+
WPS326,
45+
# WPS428: "Found statement that has no effect" -- false-positives with
46+
# attribute docstrings. Ref:
47+
# https://github.com/wemake-services/wemake-python-styleguide/issues/3056
48+
WPS428,
49+
# WPS462: "Wrong multiline string usage" -- false-positives with
50+
# attribute docstrings. Ref:
51+
# https://github.com/wemake-services/wemake-python-styleguide/issues/3056
52+
WPS462,
53+
# WPS300: "Forbid imports relative to the current folder" -- we use relative imports
54+
WPS300,
55+
56+
# https://wemake-python-styleguide.readthedocs.io/en/latest/pages/usage/formatter.html
57+
format = wemake
58+
59+
# Let's not overcomplicate the code:
60+
max-complexity = 10
61+
62+
# Accessibility/large fonts and PEP8 friendly.
63+
# This is being flexibly extended through the `flake8-length`:
64+
max-line-length = 79
65+
66+
# Allow certain violations in certain files:
67+
# Please keep both sections of this list sorted, as it will be easier for others to find and add entries in the future
68+
per-file-ignores =
69+
# The following ignores have been researched and should be considered permanent
70+
# each should be preceded with an explanation of each of the error codes
71+
# If other ignores are added for a specific file in the section following this,
72+
# these will need to be added to that line as well.
73+
74+
tests/pytest/_cli_test.py:
75+
# WPS431: "Forbid nested classes" -- this is a legitimate use case for tests
76+
WPS431,
77+
# WPS226: "Forbid the overuse of string literals" -- this is a legitimate use case for tests
78+
WPS226,
79+
# WPS115: "Require snake_case for naming class attributes" -- testing legitimate case, ignored in main code
80+
WPS115,
81+
# We will not spend time on fixing complexity in deprecated hook
82+
src/pre_commit_terraform/terraform_docs_replace.py: WPS232
83+
84+
# Count the number of occurrences of each error/warning code and print a report:
85+
statistics = true
86+
87+
# ## Plugin-provided settings: ##
88+
89+
# flake8-eradicate
90+
# E800:
91+
eradicate-whitelist-extend = isort:\s+\w+
92+
93+
# flake8-pytest-style
94+
# PT001:
95+
pytest-fixture-no-parentheses = true
96+
# PT006:
97+
pytest-parametrize-names-type = tuple
98+
# PT007:
99+
pytest-parametrize-values-type = tuple
100+
pytest-parametrize-values-row-type = tuple
101+
# PT023:
102+
pytest-mark-no-parentheses = true
103+
104+
# wemake-python-styleguide
105+
# WPS410: "Forbid some module-level variables" -- __all__ is a legitimate use case
106+
allowed-module-metadata = __all__
107+
108+
show-source = true

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ repos:
112112
- --fix
113113
- id: ruff-format
114114

115+
- repo: https://github.com/wemake-services/wemake-python-styleguide
116+
rev: 6d4ca2bdc16b3098422a2770728136fc0751b817 # frozen: 1.1.0
117+
hooks:
118+
- id: wemake-python-styleguide
119+
115120
- repo: https://github.com/pre-commit/mirrors-mypy.git
116121
rev: 7010b10a09f65cd60a23c207349b539aa36dbec1 # frozen: v1.16.0
117122
hooks:

ruff.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ convention = "pep257"
1717
[lint]
1818
select = ["ALL"]
1919
preview = true
20+
external = ["WPS"] # Do not remove noqa for wemake-python-style (WPS) checks
2021
ignore = [
2122
"CPY001", # Skip copyright notice requirement at top of files
2223
]

src/pre_commit_terraform/_cli.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,28 @@ def invoke_cli_app(cli_args: list[str]) -> ReturnCodeType:
3333
parsed_cli_args.invoke_cli_app,
3434
)
3535

36-
try:
36+
try: # noqa: WPS225 - too many `except` cases
3737
return invoke_cli_app(parsed_cli_args)
3838
except PreCommitTerraformExit as exit_err:
39-
# T201 - FIXME here and below - we will replace 'print' with
40-
# logging later
41-
print(f'App exiting: {exit_err!s}', file=sys.stderr) # noqa: T201
39+
# T201,WPS421 - FIXME here and below - we will replace 'print'
40+
# with logging later
41+
print(f'App exiting: {exit_err!s}', file=sys.stderr) # noqa: T201,WPS421
4242
raise
4343
except PreCommitTerraformRuntimeError as unhandled_exc:
44-
print( # noqa: T201
44+
print( # noqa: T201,WPS421
4545
f'App execution took an unexpected turn: {unhandled_exc!s}. '
4646
'Exiting...',
4747
file=sys.stderr,
4848
)
4949
return ReturnCode.ERROR
5050
except PreCommitTerraformBaseError as unhandled_exc:
51-
print( # noqa: T201
51+
print( # noqa: T201,WPS421
5252
f'A surprising exception happened: {unhandled_exc!s}. Exiting...',
5353
file=sys.stderr,
5454
)
5555
return ReturnCode.ERROR
5656
except KeyboardInterrupt as ctrl_c_exc:
57-
print( # noqa: T201
57+
print( # noqa: T201,WPS421
5858
f'User-initiated interrupt: {ctrl_c_exc!s}. Exiting...',
5959
file=sys.stderr,
6060
)

src/pre_commit_terraform/_cli_subcommands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
from ._types import CLISubcommandModuleProtocol
55

66

7-
SUBCOMMAND_MODULES: list[CLISubcommandModuleProtocol] = [
7+
SUBCOMMAND_MODULES: tuple[CLISubcommandModuleProtocol] = (
88
terraform_docs_replace,
9-
]
9+
)
1010

1111

1212
__all__ = ('SUBCOMMAND_MODULES',)

src/pre_commit_terraform/_structs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ class ReturnCode(IntEnum):
99
To be used in check callable implementations.
1010
"""
1111

12-
OK = 0
13-
ERROR = 1
12+
# WPS115: "Require snake_case for naming class attributes". According to
13+
# "Correct" example in docs - it's valid use case => false-positive
14+
OK = 0 # noqa: WPS115
15+
ERROR = 1 # noqa: WPS115
1416

1517

1618
__all__ = ('ReturnCode',)

src/pre_commit_terraform/_types.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
class CLISubcommandModuleProtocol(Protocol):
1515
"""A protocol for the subcommand-implementing module shape."""
1616

17-
CLI_SUBCOMMAND_NAME: str
17+
# WPS115: "Require snake_case for naming class attributes".
18+
# This protocol describes module shapes and not regular classes.
19+
# It's a valid use case as then it's used as constants:
20+
# "CLI_SUBCOMMAND_NAME: Final[str] = 'hook-name'"" on top level
21+
CLI_SUBCOMMAND_NAME: str # noqa: WPS115
1822
"""This constant contains a CLI."""
1923

2024
def populate_argument_parser(

src/pre_commit_terraform/terraform_docs_replace.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def populate_argument_parser(subcommand_parser: ArgumentParser) -> None:
5858
)
5959

6060

61-
def invoke_cli_app(parsed_cli_args: Namespace) -> ReturnCodeType:
61+
# WPS231 - Found function with too much cognitive complexity
62+
# We will not spend time on fixing complexity in deprecated hook
63+
def invoke_cli_app(parsed_cli_args: Namespace) -> ReturnCodeType: # noqa: WPS231
6264
"""Run the entry-point of the CLI app.
6365
6466
Returns:
@@ -86,7 +88,7 @@ def invoke_cli_app(parsed_cli_args: Namespace) -> ReturnCodeType:
8688
retval = ReturnCode.OK
8789

8890
for directory in dirs:
89-
try:
91+
try: # noqa: WPS229 - ignore as it's deprecated hook
9092
proc_args = []
9193
proc_args.append('terraform-docs')
9294
if cast_to('bool', parsed_cli_args.sort):
@@ -107,8 +109,10 @@ def invoke_cli_app(parsed_cli_args: Namespace) -> ReturnCodeType:
107109
subprocess.check_call(' '.join(proc_args), shell=True) # noqa: S602
108110
# PERF203 - try-except shouldn't be in a loop, but it's deprecated
109111
# hook, so leave as is
110-
except subprocess.CalledProcessError as e: # noqa: PERF203
111-
# T201 - Leave print statement as is, as this is deprecated hook
112-
print(e) # noqa: T201
112+
# WPS111 - Too short var name, but it's deprecated hook, so leave as is
113+
except subprocess.CalledProcessError as e: # noqa: PERF203,WPS111
114+
# T201,WPS421 - Leave print statement as is, as this is
115+
# deprecated hook
116+
print(e) # noqa: T201,WPS421,WPS111
113117
retval = ReturnCode.ERROR
114118
return retval

tests/pytest/_cli_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def populate_argument_parser(
5555
self,
5656
subcommand_parser: ArgumentParser,
5757
) -> None:
58-
return None
58+
pass # noqa: WPS420. This is a stub, docstring not needed so "pass" required.
5959

6060
def invoke_cli_app(self, parsed_cli_args: Namespace) -> ReturnCodeType:
6161
raise raised_error
@@ -85,7 +85,7 @@ def populate_argument_parser(
8585
self,
8686
subcommand_parser: ArgumentParser,
8787
) -> None:
88-
return None
88+
pass # noqa: WPS420. This is a stub, docstring not needed so "pass" required.
8989

9090
def invoke_cli_app(self, parsed_cli_args: Namespace) -> ReturnCodeType:
9191
raise PreCommitTerraformExit(self.CLI_SUBCOMMAND_NAME)

0 commit comments

Comments
 (0)