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.
Files changed (139) hide show
  1. codelaw_rules_python/LICENSE +23 -0
  2. codelaw_rules_python/NOTICE +14 -0
  3. codelaw_rules_python/README.md +189 -0
  4. codelaw_rules_python/codelaw-pack.toml +6 -0
  5. codelaw_rules_python/corpus.toml +103 -0
  6. codelaw_rules_python/data/python/stdlib.json +331 -0
  7. codelaw_rules_python/presets/py/conventions.toml +5 -0
  8. codelaw_rules_python/presets/py/project.toml +4 -0
  9. codelaw_rules_python/presets/py/recommended.toml +99 -0
  10. codelaw_rules_python/rules/graph/no-unused-exports.md +69 -0
  11. codelaw_rules_python/rules/py/abstract-base-class-without-abstract-method.md +50 -0
  12. codelaw_rules_python/rules/py/all-sorted.cases.md +74 -0
  13. codelaw_rules_python/rules/py/all-sorted.md +33 -0
  14. codelaw_rules_python/rules/py/ambiguous-class-name.md +41 -0
  15. codelaw_rules_python/rules/py/ambiguous-function-name.md +41 -0
  16. codelaw_rules_python/rules/py/ambiguous-variable-name.cases.md +47 -0
  17. codelaw_rules_python/rules/py/ambiguous-variable-name.md +39 -0
  18. codelaw_rules_python/rules/py/assert-false.md +34 -0
  19. codelaw_rules_python/rules/py/assert-raises-exception.md +51 -0
  20. codelaw_rules_python/rules/py/assert-tuple.md +38 -0
  21. codelaw_rules_python/rules/py/assignment-to-os-environ.md +43 -0
  22. codelaw_rules_python/rules/py/batched-without-explicit-strict.md +52 -0
  23. codelaw_rules_python/rules/py/break-outside-loop.md +41 -0
  24. codelaw_rules_python/rules/py/cached-instance-method.md +55 -0
  25. codelaw_rules_python/rules/py/continue-outside-loop.md +41 -0
  26. codelaw_rules_python/rules/py/default-except-not-last.md +48 -0
  27. codelaw_rules_python/rules/py/duplicate-dict-key-variable.md +39 -0
  28. codelaw_rules_python/rules/py/duplicate-dict-key.md +39 -0
  29. codelaw_rules_python/rules/py/duplicate-handler-exception.md +43 -0
  30. codelaw_rules_python/rules/py/duplicate-set-value.md +33 -0
  31. codelaw_rules_python/rules/py/duplicate-try-block-exception.md +47 -0
  32. codelaw_rules_python/rules/py/empty-method-without-abstract-decorator.md +55 -0
  33. codelaw_rules_python/rules/py/except-with-empty-tuple.md +44 -0
  34. codelaw_rules_python/rules/py/except-with-non-exception-classes.md +44 -0
  35. codelaw_rules_python/rules/py/f-string-docstring.md +40 -0
  36. codelaw_rules_python/rules/py/f-string-missing-placeholders.cases.md +49 -0
  37. codelaw_rules_python/rules/py/f-string-missing-placeholders.md +33 -0
  38. codelaw_rules_python/rules/py/format-invalid.md +38 -0
  39. codelaw_rules_python/rules/py/format-missing-argument.md +38 -0
  40. codelaw_rules_python/rules/py/format-mixed-numbering.md +38 -0
  41. codelaw_rules_python/rules/py/format-unused-named-argument.md +37 -0
  42. codelaw_rules_python/rules/py/format-unused-positional-argument.md +37 -0
  43. codelaw_rules_python/rules/py/forward-annotation-syntax-error.md +44 -0
  44. codelaw_rules_python/rules/py/function-call-in-default-argument.md +51 -0
  45. codelaw_rules_python/rules/py/function-uses-loop-variable.cases.md +57 -0
  46. codelaw_rules_python/rules/py/function-uses-loop-variable.md +42 -0
  47. codelaw_rules_python/rules/py/getattr-with-constant.cases.md +31 -0
  48. codelaw_rules_python/rules/py/getattr-with-constant.md +33 -0
  49. codelaw_rules_python/rules/py/if-tuple.md +40 -0
  50. codelaw_rules_python/rules/py/import-shadowed-by-loop-var.md +45 -0
  51. codelaw_rules_python/rules/py/invalid-print-syntax.md +42 -0
  52. codelaw_rules_python/rules/py/is-literal.md +36 -0
  53. codelaw_rules_python/rules/py/jump-statement-in-finally.cases.md +15 -0
  54. codelaw_rules_python/rules/py/jump-statement-in-finally.md +47 -0
  55. codelaw_rules_python/rules/py/lambda-assignment.cases.md +48 -0
  56. codelaw_rules_python/rules/py/lambda-assignment.md +35 -0
  57. codelaw_rules_python/rules/py/late-future-import.md +40 -0
  58. codelaw_rules_python/rules/py/loop-variable-overrides-iterator.md +41 -0
  59. codelaw_rules_python/rules/py/map-without-explicit-strict.md +46 -0
  60. codelaw_rules_python/rules/py/module-import-not-at-top.cases.md +64 -0
  61. codelaw_rules_python/rules/py/module-import-not-at-top.md +46 -0
  62. codelaw_rules_python/rules/py/multiple-imports-on-one-line.md +38 -0
  63. codelaw_rules_python/rules/py/multiple-starred-expressions.md +38 -0
  64. codelaw_rules_python/rules/py/mutable-contextvar-default.md +42 -0
  65. codelaw_rules_python/rules/py/nested-star-import.md +42 -0
  66. codelaw_rules_python/rules/py/no-bare-except.cases.md +37 -0
  67. codelaw_rules_python/rules/py/no-bare-except.md +44 -0
  68. codelaw_rules_python/rules/py/no-explicit-stacklevel.md +38 -0
  69. codelaw_rules_python/rules/py/no-mutable-default.cases.md +58 -0
  70. codelaw_rules_python/rules/py/no-mutable-default.md +44 -0
  71. codelaw_rules_python/rules/py/no-print.cases.md +40 -0
  72. codelaw_rules_python/rules/py/no-print.md +42 -0
  73. codelaw_rules_python/rules/py/no-star-import.cases.md +11 -0
  74. codelaw_rules_python/rules/py/no-star-import.md +47 -0
  75. codelaw_rules_python/rules/py/no-tunable-literals.cases.md +54 -0
  76. codelaw_rules_python/rules/py/no-tunable-literals.md +58 -0
  77. codelaw_rules_python/rules/py/none-comparison.md +36 -0
  78. codelaw_rules_python/rules/py/not-in-test.md +35 -0
  79. codelaw_rules_python/rules/py/not-is-test.md +35 -0
  80. codelaw_rules_python/rules/py/percent-format-count-mismatch.md +38 -0
  81. codelaw_rules_python/rules/py/percent-format-expected-mapping.md +38 -0
  82. codelaw_rules_python/rules/py/percent-format-expected-sequence.md +38 -0
  83. codelaw_rules_python/rules/py/percent-format-invalid.md +38 -0
  84. codelaw_rules_python/rules/py/percent-format-missing-argument.md +38 -0
  85. codelaw_rules_python/rules/py/percent-format-mixed-placeholders.md +38 -0
  86. codelaw_rules_python/rules/py/percent-format-star-requires-sequence.md +38 -0
  87. codelaw_rules_python/rules/py/percent-format-unsupported-character.md +38 -0
  88. codelaw_rules_python/rules/py/percent-format-unused-named-argument.md +37 -0
  89. codelaw_rules_python/rules/py/raise-literal.md +38 -0
  90. codelaw_rules_python/rules/py/raise-not-implemented.md +38 -0
  91. codelaw_rules_python/rules/py/raise-without-from-inside-except.cases.md +46 -0
  92. codelaw_rules_python/rules/py/raise-without-from-inside-except.md +45 -0
  93. codelaw_rules_python/rules/py/re-sub-positional-args.md +42 -0
  94. codelaw_rules_python/rules/py/redefined-while-unused.cases.md +186 -0
  95. codelaw_rules_python/rules/py/redefined-while-unused.md +53 -0
  96. codelaw_rules_python/rules/py/redundant-tuple-in-exception-handler.md +39 -0
  97. codelaw_rules_python/rules/py/return-outside-function.md +41 -0
  98. codelaw_rules_python/rules/py/reuse-of-groupby-generator.cases.md +40 -0
  99. codelaw_rules_python/rules/py/reuse-of-groupby-generator.md +46 -0
  100. codelaw_rules_python/rules/py/setattr-with-constant.md +33 -0
  101. codelaw_rules_python/rules/py/star-arg-unpacking-after-keyword-arg.md +38 -0
  102. codelaw_rules_python/rules/py/star-import-usage.cases.md +35 -0
  103. codelaw_rules_python/rules/py/star-import-usage.md +43 -0
  104. codelaw_rules_python/rules/py/static-key-dict-comprehension.md +38 -0
  105. codelaw_rules_python/rules/py/strip-with-multi-characters.md +39 -0
  106. codelaw_rules_python/rules/py/too-many-expressions-in-star-assignment.md +38 -0
  107. codelaw_rules_python/rules/py/true-false-comparison.cases.md +33 -0
  108. codelaw_rules_python/rules/py/true-false-comparison.md +36 -0
  109. codelaw_rules_python/rules/py/type-comparison.md +41 -0
  110. codelaw_rules_python/rules/py/unary-prefix-increment.md +40 -0
  111. codelaw_rules_python/rules/py/undefined-export.md +55 -0
  112. codelaw_rules_python/rules/py/undefined-future-feature.md +39 -0
  113. codelaw_rules_python/rules/py/undefined-local.cases.md +80 -0
  114. codelaw_rules_python/rules/py/undefined-local.md +48 -0
  115. codelaw_rules_python/rules/py/undefined-name.cases.md +132 -0
  116. codelaw_rules_python/rules/py/undefined-name.md +42 -0
  117. codelaw_rules_python/rules/py/unintentional-type-annotation.cases.md +13 -0
  118. codelaw_rules_python/rules/py/unintentional-type-annotation.md +38 -0
  119. codelaw_rules_python/rules/py/unreliable-callable-check.md +36 -0
  120. codelaw_rules_python/rules/py/unsorted-imports.cases.md +159 -0
  121. codelaw_rules_python/rules/py/unsorted-imports.md +60 -0
  122. codelaw_rules_python/rules/py/unused-annotation.md +36 -0
  123. codelaw_rules_python/rules/py/unused-import.cases.md +289 -0
  124. codelaw_rules_python/rules/py/unused-import.md +45 -0
  125. codelaw_rules_python/rules/py/unused-loop-control-variable.cases.md +30 -0
  126. codelaw_rules_python/rules/py/unused-loop-control-variable.md +36 -0
  127. codelaw_rules_python/rules/py/unused-variable.cases.md +169 -0
  128. codelaw_rules_python/rules/py/unused-variable.md +38 -0
  129. codelaw_rules_python/rules/py/useless-comparison.md +41 -0
  130. codelaw_rules_python/rules/py/useless-contextlib-suppress.md +44 -0
  131. codelaw_rules_python/rules/py/useless-expression.cases.md +57 -0
  132. codelaw_rules_python/rules/py/useless-expression.md +45 -0
  133. codelaw_rules_python/rules/py/yield-outside-function.md +44 -0
  134. codelaw_rules_python/rules/py/zip-without-explicit-strict.cases.md +26 -0
  135. codelaw_rules_python/rules/py/zip-without-explicit-strict.md +46 -0
  136. codelaw_rules_python-0.2.0.dist-info/METADATA +199 -0
  137. codelaw_rules_python-0.2.0.dist-info/RECORD +139 -0
  138. codelaw_rules_python-0.2.0.dist-info/WHEEL +4 -0
  139. 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,13 @@
1
+ # py/unintentional-type-annotation — cases
2
+
3
+ ### case: self attributes are declarations
4
+
5
+ ```py
6
+ class C:
7
+ def __init__(self):
8
+ self.x: int
9
+ ```
10
+
11
+ ```expect
12
+ (none)
13
+ ```
@@ -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
+ ```