-
-
Notifications
You must be signed in to change notification settings - Fork 383
/
config.py
428 lines (391 loc) · 15.8 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""
Provides configuration options for ``wemake-python-styleguide``.
We do not like our linter to be highly configurable.
Since, people may take the wrong path or make wrong decisions.
We try to make all defaults as reasonable as possible.
However, you can currently adjust some complexity options. Why?
Because we are not quite sure about the ideal values.
All options are configurable via ``flake8`` CLI.
.. code:: ini
flake8 --max-returns=2 --max-arguments=4
Or you can provide options in ``setup.cfg`` or similar supported files.
.. code:: ini
[flake8]
max-returns = 2
max-arguments = 4
We use ``setup.cfg`` as a default way to provide configuration.
You can also show all options that ``flake8`` supports by running:
.. code:: bash
flake8 --help
.. rubric:: General options
- ``min-name-length`` - minimum number of chars to define a valid
variable and module name, defaults to
:str:`wemake_python_styleguide.options.defaults.MIN_NAME_LENGTH`
- ``max-name-length`` - maximum number of chars to define a valid
variable and module name, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_NAME_LENGTH`
- ``i-control-code`` - whether you control ones who use your code,
more rules are enforced when you do control it,
opposite to ``--i-dont-control-code``, defaults to
:str:`wemake_python_styleguide.options.defaults.I_CONTROL_CODE`
- ``i-dont-control-code`` - whether you control ones who use your code,
more rules are enforced when you do control it,
opposite to ``--i-control-code``, defaults to
:str:`wemake_python_styleguide.options.defaults.I_CONTROL_CODE`
- ``nested-classes-whitelist`` - list of nested classes' names we allow to use,
defaults to
:str:`wemake_python_styleguide.options.defaults.NESTED_CLASSES_WHITELIST`
- ``max-noqa-comments`` - maximum number of `noqa` allowed in a module,
defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_NOQA_COMMENTS`
- ``allowed-domain-names`` - list of allowed domain names, defaults to
:str:`wemake_python_styleguide.options.defaults.ALLOWED_DOMAIN_NAMES`
- ``forbidden-domain-names`` - list of forbidden domain names, defaults to
:str:`wemake_python_styleguide.options.defaults.FORBIDDEN_DOMAIN_NAMES`
- ``forbidden-inline-ignore`` - list of codes of violations or
class of violations that are forbidden to ignore inline, defaults to
:str:`wemake_python_styleguide.options.defaults.FORBIDDEN_INLINE_IGNORE`
- ``exps-for-one-empty-line`` - number of expressions in
function or method body for available empty line (without code)
:str:`wemake_python_styleguide.options.defaults.EXPS_FOR_ONE_EMPTY_LINE`
.. rubric:: Complexity options
- ``max-returns`` - maximum allowed number of ``return``
statements in one function, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_RETURNS`
- ``max-local-variables`` - maximum allowed number of local
variables in one function, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_LOCAL_VARIABLES`
- ``max-expressions`` - maximum allowed number of expressions
in one function, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_EXPRESSIONS`
- ``max-arguments`` - maximum allowed number of arguments in one function,
defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_ARGUMENTS`
- ``max-module-members`` - maximum number of classes and functions
in a single module, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_MODULE_MEMBERS`
- ``max-methods`` - maximum number of methods in a single class,
defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_METHODS`
- ``max-line-complexity`` - maximum line complexity measured in number of
``ast`` nodes per line, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_LINE_COMPLEXITY`
- ``max-jones-score`` - maximum Jones score for a module, which is equal
to the median of all lines complexity sum, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_JONES_SCORE`
- ``max-imports`` - maximum number of imports in a single module,
defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_IMPORTS`
- ``max-imported-names`` - maximum number of imported names
in a single module, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_IMPORTED_NAMES`
- ``max-base-classes`` - maximum number of parent classes inside a class
definition, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_BASE_CLASSES`
- ``max-decorators`` - maximum number of decorators for single function
or class definition, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_DECORATORS`
- ``max-string-usages`` - maximum number of repeated String constants
in your modules, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_STRING_USAGES`
- ``max-awaits`` - maximum allowed number of ``await``
expressions in one function, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_AWAITS`
- ``max-try-body-length`` - maximum amount of ``try`` node body length,
defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_TRY_BODY_LENGTH`
- ``max-module-expressions`` - maximum number of expression
usages in a module, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_MODULE_EXPRESSIONS`
- ``max-function-expressions`` - maximum number of expression
usages in a function or method, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_FUNCTION_EXPRESSIONS`
- ``max-asserts`` - maximum number of ``assert`` statements in a function,
default to
:str:`wemake_python_styleguide.options.defaults.MAX_ASSERTS`
- ``max-access-level`` - maximum number of access level in an expression,
defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_ACCESS_LEVEL`
- ``max-attributes`` - maximum number of public instance attributes,
defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_ATTRIBUTES`
- ``max-raises`` - maximum number of raises in a function,
defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_RAISES`
- ``max-cognitive-score`` - maximum amount of cognitive complexity
per function, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_COGNITIVE_SCORE`
- ``max-cognitive-average`` - maximum amount of cognitive complexity
per module, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_COGNITIVE_AVERAGE`
:str:`wemake_python_styleguide.options.defaults.NESTED_CLASSES_WHITELIST`
- ``max-call-level`` - maximum number of call chains, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_CALL_LEVEL`
- ``max-annotation-complexity`` - maximum number of nested annotations,
defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_ANN_COMPLEXITY`
- ``max-import-from-members`` - maximum number of names that can be imported
from module, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_IMPORT_FROM_MEMBERS`
- ``max-tuple-unpack-length`` - maximum number of variables in tuple unpacking,
defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_TUPLE_UNPACK_LENGTH`
.. rubric:: Formatter options
- ``show-violation-links`` - whether to show violation shortlinks in the
formatter output
:str:`wemake_python_styleguide.options.defaults.SHOW_VIOLATION_LINKS`
"""
from typing import ClassVar, Mapping, Optional, Sequence, Union
import attr
from flake8.options.manager import OptionManager
from typing_extensions import Final, TypeAlias, final
from wemake_python_styleguide.options import defaults
_Type: TypeAlias = type
ConfigValuesTypes: TypeAlias = Union[str, int, bool, Sequence[str]]
String: Final = str
@final
@attr.dataclass(frozen=True, slots=True)
class _Option:
"""Represents ``flake8`` option object."""
long_option_name: str
default: ConfigValuesTypes
help: str # noqa: WPS125
type: Optional[_Type] = int # noqa: WPS125
parse_from_config: bool = True
action: str = 'store'
comma_separated_list: bool = False
dest: Optional[str] = None
def __attrs_post_init__(self) -> None:
"""Is called after regular init is done."""
object.__setattr__( # noqa: WPS609
self, 'help', ' '.join(
(self.help, 'Defaults to: %(default)s'), # noqa: WPS323
),
)
def asdict_no_none(self) -> Mapping[str, ConfigValuesTypes]:
"""We need this method to return options, but filter out ``None``."""
return {
key: opt
for key, opt in attr.asdict(self).items()
if opt is not None
}
@final
class Configuration:
"""Simple configuration store with all options."""
_options: ClassVar[Sequence[_Option]] = [
# General:
_Option(
'--min-name-length',
defaults.MIN_NAME_LENGTH,
'Minimum required length of variable and module names.',
),
_Option(
'--max-name-length',
defaults.MAX_NAME_LENGTH,
'Maximum possible length of the variable and module names.',
),
_Option(
'--i-control-code',
defaults.I_CONTROL_CODE,
'Whether you control ones who use your code.',
action='store_true',
type=None,
dest='i_control_code',
),
_Option(
'--i-dont-control-code',
defaults.I_CONTROL_CODE,
'Whether you control ones who use your code.',
action='store_false',
type=None,
dest='i_control_code',
parse_from_config=False,
),
_Option(
'--max-noqa-comments',
defaults.MAX_NOQA_COMMENTS,
'Maximum amount of `noqa` comments per module.',
),
_Option(
'--nested-classes-whitelist',
defaults.NESTED_CLASSES_WHITELIST,
'List of nested classes names we allow to use.',
type=String,
comma_separated_list=True,
),
_Option(
'--allowed-domain-names',
defaults.ALLOWED_DOMAIN_NAMES,
"Domain names that are removed from variable names' blacklist.",
type=String,
comma_separated_list=True,
),
_Option(
'--forbidden-domain-names',
defaults.FORBIDDEN_DOMAIN_NAMES,
"Domain names that extends variable names' blacklist.",
type=String,
comma_separated_list=True,
),
_Option(
'--forbidden-inline-ignore',
defaults.FORBIDDEN_INLINE_IGNORE,
'Codes of violations or class of violations forbidden to ignore.',
type=String,
comma_separated_list=True,
),
_Option(
'--exps-for-one-empty-line',
defaults.EXPS_FOR_ONE_EMPTY_LINE,
'Count of expressions for one empty line in a function body.',
),
# Complexity:
_Option(
'--max-returns',
defaults.MAX_RETURNS,
'Maximum allowed number of return statements in one function.',
),
_Option(
'--max-local-variables',
defaults.MAX_LOCAL_VARIABLES,
'Maximum allowed number of local variables in one function.',
),
_Option(
'--max-expressions',
defaults.MAX_EXPRESSIONS,
'Maximum allowed number of expressions in one function.',
),
_Option(
'--max-arguments',
defaults.MAX_ARGUMENTS,
'Maximum allowed number of arguments in one function.',
),
_Option(
'--max-module-members',
defaults.MAX_MODULE_MEMBERS,
'Maximum number of classes and functions in a single module.',
),
_Option(
'--max-methods',
defaults.MAX_METHODS,
'Maximum number of methods in a single class.',
),
_Option(
'--max-line-complexity',
defaults.MAX_LINE_COMPLEXITY,
'Maximum line complexity, measured in `ast` nodes.',
),
_Option(
'--max-jones-score',
defaults.MAX_JONES_SCORE,
'Maximum median module complexity, based on sum of lines.',
),
_Option(
'--max-imports',
defaults.MAX_IMPORTS,
'Maximum number of imports in a single module.',
),
_Option(
'--max-imported-names',
defaults.MAX_IMPORTED_NAMES,
'Maximum number of imported names in a single module.',
),
_Option(
'--max-base-classes',
defaults.MAX_BASE_CLASSES,
'Maximum number of base classes.',
),
_Option(
'--max-decorators',
defaults.MAX_DECORATORS,
'Maximum number of decorators.',
),
_Option(
'--max-string-usages',
defaults.MAX_STRING_USAGES,
'Maximum number of string constant usages.',
),
_Option(
'--max-awaits',
defaults.MAX_AWAITS,
'Maximum allowed number of await expressions in one function.',
),
_Option(
'--max-try-body-length',
defaults.MAX_TRY_BODY_LENGTH,
'Maximum amount of try block node body length.',
),
_Option(
'--max-module-expressions',
defaults.MAX_MODULE_EXPRESSIONS,
'Maximum amount of expression usages in a module.',
),
_Option(
'--max-function-expressions',
defaults.MAX_FUNCTION_EXPRESSIONS,
'Maximum amount of expression usages in a function or method.',
),
_Option(
'--max-asserts',
defaults.MAX_ASSERTS,
'Maximum allowed number of assert statements in one function.',
),
_Option(
'--max-access-level',
defaults.MAX_ACCESS_LEVEL,
'Maximum number of access level in an expression.',
),
_Option(
'--max-attributes',
defaults.MAX_ATTRIBUTES,
'Maximum number of public instance attributes.',
),
_Option(
'--max-raises',
defaults.MAX_RAISES,
'Maximum number of raises in a function.',
),
_Option(
'--max-cognitive-score',
defaults.MAX_COGNITIVE_SCORE,
'Maximum amount of cognitive complexity per function.',
),
_Option(
'--max-cognitive-average',
defaults.MAX_COGNITIVE_AVERAGE,
'Maximum amount of average cognitive complexity per module.',
),
_Option(
'--max-call-level',
defaults.MAX_CALL_LEVEL,
'Maximum number of call chains.',
),
_Option(
'--max-annotation-complexity',
defaults.MAX_ANN_COMPLEXITY,
'Maximum number of nested annotations.',
),
_Option(
'--max-import-from-members',
defaults.MAX_IMPORT_FROM_MEMBERS,
'Maximum number of names that can be imported from module.',
),
_Option(
'--max-tuple-unpack-length',
defaults.MAX_TUPLE_UNPACK_LENGTH,
'Maximum number of variables in a tuple unpacking.',
),
# Formatter:
_Option(
'--show-violation-links',
defaults.SHOW_VIOLATION_LINKS,
'Whether to show violation shortlinks in the formatter output.',
action='store_true',
type=None,
dest='show_violation_links',
),
]
def register_options(self, parser: OptionManager) -> None:
"""Registers options for our plugin."""
for option in self._options:
parser.add_option(**option.asdict_no_none())