codelaw-rules-python 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codelaw_rules_python/LICENSE +23 -0
- codelaw_rules_python/NOTICE +14 -0
- codelaw_rules_python/README.md +189 -0
- codelaw_rules_python/codelaw-pack.toml +6 -0
- codelaw_rules_python/corpus.toml +103 -0
- codelaw_rules_python/data/python/stdlib.json +331 -0
- codelaw_rules_python/presets/py/conventions.toml +5 -0
- codelaw_rules_python/presets/py/project.toml +4 -0
- codelaw_rules_python/presets/py/recommended.toml +99 -0
- codelaw_rules_python/rules/graph/no-unused-exports.md +69 -0
- codelaw_rules_python/rules/py/abstract-base-class-without-abstract-method.md +50 -0
- codelaw_rules_python/rules/py/all-sorted.cases.md +74 -0
- codelaw_rules_python/rules/py/all-sorted.md +33 -0
- codelaw_rules_python/rules/py/ambiguous-class-name.md +41 -0
- codelaw_rules_python/rules/py/ambiguous-function-name.md +41 -0
- codelaw_rules_python/rules/py/ambiguous-variable-name.cases.md +47 -0
- codelaw_rules_python/rules/py/ambiguous-variable-name.md +39 -0
- codelaw_rules_python/rules/py/assert-false.md +34 -0
- codelaw_rules_python/rules/py/assert-raises-exception.md +51 -0
- codelaw_rules_python/rules/py/assert-tuple.md +38 -0
- codelaw_rules_python/rules/py/assignment-to-os-environ.md +43 -0
- codelaw_rules_python/rules/py/batched-without-explicit-strict.md +52 -0
- codelaw_rules_python/rules/py/break-outside-loop.md +41 -0
- codelaw_rules_python/rules/py/cached-instance-method.md +55 -0
- codelaw_rules_python/rules/py/continue-outside-loop.md +41 -0
- codelaw_rules_python/rules/py/default-except-not-last.md +48 -0
- codelaw_rules_python/rules/py/duplicate-dict-key-variable.md +39 -0
- codelaw_rules_python/rules/py/duplicate-dict-key.md +39 -0
- codelaw_rules_python/rules/py/duplicate-handler-exception.md +43 -0
- codelaw_rules_python/rules/py/duplicate-set-value.md +33 -0
- codelaw_rules_python/rules/py/duplicate-try-block-exception.md +47 -0
- codelaw_rules_python/rules/py/empty-method-without-abstract-decorator.md +55 -0
- codelaw_rules_python/rules/py/except-with-empty-tuple.md +44 -0
- codelaw_rules_python/rules/py/except-with-non-exception-classes.md +44 -0
- codelaw_rules_python/rules/py/f-string-docstring.md +40 -0
- codelaw_rules_python/rules/py/f-string-missing-placeholders.cases.md +49 -0
- codelaw_rules_python/rules/py/f-string-missing-placeholders.md +33 -0
- codelaw_rules_python/rules/py/format-invalid.md +38 -0
- codelaw_rules_python/rules/py/format-missing-argument.md +38 -0
- codelaw_rules_python/rules/py/format-mixed-numbering.md +38 -0
- codelaw_rules_python/rules/py/format-unused-named-argument.md +37 -0
- codelaw_rules_python/rules/py/format-unused-positional-argument.md +37 -0
- codelaw_rules_python/rules/py/forward-annotation-syntax-error.md +44 -0
- codelaw_rules_python/rules/py/function-call-in-default-argument.md +51 -0
- codelaw_rules_python/rules/py/function-uses-loop-variable.cases.md +57 -0
- codelaw_rules_python/rules/py/function-uses-loop-variable.md +42 -0
- codelaw_rules_python/rules/py/getattr-with-constant.cases.md +31 -0
- codelaw_rules_python/rules/py/getattr-with-constant.md +33 -0
- codelaw_rules_python/rules/py/if-tuple.md +40 -0
- codelaw_rules_python/rules/py/import-shadowed-by-loop-var.md +45 -0
- codelaw_rules_python/rules/py/invalid-print-syntax.md +42 -0
- codelaw_rules_python/rules/py/is-literal.md +36 -0
- codelaw_rules_python/rules/py/jump-statement-in-finally.cases.md +15 -0
- codelaw_rules_python/rules/py/jump-statement-in-finally.md +47 -0
- codelaw_rules_python/rules/py/lambda-assignment.cases.md +48 -0
- codelaw_rules_python/rules/py/lambda-assignment.md +35 -0
- codelaw_rules_python/rules/py/late-future-import.md +40 -0
- codelaw_rules_python/rules/py/loop-variable-overrides-iterator.md +41 -0
- codelaw_rules_python/rules/py/map-without-explicit-strict.md +46 -0
- codelaw_rules_python/rules/py/module-import-not-at-top.cases.md +64 -0
- codelaw_rules_python/rules/py/module-import-not-at-top.md +46 -0
- codelaw_rules_python/rules/py/multiple-imports-on-one-line.md +38 -0
- codelaw_rules_python/rules/py/multiple-starred-expressions.md +38 -0
- codelaw_rules_python/rules/py/mutable-contextvar-default.md +42 -0
- codelaw_rules_python/rules/py/nested-star-import.md +42 -0
- codelaw_rules_python/rules/py/no-bare-except.cases.md +37 -0
- codelaw_rules_python/rules/py/no-bare-except.md +44 -0
- codelaw_rules_python/rules/py/no-explicit-stacklevel.md +38 -0
- codelaw_rules_python/rules/py/no-mutable-default.cases.md +58 -0
- codelaw_rules_python/rules/py/no-mutable-default.md +44 -0
- codelaw_rules_python/rules/py/no-print.cases.md +40 -0
- codelaw_rules_python/rules/py/no-print.md +42 -0
- codelaw_rules_python/rules/py/no-star-import.cases.md +11 -0
- codelaw_rules_python/rules/py/no-star-import.md +47 -0
- codelaw_rules_python/rules/py/no-tunable-literals.cases.md +54 -0
- codelaw_rules_python/rules/py/no-tunable-literals.md +58 -0
- codelaw_rules_python/rules/py/none-comparison.md +36 -0
- codelaw_rules_python/rules/py/not-in-test.md +35 -0
- codelaw_rules_python/rules/py/not-is-test.md +35 -0
- codelaw_rules_python/rules/py/percent-format-count-mismatch.md +38 -0
- codelaw_rules_python/rules/py/percent-format-expected-mapping.md +38 -0
- codelaw_rules_python/rules/py/percent-format-expected-sequence.md +38 -0
- codelaw_rules_python/rules/py/percent-format-invalid.md +38 -0
- codelaw_rules_python/rules/py/percent-format-missing-argument.md +38 -0
- codelaw_rules_python/rules/py/percent-format-mixed-placeholders.md +38 -0
- codelaw_rules_python/rules/py/percent-format-star-requires-sequence.md +38 -0
- codelaw_rules_python/rules/py/percent-format-unsupported-character.md +38 -0
- codelaw_rules_python/rules/py/percent-format-unused-named-argument.md +37 -0
- codelaw_rules_python/rules/py/raise-literal.md +38 -0
- codelaw_rules_python/rules/py/raise-not-implemented.md +38 -0
- codelaw_rules_python/rules/py/raise-without-from-inside-except.cases.md +46 -0
- codelaw_rules_python/rules/py/raise-without-from-inside-except.md +45 -0
- codelaw_rules_python/rules/py/re-sub-positional-args.md +42 -0
- codelaw_rules_python/rules/py/redefined-while-unused.cases.md +186 -0
- codelaw_rules_python/rules/py/redefined-while-unused.md +53 -0
- codelaw_rules_python/rules/py/redundant-tuple-in-exception-handler.md +39 -0
- codelaw_rules_python/rules/py/return-outside-function.md +41 -0
- codelaw_rules_python/rules/py/reuse-of-groupby-generator.cases.md +40 -0
- codelaw_rules_python/rules/py/reuse-of-groupby-generator.md +46 -0
- codelaw_rules_python/rules/py/setattr-with-constant.md +33 -0
- codelaw_rules_python/rules/py/star-arg-unpacking-after-keyword-arg.md +38 -0
- codelaw_rules_python/rules/py/star-import-usage.cases.md +35 -0
- codelaw_rules_python/rules/py/star-import-usage.md +43 -0
- codelaw_rules_python/rules/py/static-key-dict-comprehension.md +38 -0
- codelaw_rules_python/rules/py/strip-with-multi-characters.md +39 -0
- codelaw_rules_python/rules/py/too-many-expressions-in-star-assignment.md +38 -0
- codelaw_rules_python/rules/py/true-false-comparison.cases.md +33 -0
- codelaw_rules_python/rules/py/true-false-comparison.md +36 -0
- codelaw_rules_python/rules/py/type-comparison.md +41 -0
- codelaw_rules_python/rules/py/unary-prefix-increment.md +40 -0
- codelaw_rules_python/rules/py/undefined-export.md +55 -0
- codelaw_rules_python/rules/py/undefined-future-feature.md +39 -0
- codelaw_rules_python/rules/py/undefined-local.cases.md +80 -0
- codelaw_rules_python/rules/py/undefined-local.md +48 -0
- codelaw_rules_python/rules/py/undefined-name.cases.md +132 -0
- codelaw_rules_python/rules/py/undefined-name.md +42 -0
- codelaw_rules_python/rules/py/unintentional-type-annotation.cases.md +13 -0
- codelaw_rules_python/rules/py/unintentional-type-annotation.md +38 -0
- codelaw_rules_python/rules/py/unreliable-callable-check.md +36 -0
- codelaw_rules_python/rules/py/unsorted-imports.cases.md +159 -0
- codelaw_rules_python/rules/py/unsorted-imports.md +60 -0
- codelaw_rules_python/rules/py/unused-annotation.md +36 -0
- codelaw_rules_python/rules/py/unused-import.cases.md +289 -0
- codelaw_rules_python/rules/py/unused-import.md +45 -0
- codelaw_rules_python/rules/py/unused-loop-control-variable.cases.md +30 -0
- codelaw_rules_python/rules/py/unused-loop-control-variable.md +36 -0
- codelaw_rules_python/rules/py/unused-variable.cases.md +169 -0
- codelaw_rules_python/rules/py/unused-variable.md +38 -0
- codelaw_rules_python/rules/py/useless-comparison.md +41 -0
- codelaw_rules_python/rules/py/useless-contextlib-suppress.md +44 -0
- codelaw_rules_python/rules/py/useless-expression.cases.md +57 -0
- codelaw_rules_python/rules/py/useless-expression.md +45 -0
- codelaw_rules_python/rules/py/yield-outside-function.md +44 -0
- codelaw_rules_python/rules/py/zip-without-explicit-strict.cases.md +26 -0
- codelaw_rules_python/rules/py/zip-without-explicit-strict.md +46 -0
- codelaw_rules_python-0.2.0.dist-info/METADATA +199 -0
- codelaw_rules_python-0.2.0.dist-info/RECORD +139 -0
- codelaw_rules_python-0.2.0.dist-info/WHEEL +4 -0
- codelaw_rules_python-0.2.0.dist-info/licenses/LICENSE +23 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# py/undefined-name — cases
|
|
2
|
+
|
|
3
|
+
### case: class body sees its own names
|
|
4
|
+
|
|
5
|
+
```py
|
|
6
|
+
class C:
|
|
7
|
+
x = 1
|
|
8
|
+
y = x + 1
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```expect
|
|
12
|
+
(none)
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### case: methods don't see class names
|
|
16
|
+
|
|
17
|
+
```py
|
|
18
|
+
class C:
|
|
19
|
+
x = 1
|
|
20
|
+
def m(self):
|
|
21
|
+
return x
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```expect
|
|
25
|
+
4:16 py/undefined-name
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### case: __class__ in a method
|
|
29
|
+
|
|
30
|
+
```py
|
|
31
|
+
class C:
|
|
32
|
+
def m(self):
|
|
33
|
+
return __class__
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```expect
|
|
37
|
+
(none)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### case: guarded by NameError
|
|
41
|
+
|
|
42
|
+
```py
|
|
43
|
+
try:
|
|
44
|
+
unicode
|
|
45
|
+
except NameError:
|
|
46
|
+
unicode = str
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```expect
|
|
50
|
+
(none)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### case: star import makes everything possible
|
|
54
|
+
|
|
55
|
+
```py
|
|
56
|
+
from os.path import *
|
|
57
|
+
print(join)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```expect
|
|
61
|
+
(none)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### case: comprehension variable
|
|
65
|
+
|
|
66
|
+
```py
|
|
67
|
+
values = [x for x in range(3)]
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
```expect
|
|
71
|
+
(none)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### case: module dunders
|
|
75
|
+
|
|
76
|
+
```py
|
|
77
|
+
print(__file__, __name__, __doc__)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
```expect
|
|
81
|
+
(none)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### case: del of an undefined name
|
|
85
|
+
|
|
86
|
+
```py
|
|
87
|
+
del missing
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
```expect
|
|
91
|
+
1:5 py/undefined-name
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### case: PEP 695 type parameters reach annotations, bases and methods
|
|
95
|
+
|
|
96
|
+
```py
|
|
97
|
+
class Base[T]:
|
|
98
|
+
pass
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def first[T](xs: list[T]) -> T:
|
|
102
|
+
return xs[0]
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class Box[T](Base[T]):
|
|
106
|
+
def get(self) -> T:
|
|
107
|
+
item: T = self.item
|
|
108
|
+
return item
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
type Pair[K] = tuple[K, K]
|
|
112
|
+
p: Pair[int] = (1, 2)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
```expect
|
|
116
|
+
(none)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### case: a type parameter is not visible outside its declaration
|
|
120
|
+
|
|
121
|
+
```py
|
|
122
|
+
def first[T](xs: list[T], default=T) -> T:
|
|
123
|
+
return xs[0]
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
x: T
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
```expect
|
|
130
|
+
1:35 py/undefined-name
|
|
131
|
+
5:4 py/undefined-name
|
|
132
|
+
```
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: py/undefined-name
|
|
3
|
+
title: "Every name read is defined"
|
|
4
|
+
languages: [python]
|
|
5
|
+
fix: manual
|
|
6
|
+
fix_reason: "whether to import, define or correct the name needs a person"
|
|
7
|
+
stability: preview
|
|
8
|
+
options:
|
|
9
|
+
builtins: { type: "string[]", default: [], description: "extra names to treat as builtins" }
|
|
10
|
+
ported_from: [ruff/F821]
|
|
11
|
+
since: 0.1.0
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
Every name the module reads is bound in an enclosing scope, imported, or one of Python's builtins.
|
|
15
|
+
|
|
16
|
+
**Why:** An undefined name raises `NameError` when the line runs — usually a typo or a missing import.
|
|
17
|
+
|
|
18
|
+
```codelaw
|
|
19
|
+
native: py/undefined-name
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## How to fix
|
|
23
|
+
|
|
24
|
+
1. Correct a misspelled name, or import or define it.
|
|
25
|
+
|
|
26
|
+
## Examples
|
|
27
|
+
|
|
28
|
+
### Python
|
|
29
|
+
|
|
30
|
+
#### Don't
|
|
31
|
+
|
|
32
|
+
```py
|
|
33
|
+
def total(items):
|
|
34
|
+
return sum(item.price for item in itmes)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
#### Do
|
|
38
|
+
|
|
39
|
+
```py
|
|
40
|
+
def total(items):
|
|
41
|
+
return sum(item.price for item in items)
|
|
42
|
+
```
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: py/unintentional-type-annotation
|
|
3
|
+
title: "No annotation where an assignment was meant"
|
|
4
|
+
languages: [python]
|
|
5
|
+
fix: manual
|
|
6
|
+
fix_reason: "whether an assignment was meant needs a person"
|
|
7
|
+
stability: preview
|
|
8
|
+
ported_from: [ruff/B032]
|
|
9
|
+
since: 0.1.0
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
An attribute or item isn't annotated without a value (`obj.x: 1`, `d["k"]: v`).
|
|
13
|
+
|
|
14
|
+
**Why:** This is almost always a typo for `=`: nothing is assigned.
|
|
15
|
+
|
|
16
|
+
```codelaw
|
|
17
|
+
native: py/unintentional-type-annotation
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## How to fix
|
|
21
|
+
|
|
22
|
+
1. Replace `:` with `=` if an assignment was meant.
|
|
23
|
+
|
|
24
|
+
## Examples
|
|
25
|
+
|
|
26
|
+
### Python
|
|
27
|
+
|
|
28
|
+
#### Don't
|
|
29
|
+
|
|
30
|
+
```py
|
|
31
|
+
config["debug"]: True
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
#### Do
|
|
35
|
+
|
|
36
|
+
```py
|
|
37
|
+
config["debug"] = True
|
|
38
|
+
```
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: py/unreliable-callable-check
|
|
3
|
+
title: "Test for callables with `callable()`"
|
|
4
|
+
languages: [python]
|
|
5
|
+
fix: suggest
|
|
6
|
+
fix_reason: "objects that define `__getattr__` can answer differently to the two tests"
|
|
7
|
+
stability: preview
|
|
8
|
+
ported_from: [ruff/B004]
|
|
9
|
+
since: 0.1.0
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
Code tests whether an object can be called with `callable(x)`, not by looking for a `__call__` attribute.
|
|
13
|
+
|
|
14
|
+
**Why:** `hasattr(x, "__call__")` can be fooled by `__getattr__` and misses some callables.
|
|
15
|
+
|
|
16
|
+
```codelaw
|
|
17
|
+
native: py/unreliable-callable-check
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Examples
|
|
21
|
+
|
|
22
|
+
### Python
|
|
23
|
+
|
|
24
|
+
#### Don't
|
|
25
|
+
|
|
26
|
+
```py
|
|
27
|
+
if hasattr(handler, "__call__"):
|
|
28
|
+
handler()
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
#### Do
|
|
32
|
+
|
|
33
|
+
```py
|
|
34
|
+
if callable(handler):
|
|
35
|
+
handler()
|
|
36
|
+
```
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# py/unsorted-imports — cases
|
|
2
|
+
|
|
3
|
+
### case: same module ordered by first name
|
|
4
|
+
|
|
5
|
+
```py
|
|
6
|
+
from multiprocessing import pool
|
|
7
|
+
from multiprocessing import Pool as ProcessPool
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```expect
|
|
11
|
+
1:1 py/unsorted-imports
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```fixed
|
|
15
|
+
from multiprocessing import Pool as ProcessPool
|
|
16
|
+
from multiprocessing import pool
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### case: members and merging
|
|
20
|
+
|
|
21
|
+
```py
|
|
22
|
+
from collections import deque, OrderedDict
|
|
23
|
+
from collections import abc
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```expect
|
|
27
|
+
1:1 py/unsorted-imports
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```fixed
|
|
31
|
+
from collections import OrderedDict, abc, deque
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### case: relative imports furthest first
|
|
35
|
+
|
|
36
|
+
```py
|
|
37
|
+
from . import a
|
|
38
|
+
from .. import b
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
```expect
|
|
42
|
+
1:1 py/unsorted-imports
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```fixed
|
|
46
|
+
from .. import b
|
|
47
|
+
from . import a
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### case: a comment moves with its import
|
|
51
|
+
|
|
52
|
+
```py
|
|
53
|
+
import sys
|
|
54
|
+
# json is fast enough
|
|
55
|
+
import json
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
```expect
|
|
59
|
+
1:1 py/unsorted-imports
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
```fixed
|
|
63
|
+
# json is fast enough
|
|
64
|
+
import json
|
|
65
|
+
import sys
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### case: blocks are separate
|
|
69
|
+
|
|
70
|
+
```py
|
|
71
|
+
import sys
|
|
72
|
+
|
|
73
|
+
x = 1
|
|
74
|
+
|
|
75
|
+
import os
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```expect
|
|
79
|
+
(none)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### case: nested blocks
|
|
83
|
+
|
|
84
|
+
```py
|
|
85
|
+
def f():
|
|
86
|
+
import sys
|
|
87
|
+
import os
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
```expect
|
|
91
|
+
2:5 py/unsorted-imports
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
```fixed
|
|
95
|
+
def f():
|
|
96
|
+
import os
|
|
97
|
+
import sys
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### case: future first
|
|
101
|
+
|
|
102
|
+
```py
|
|
103
|
+
import os
|
|
104
|
+
from __future__ import annotations
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
```expect
|
|
108
|
+
1:1 py/unsorted-imports
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
```fixed
|
|
112
|
+
from __future__ import annotations
|
|
113
|
+
|
|
114
|
+
import os
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### case: case-insensitive module order
|
|
118
|
+
|
|
119
|
+
```py
|
|
120
|
+
import apple
|
|
121
|
+
import Zebra
|
|
122
|
+
import requests
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
```expect
|
|
126
|
+
1:1 py/unsorted-imports
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
```fixed
|
|
130
|
+
import apple
|
|
131
|
+
import requests
|
|
132
|
+
import Zebra
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### case: already sorted with sections
|
|
136
|
+
|
|
137
|
+
```py
|
|
138
|
+
import os
|
|
139
|
+
import sys
|
|
140
|
+
|
|
141
|
+
import requests
|
|
142
|
+
|
|
143
|
+
from . import local
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
```expect
|
|
147
|
+
(none)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### case: isort: skip
|
|
151
|
+
|
|
152
|
+
```py
|
|
153
|
+
import sys # isort: skip
|
|
154
|
+
import os
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
```expect
|
|
158
|
+
(none)
|
|
159
|
+
```
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: py/unsorted-imports
|
|
3
|
+
title: "Imports are sorted"
|
|
4
|
+
languages: [python]
|
|
5
|
+
fix: auto
|
|
6
|
+
stability: preview
|
|
7
|
+
options:
|
|
8
|
+
known_first_party: { type: "string[]", default: [], description: "top-level modules that belong to this project" }
|
|
9
|
+
line_length: { type: integer, default: 88, description: "the width a from-import may reach before it is wrapped" }
|
|
10
|
+
ported_from: [ruff/I001]
|
|
11
|
+
since: 0.1.0
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
Each block of consecutive imports is in isort's order: sections (`__future__`, standard library, third party, first party, local), plain `import x` before `from x import y` within a section, then by module name, and the names of a `from` import sorted (constants, classes, then the rest).
|
|
15
|
+
|
|
16
|
+
**Why:** A fixed order makes imports easy to scan and stops merge conflicts from reordering.
|
|
17
|
+
|
|
18
|
+
```codelaw
|
|
19
|
+
native: py/unsorted-imports
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Examples
|
|
23
|
+
|
|
24
|
+
### Python
|
|
25
|
+
|
|
26
|
+
#### Don't
|
|
27
|
+
|
|
28
|
+
```py
|
|
29
|
+
import sys
|
|
30
|
+
import os
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### Don't
|
|
34
|
+
|
|
35
|
+
```py
|
|
36
|
+
import requests
|
|
37
|
+
import os
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
#### Do
|
|
41
|
+
|
|
42
|
+
```py
|
|
43
|
+
import os
|
|
44
|
+
import sys
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### Do
|
|
48
|
+
|
|
49
|
+
```py
|
|
50
|
+
import os
|
|
51
|
+
|
|
52
|
+
import requests
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Differences from the source
|
|
56
|
+
|
|
57
|
+
- The rule checks order. Line wrapping and the blank lines after the import block belong to the formatter; the fix separates sections with one blank line and wraps a `from` import past `line_length`, as isort does.
|
|
58
|
+
- First-party modules are those listed in `known_first_party`; Ruff also detects them from the project's source folders.
|
|
59
|
+
- `import a, b` is not reported here (that is `py/multiple-imports-on-one-line`), and `from m import a, b as c` is not reported for mixing aliases; the fix still writes each on its own line.
|
|
60
|
+
- A block with comments the fix can't place (inside a parenthesised import, or loose between imports) is reported without an edit.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: py/unused-annotation
|
|
3
|
+
title: "Every annotated local is used"
|
|
4
|
+
languages: [python]
|
|
5
|
+
fix: auto
|
|
6
|
+
stability: preview
|
|
7
|
+
ported_from: [ruff/F842]
|
|
8
|
+
since: 0.1.0
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
A local variable annotated without a value (`x: int`) is used somewhere in the function.
|
|
12
|
+
|
|
13
|
+
**Why:** A local annotation that nothing uses declares nothing; local annotations aren't even evaluated.
|
|
14
|
+
|
|
15
|
+
```codelaw
|
|
16
|
+
native: py/unused-annotation
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Examples
|
|
20
|
+
|
|
21
|
+
### Python
|
|
22
|
+
|
|
23
|
+
#### Don't
|
|
24
|
+
|
|
25
|
+
```py
|
|
26
|
+
def total():
|
|
27
|
+
count: int
|
|
28
|
+
return 0
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
#### Do
|
|
32
|
+
|
|
33
|
+
```py
|
|
34
|
+
def total():
|
|
35
|
+
return 0
|
|
36
|
+
```
|