0
# Extensions System
1
2
Optional extensions providing 25+ additional specialized checkers for advanced code analysis. Extensions enhance pylint's capabilities with domain-specific checks, advanced pattern detection, and integration with external tools and standards.
3
4
## Capabilities
5
6
### Extension Architecture
7
8
System for loading and managing optional checker extensions.
9
10
```python { .api }
11
def initialize(linter):
12
"""
13
Standard extension initialization function.
14
15
Each extension module must provide this function to register
16
its checkers with the linter.
17
18
Args:
19
linter: PyLinter instance to register checkers with
20
"""
21
22
# Extension loading patterns
23
def register(linter):
24
"""Alternative registration function name."""
25
pass
26
27
def load_configuration(linter):
28
"""Load extension-specific configuration."""
29
pass
30
```
31
32
### Code Style Extensions
33
34
Advanced code style and formatting checkers beyond the standard format checker.
35
36
```python { .api }
37
# Code style enhancements
38
import pylint.extensions.code_style
39
# - Additional naming convention checks
40
# - Enhanced formatting validation
41
# - Style consistency enforcement
42
43
# Empty comment detection
44
import pylint.extensions.empty_comment
45
# - Detect and flag empty comment lines
46
# - Configurable comment content validation
47
48
# Bad builtin usage
49
import pylint.extensions.bad_builtin
50
# - Flag usage of problematic builtin functions
51
# - Suggest better alternatives
52
# - Configurable builtin blacklist
53
```
54
55
### Complexity Analysis
56
57
Advanced complexity metrics and analysis tools.
58
59
```python { .api }
60
# McCabe complexity analysis
61
import pylint.extensions.mccabe
62
# - Cyclomatic complexity calculation
63
# - Configurable complexity thresholds
64
# - Function and method complexity limits
65
66
# Broad try clause detection
67
import pylint.extensions.broad_try_clause
68
# - Detect overly broad try blocks
69
# - Encourage specific exception handling
70
# - Configurable try block size limits
71
```
72
73
### Documentation and Docstring Validation
74
75
Comprehensive docstring and documentation checking.
76
77
```python { .api }
78
# Docstring parameter validation
79
import pylint.extensions.docparams
80
# - Validate docstring parameters match function signature
81
# - Check parameter descriptions
82
# - Validate return value documentation
83
# - Support for multiple docstring formats (Google, NumPy, Sphinx)
84
85
# Docstring style checking
86
import pylint.extensions.docstyle
87
# - Docstring format validation
88
# - Style consistency checking
89
# - Missing docstring detection enhancement
90
```
91
92
### Type System Enhancements
93
94
Advanced type checking and analysis capabilities.
95
96
```python { .api }
97
# Enhanced type checking
98
import pylint.extensions.typing
99
# - Additional type annotation validation
100
# - Generic type usage checking
101
# - Type compatibility analysis
102
# - Protocol and TypedDict validation
103
104
# Equality without hash detection
105
import pylint.extensions.eq_without_hash
106
# - Detect classes with __eq__ but no __hash__
107
# - Flag potential dictionary/set usage issues
108
# - Suggest proper hash implementation
109
```
110
111
### Performance and Optimization
112
113
Checkers focused on performance patterns and optimization opportunities.
114
115
```python { .api }
116
# Set membership optimization
117
import pylint.extensions.set_membership
118
# - Detect inefficient membership testing
119
# - Suggest set/frozenset for O(1) lookups
120
# - Flag repeated list/tuple membership checks
121
122
# For/any/all pattern detection
123
import pylint.extensions.for_any_all
124
# - Suggest using any() or all() instead of loops
125
# - Detect common iteration patterns
126
# - Performance optimization suggestions
127
```
128
129
### Control Flow Analysis
130
131
Advanced control flow and conditional logic analysis.
132
133
```python { .api }
134
# Elif condition analysis
135
import pylint.extensions.check_elif
136
# - Validate elif condition logic
137
# - Detect unreachable elif branches
138
# - Suggest condition simplification
139
140
# Confusing elif detection
141
import pylint.extensions.confusing_elif
142
# - Flag potentially confusing elif structures
143
# - Suggest clearer conditional logic
144
# - Detect overlapping conditions
145
146
# Ternary expression suggestions
147
import pylint.extensions.consider_ternary_expression
148
# - Suggest ternary operators for simple if/else
149
# - Improve code conciseness
150
# - Configurable complexity thresholds
151
```
152
153
### Import and Dependency Analysis
154
155
Advanced import analysis and dependency management.
156
157
```python { .api }
158
# Private import detection
159
import pylint.extensions.private_import
160
# - Detect imports from private modules
161
# - Flag underscore-prefixed imports
162
# - Validate API boundary respect
163
164
# Overlapping exception detection
165
import pylint.extensions.overlapping_exceptions
166
# - Detect overlapping exception handlers
167
# - Flag unreachable except clauses
168
# - Suggest exception hierarchy improvements
169
```
170
171
### Variable and Assignment Analysis
172
173
Enhanced variable usage and assignment pattern analysis.
174
175
```python { .api }
176
# Redefined variable type detection
177
import pylint.extensions.redefined_variable_type
178
# - Detect variables assigned different types
179
# - Flag potential type confusion
180
# - Suggest consistent typing patterns
181
182
# Unused self parameter detection
183
import pylint.extensions.no_self_use
184
# - Detect methods that don't use self
185
# - Suggest staticmethod or function conversion
186
# - Configurable whitelist patterns
187
```
188
189
### Magic Values and Constants
190
191
Detection and management of magic numbers and strings.
192
193
```python { .api }
194
# Magic value detection
195
import pylint.extensions.magic_value
196
# - Detect magic numbers and strings
197
# - Suggest named constants
198
# - Configurable value whitelists
199
# - Context-aware detection
200
```
201
202
### Comparison and Logic Patterns
203
204
Advanced comparison and logical expression analysis.
205
206
```python { .api }
207
# Zero comparison optimization
208
import pylint.extensions.comparetozero
209
# - Detect inefficient zero comparisons
210
# - Suggest truthiness testing
211
# - Flag explicit None comparisons
212
213
# No else return pattern
214
import pylint.extensions.no_else_return
215
# - Detect unnecessary else after return
216
# - Suggest early return patterns
217
# - Improve code flow readability
218
```
219
220
## Usage Examples
221
222
### Loading Extensions
223
224
```python
225
from pylint.lint import PyLinter
226
227
# Load extension programmatically
228
linter = PyLinter()
229
from pylint.extensions import mccabe
230
mccabe.initialize(linter)
231
232
# Load multiple extensions
233
extensions = [
234
'pylint.extensions.mccabe',
235
'pylint.extensions.docparams',
236
'pylint.extensions.code_style'
237
]
238
239
for ext_name in extensions:
240
linter.load_plugin(ext_name)
241
```
242
243
### Command Line Extension Loading
244
245
```bash
246
# Load single extension
247
pylint --load-plugins=pylint.extensions.mccabe mymodule.py
248
249
# Load multiple extensions
250
pylint --load-plugins=mccabe,docparams,code_style mymodule.py
251
252
# Load all available extensions
253
pylint --load-plugins=pylint.extensions mymodule.py
254
```
255
256
### Extension Configuration
257
258
```python
259
# McCabe complexity configuration
260
linter.config.max_complexity = 10
261
262
# Docparams configuration
263
linter.config.accept_no_param_doc = False
264
linter.config.accept_no_raise_doc = False
265
linter.config.accept_no_return_doc = False
266
linter.config.accept_no_yields_doc = False
267
268
# Magic value configuration
269
linter.config.valid_magic_values = [-1, 0, 1, 2]
270
linter.config.valid_magic_strings = ['', 'utf-8', 'ascii']
271
```
272
273
### Custom Extension Development
274
275
```python
276
# Creating a custom extension
277
from pylint.checkers import BaseChecker
278
from pylint.interfaces import IAstroidChecker
279
280
class CustomExtensionChecker(BaseChecker):
281
"""Custom extension checker example."""
282
283
__implements__ = IAstroidChecker
284
285
name = 'custom-extension'
286
msgs = {
287
'C9900': (
288
'Custom extension message: %s',
289
'custom-extension-message',
290
'Description of custom extension check'
291
)
292
}
293
294
options = (
295
('custom-option', {
296
'default': True,
297
'type': 'yn',
298
'help': 'Enable custom extension behavior'
299
}),
300
)
301
302
def visit_functiondef(self, node):
303
"""Custom analysis logic."""
304
if self.config.custom_option:
305
# Perform custom analysis
306
self.add_message('custom-extension-message',
307
node=node, args=(node.name,))
308
309
def initialize(linter):
310
"""Register the custom extension."""
311
linter.register_checker(CustomExtensionChecker(linter))
312
```
313
314
## Available Extensions Reference
315
316
### Documentation Extensions
317
318
```python { .api }
319
# pylint.extensions.docparams
320
# - missing-param-doc: Missing parameter documentation
321
# - missing-type-doc: Missing parameter type documentation
322
# - missing-return-doc: Missing return documentation
323
# - missing-return-type-doc: Missing return type documentation
324
# - missing-raises-doc: Missing exception documentation
325
326
# pylint.extensions.docstyle
327
# - docstring-first-line-empty: Empty first docstring line
328
# - docstring-min-length: Docstring too short
329
```
330
331
### Code Quality Extensions
332
333
```python { .api }
334
# pylint.extensions.mccabe
335
# - too-complex: Function/method too complex
336
337
# pylint.extensions.code_style
338
# - consider-using-assignment-expr: Consider using assignment expression
339
# - consider-using-namedtuple-or-dataclass: Consider namedtuple/dataclass
340
341
# pylint.extensions.magic_value
342
# - magic-value-comparison: Magic value used in comparison
343
```
344
345
### Performance Extensions
346
347
```python { .api }
348
# pylint.extensions.set_membership
349
# - use-set-for-membership: Use set for membership testing
350
351
# pylint.extensions.for_any_all
352
# - consider-using-any-or-all: Consider using any() or all()
353
```
354
355
### Logic and Control Flow
356
357
```python { .api }
358
# pylint.extensions.consider_ternary_expression
359
# - consider-ternary-expression: Consider using ternary expression
360
361
# pylint.extensions.confusing_elif
362
# - confusing-elif: Confusing elif condition
363
```
364
365
## Extension Configuration Examples
366
367
### McCabe Configuration
368
369
```ini
370
# .pylintrc configuration
371
[DESIGN]
372
max-complexity = 10
373
374
[MESSAGES CONTROL]
375
enable = too-complex
376
```
377
378
### Docparams Configuration
379
380
```ini
381
[PARAMETER_DOCUMENTATION]
382
accept-no-param-doc = no
383
accept-no-raise-doc = no
384
accept-no-return-doc = no
385
accept-no-yields-doc = no
386
default-docstring-type = sphinx
387
```
388
389
### Magic Value Configuration
390
391
```ini
392
[MAGIC_VALUE]
393
valid-magic-values = -1,0,1,2,100,1000
394
valid-magic-strings = '',utf-8,ascii,latin-1
395
```