codeveto 0.1.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.
- codeveto/__init__.py +3 -0
- codeveto/__main__.py +8 -0
- codeveto/ai/__init__.py +19 -0
- codeveto/ai/harness.py +303 -0
- codeveto/ai/packets.py +855 -0
- codeveto/ai/policy.py +222 -0
- codeveto/ai/prompt.py +101 -0
- codeveto/ai/providers/__init__.py +178 -0
- codeveto/ai/providers/claude.py +247 -0
- codeveto/ai/providers/openai_compat.py +246 -0
- codeveto/ai/runner.py +293 -0
- codeveto/ai/verify.py +324 -0
- codeveto/anchor.py +211 -0
- codeveto/cli.py +247 -0
- codeveto/cli_explain.py +49 -0
- codeveto/cli_hook.py +31 -0
- codeveto/cli_init.py +254 -0
- codeveto/cli_mcp.py +29 -0
- codeveto/cli_packets.py +35 -0
- codeveto/cli_publish.py +29 -0
- codeveto/cli_review.py +362 -0
- codeveto/cli_rules.py +78 -0
- codeveto/cli_verify.py +47 -0
- codeveto/config.py +1229 -0
- codeveto/data/README.md +16 -0
- codeveto/data/ai-wire.schema.json +53 -0
- codeveto/data/prompts/system.md +57 -0
- codeveto/data/report-v1.schema.json +268 -0
- codeveto/data/templates/forgejo-codeveto.yml +104 -0
- codeveto/data/templates/github-codeveto.yml +18 -0
- codeveto/embedded.py +311 -0
- codeveto/engine.py +1570 -0
- codeveto/git.py +1549 -0
- codeveto/globs.py +348 -0
- codeveto/integrations/__init__.py +1 -0
- codeveto/integrations/hooks.py +327 -0
- codeveto/integrations/mcp.py +770 -0
- codeveto/integrations/mcp_ai.py +77 -0
- codeveto/integrations/prepush.py +222 -0
- codeveto/jsonsafe.py +60 -0
- codeveto/languages.py +539 -0
- codeveto/lexers.py +2560 -0
- codeveto/model.py +372 -0
- codeveto/publish/__init__.py +28 -0
- codeveto/publish/action.py +330 -0
- codeveto/publish/common.py +84 -0
- codeveto/publish/diff.py +208 -0
- codeveto/publish/event.py +241 -0
- codeveto/publish/forgejo.py +184 -0
- codeveto/publish/github.py +380 -0
- codeveto/publish/http.py +263 -0
- codeveto/publish/pull.py +218 -0
- codeveto/publish/untrusted.py +110 -0
- codeveto/py.typed +0 -0
- codeveto/redact.py +113 -0
- codeveto/render/__init__.py +64 -0
- codeveto/render/agent.py +43 -0
- codeveto/render/github.py +101 -0
- codeveto/render/json.py +23 -0
- codeveto/render/markdown.py +208 -0
- codeveto/render/safe.py +249 -0
- codeveto/render/sarif.py +217 -0
- codeveto/render/text.py +210 -0
- codeveto/report.py +252 -0
- codeveto/rules/__init__.py +84 -0
- codeveto/rules/agent.py +2972 -0
- codeveto/rules/base.py +1026 -0
- codeveto/rules/ci.py +1708 -0
- codeveto/rules/config.py +493 -0
- codeveto/rules/deps.py +1693 -0
- codeveto/rules/docker.py +479 -0
- codeveto/rules/examples/__init__.py +148 -0
- codeveto/rules/examples/agent.py +839 -0
- codeveto/rules/examples/ci.py +1046 -0
- codeveto/rules/examples/config.py +191 -0
- codeveto/rules/examples/deps.py +369 -0
- codeveto/rules/examples/docker.py +157 -0
- codeveto/rules/examples/go.py +241 -0
- codeveto/rules/examples/hygiene.py +466 -0
- codeveto/rules/examples/js.py +462 -0
- codeveto/rules/examples/python.py +595 -0
- codeveto/rules/examples/rust.py +268 -0
- codeveto/rules/examples/secrets.py +561 -0
- codeveto/rules/examples/security.py +608 -0
- codeveto/rules/examples/shell.py +174 -0
- codeveto/rules/examples/sql.py +194 -0
- codeveto/rules/examples/tests.py +497 -0
- codeveto/rules/go.py +328 -0
- codeveto/rules/hygiene.py +1094 -0
- codeveto/rules/js.py +768 -0
- codeveto/rules/python.py +1642 -0
- codeveto/rules/rust.py +209 -0
- codeveto/rules/secrets.py +1335 -0
- codeveto/rules/security.py +3680 -0
- codeveto/rules/shell.py +867 -0
- codeveto/rules/sql.py +629 -0
- codeveto/rules/tests.py +1404 -0
- codeveto/schema.py +384 -0
- codeveto/statements.py +607 -0
- codeveto/worktree.py +1176 -0
- codeveto/yamlish.py +1179 -0
- codeveto-0.1.0.dist-info/METADATA +253 -0
- codeveto-0.1.0.dist-info/RECORD +106 -0
- codeveto-0.1.0.dist-info/WHEEL +4 -0
- codeveto-0.1.0.dist-info/entry_points.txt +2 -0
- codeveto-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
"""Examples for the python pack."""
|
|
2
|
+
|
|
3
|
+
from codeveto.rules.base import Change, Example, bad, good
|
|
4
|
+
from codeveto.rules.python import NEWEST_PYTHON as NEWEST
|
|
5
|
+
|
|
6
|
+
# Only the newest supported Python reports parse failures (SPEC 6.5); on older
|
|
7
|
+
# interpreters these bad examples make coverage partial and report nothing.
|
|
8
|
+
|
|
9
|
+
EXAMPLES = {
|
|
10
|
+
"python/syntax-error": [
|
|
11
|
+
bad(
|
|
12
|
+
"src/geometry.py",
|
|
13
|
+
"def area(width, height):\n return (width * height # expect\n",
|
|
14
|
+
old="def area(width, height):\n return width * height\n",
|
|
15
|
+
note="An unclosed parenthesis on a changed line.",
|
|
16
|
+
min_python=NEWEST,
|
|
17
|
+
),
|
|
18
|
+
bad(
|
|
19
|
+
"src/flags.py",
|
|
20
|
+
"def enabled(flag):\n if flag: # expect\n return False\n",
|
|
21
|
+
old="def enabled(flag):\n if flag:\n return True\n return False\n",
|
|
22
|
+
note="Deleting the only statement of a block breaks the file; the hit sits at the "
|
|
23
|
+
"deletion point.",
|
|
24
|
+
min_python=NEWEST,
|
|
25
|
+
),
|
|
26
|
+
bad(
|
|
27
|
+
"src/setup_hooks.py",
|
|
28
|
+
"def install():\n pass\nreturn True # expect\n",
|
|
29
|
+
old="def install():\n return True\n",
|
|
30
|
+
note="The parser accepts this, the compiler rejects 'return' outside a function.",
|
|
31
|
+
min_python=NEWEST,
|
|
32
|
+
),
|
|
33
|
+
good(
|
|
34
|
+
"src/geometry.py",
|
|
35
|
+
"def area(width: float, height: float) -> float:\n return width * height\n",
|
|
36
|
+
old="def area(width, height):\n return width * height\n",
|
|
37
|
+
),
|
|
38
|
+
good(
|
|
39
|
+
"src/legacy.py",
|
|
40
|
+
"def broken(:\n return 2\n",
|
|
41
|
+
old="def broken(:\n return 1\n",
|
|
42
|
+
note="The old side already failed to parse: coverage is partial, nothing is reported.",
|
|
43
|
+
),
|
|
44
|
+
good(
|
|
45
|
+
"docs/conf.py",
|
|
46
|
+
"project = 'demo'\nextensions = [\n",
|
|
47
|
+
old="project = 'demo'\n",
|
|
48
|
+
note="Language bug-pattern rules do not run on docs files.",
|
|
49
|
+
),
|
|
50
|
+
good(
|
|
51
|
+
"src/vectors.py",
|
|
52
|
+
"type Vector = list[float]\n\n\ndef first[T](items: list[T]) -> T:\n"
|
|
53
|
+
" return items[0]\n",
|
|
54
|
+
note="Python 3.12 syntax: older interpreters only make coverage partial.",
|
|
55
|
+
),
|
|
56
|
+
],
|
|
57
|
+
"python/is-literal": [
|
|
58
|
+
bad(
|
|
59
|
+
"src/jobs.py",
|
|
60
|
+
"def finished(job):\n return job.status is 'done' # expect\n",
|
|
61
|
+
note="Equal strings are not guaranteed to be the same object.",
|
|
62
|
+
),
|
|
63
|
+
bad(
|
|
64
|
+
"src/codes.py",
|
|
65
|
+
"def failed(code):\n return code is not -1 # expect\n",
|
|
66
|
+
old="def failed(code):\n return code != -1\n",
|
|
67
|
+
note="A negative number literal counts too.",
|
|
68
|
+
),
|
|
69
|
+
bad(
|
|
70
|
+
"src/filters.py",
|
|
71
|
+
"def empty(items):\n return items is [] # expect\n",
|
|
72
|
+
note="A list display is a new object, so this is always False.",
|
|
73
|
+
),
|
|
74
|
+
good(
|
|
75
|
+
"src/jobs.py",
|
|
76
|
+
"_MISSING = object()\n\n\ndef pick(value, default=_MISSING):\n"
|
|
77
|
+
" if value is None or value is True or default is _MISSING:\n"
|
|
78
|
+
" return default\n return value\n",
|
|
79
|
+
note="None, True and sentinels are singletons: 'is' is right.",
|
|
80
|
+
),
|
|
81
|
+
good(
|
|
82
|
+
"src/jobs.py",
|
|
83
|
+
"def finished(job):\n return job.status == 'done'\n",
|
|
84
|
+
old="def finished(job):\n return job.status == 'pending'\n",
|
|
85
|
+
),
|
|
86
|
+
good(
|
|
87
|
+
"tests/test_lists.py",
|
|
88
|
+
"def test_displays_are_new_objects():\n assert [] is not []\n"
|
|
89
|
+
" assert {} is not {}\n",
|
|
90
|
+
note="Two displays compared with each other: a deliberate identity test.",
|
|
91
|
+
),
|
|
92
|
+
],
|
|
93
|
+
"python/assert-tuple": [
|
|
94
|
+
bad(
|
|
95
|
+
"src/accounts.py",
|
|
96
|
+
"def close(account):\n assert (account.balance == 0, 'balance left') # expect\n"
|
|
97
|
+
" account.closed = True\n",
|
|
98
|
+
note="The parenthesised pair is a tuple, which is always true.",
|
|
99
|
+
),
|
|
100
|
+
bad(
|
|
101
|
+
"src/orders.py",
|
|
102
|
+
"def ship(order):\n assert ( # expect\n order.paid,\n"
|
|
103
|
+
" 'unpaid order',\n )\n order.ship()\n",
|
|
104
|
+
note="Spread over lines: reported at the assert.",
|
|
105
|
+
),
|
|
106
|
+
good(
|
|
107
|
+
"src/accounts.py",
|
|
108
|
+
"def close(account):\n assert account.balance == 0, 'balance left'\n"
|
|
109
|
+
" account.closed = True\n",
|
|
110
|
+
),
|
|
111
|
+
good(
|
|
112
|
+
"src/points.py",
|
|
113
|
+
"def check(point):\n assert (point.x, point.y) == (0, 0)\n",
|
|
114
|
+
note="Comparing two tuples is a real assertion.",
|
|
115
|
+
),
|
|
116
|
+
],
|
|
117
|
+
"python/duplicate-dict-key": [
|
|
118
|
+
bad(
|
|
119
|
+
"src/settings.py",
|
|
120
|
+
'DEFAULTS = {\n "timeout": 30,\n "retries": 3,\n "timeout": 5, # expect\n}\n',
|
|
121
|
+
note="The first timeout is silently overwritten.",
|
|
122
|
+
),
|
|
123
|
+
bad(
|
|
124
|
+
"src/labels.py",
|
|
125
|
+
'LABELS = {1: "one", True: "yes"} # expect\n',
|
|
126
|
+
note="1 and True are the same dict key.",
|
|
127
|
+
),
|
|
128
|
+
good(
|
|
129
|
+
"src/labels.py",
|
|
130
|
+
'LABELS = {"1": "string one", 1: "number one", (1, 2): "pair"}\n',
|
|
131
|
+
note="A string and a number are different keys.",
|
|
132
|
+
),
|
|
133
|
+
good(
|
|
134
|
+
"src/columns.py",
|
|
135
|
+
'def columns(prefix):\n return {f"{prefix}_id": int, f"{prefix}_name": str}\n',
|
|
136
|
+
note="Computed keys are not compared.",
|
|
137
|
+
),
|
|
138
|
+
],
|
|
139
|
+
"python/return-in-finally": [
|
|
140
|
+
bad(
|
|
141
|
+
"src/files.py",
|
|
142
|
+
"def read(path):\n handle = open(path)\n try:\n return handle.read()\n"
|
|
143
|
+
" finally:\n handle.close()\n return '' # expect\n",
|
|
144
|
+
note="Any read error is discarded and '' returned instead.",
|
|
145
|
+
),
|
|
146
|
+
bad(
|
|
147
|
+
"src/workers.py",
|
|
148
|
+
"def drain(queue):\n while True:\n try:\n queue.process()\n"
|
|
149
|
+
" finally:\n break # expect\n",
|
|
150
|
+
note="break leaves the finally block too.",
|
|
151
|
+
),
|
|
152
|
+
good(
|
|
153
|
+
"src/files.py",
|
|
154
|
+
"def read(path):\n handle = open(path)\n try:\n return handle.read()\n"
|
|
155
|
+
" finally:\n handle.close()\n",
|
|
156
|
+
),
|
|
157
|
+
good(
|
|
158
|
+
"src/hooks.py",
|
|
159
|
+
"def shutdown(hooks):\n try:\n hooks.flush()\n finally:\n"
|
|
160
|
+
" for hook in hooks:\n if hook.done:\n break\n"
|
|
161
|
+
" hook.close()\n",
|
|
162
|
+
note="This break ends a loop inside the finally block.",
|
|
163
|
+
),
|
|
164
|
+
],
|
|
165
|
+
"python/debugger": [
|
|
166
|
+
bad(
|
|
167
|
+
"src/billing.py",
|
|
168
|
+
"def total(lines):\n breakpoint() # expect\n return sum(lines)\n",
|
|
169
|
+
old="def total(lines):\n return sum(lines)\n",
|
|
170
|
+
),
|
|
171
|
+
bad(
|
|
172
|
+
"src/sync.py",
|
|
173
|
+
"def sync(items):\n import pdb; pdb.set_trace() # expect\n return items\n",
|
|
174
|
+
),
|
|
175
|
+
bad(
|
|
176
|
+
"src/views.py",
|
|
177
|
+
"from ipdb import set_trace as st\n\n\ndef index(request):\n"
|
|
178
|
+
" st() # expect\n return request\n",
|
|
179
|
+
note="An imported set_trace under another name.",
|
|
180
|
+
),
|
|
181
|
+
good(
|
|
182
|
+
"src/devtools.py",
|
|
183
|
+
"import os\n\n\ndef maybe_break():\n if os.environ.get('APP_DEBUGGER'):\n"
|
|
184
|
+
" breakpoint()\n",
|
|
185
|
+
note="A breakpoint behind a debugging switch is developer tooling.",
|
|
186
|
+
),
|
|
187
|
+
good(
|
|
188
|
+
"src/runner.py",
|
|
189
|
+
"import pdb\nimport sys\n\n\ndef run(main, post_mortem=False):\n try:\n"
|
|
190
|
+
" main()\n except Exception:\n if not post_mortem:\n"
|
|
191
|
+
" raise\n pdb.post_mortem(sys.exc_info()[2])\n",
|
|
192
|
+
note="post_mortem on a --pdb style flag is deliberate.",
|
|
193
|
+
),
|
|
194
|
+
good(
|
|
195
|
+
"src/help.py",
|
|
196
|
+
"HELP = 'Call breakpoint() where you want to stop.'\n",
|
|
197
|
+
note="Mentioned in a string only.",
|
|
198
|
+
),
|
|
199
|
+
good(
|
|
200
|
+
"src/debugger.py",
|
|
201
|
+
"import sys\n\n\ndef set_trace(header=None):\n pdb = Debugger()\n"
|
|
202
|
+
" pdb.set_trace(sys._getframe().f_back)\n\n\ndef main():\n"
|
|
203
|
+
" import pdb\n pdb.main()\n",
|
|
204
|
+
note="A debugger's own set_trace on its debugger object, not the pdb module.",
|
|
205
|
+
),
|
|
206
|
+
],
|
|
207
|
+
"python/missing-raise": [
|
|
208
|
+
bad(
|
|
209
|
+
"src/payments.py",
|
|
210
|
+
"def refund(amount):\n if amount < 0:\n"
|
|
211
|
+
" ValueError('negative refund') # expect\n return amount\n",
|
|
212
|
+
note="The error case falls through to the return.",
|
|
213
|
+
),
|
|
214
|
+
bad(
|
|
215
|
+
"src/cards.py",
|
|
216
|
+
"from payments.errors import CardDeclinedError\n\n\ndef charge(card):\n"
|
|
217
|
+
" if card.declined:\n CardDeclinedError(card.id) # expect\n"
|
|
218
|
+
" return card.charge()\n",
|
|
219
|
+
note="An imported exception class.",
|
|
220
|
+
),
|
|
221
|
+
good(
|
|
222
|
+
"src/payments.py",
|
|
223
|
+
"def refund(amount):\n if amount < 0:\n"
|
|
224
|
+
" raise ValueError('negative refund')\n return amount\n",
|
|
225
|
+
),
|
|
226
|
+
good(
|
|
227
|
+
"src/validation.py",
|
|
228
|
+
"def collect(rows, errors):\n for row in rows:\n if not row:\n"
|
|
229
|
+
" errors.append(ValueError('empty row'))\n"
|
|
230
|
+
" self_check = Reporter()\n self_check.ReportError('done')\n",
|
|
231
|
+
note="An exception stored for later, and a CamelCase method call.",
|
|
232
|
+
),
|
|
233
|
+
good(
|
|
234
|
+
"tests/test_errors.py",
|
|
235
|
+
"import pytest\n\n\ndef test_bad_keyword(self):\n"
|
|
236
|
+
" with pytest.raises(TypeError):\n ImportError(invalid='keyword')\n"
|
|
237
|
+
" with self.assertRaisesRegex(TypeError, 'arguments'):\n"
|
|
238
|
+
" ExceptionGroup('no errors')\n",
|
|
239
|
+
note="Inside pytest.raises or assertRaises the constructor call is what is tested.",
|
|
240
|
+
),
|
|
241
|
+
bad(
|
|
242
|
+
"src/jobs.py",
|
|
243
|
+
"def run(job, lock):\n with lock:\n if job.done:\n"
|
|
244
|
+
" RuntimeError('job ran twice') # expect\n job.start()\n",
|
|
245
|
+
note="A with block that expects nothing does not excuse the missing raise.",
|
|
246
|
+
),
|
|
247
|
+
],
|
|
248
|
+
"python/mutable-default": [
|
|
249
|
+
bad(
|
|
250
|
+
"src/cart.py",
|
|
251
|
+
"def add(item, basket=[]): # expect\n basket.append(item)\n return basket\n",
|
|
252
|
+
note="Every call without basket shares one list.",
|
|
253
|
+
),
|
|
254
|
+
bad(
|
|
255
|
+
"src/crawl.py",
|
|
256
|
+
"def crawl(url, *, seen=set()): # expect\n seen.add(url)\n",
|
|
257
|
+
),
|
|
258
|
+
bad(
|
|
259
|
+
"src/cache.py",
|
|
260
|
+
"from collections import defaultdict\n\n\n"
|
|
261
|
+
"def index(rows, groups=defaultdict(list)): # expect\n return groups\n",
|
|
262
|
+
),
|
|
263
|
+
good(
|
|
264
|
+
"src/cart.py",
|
|
265
|
+
"def add(item, basket=None):\n if basket is None:\n basket = []\n"
|
|
266
|
+
" basket.append(item)\n return basket\n",
|
|
267
|
+
),
|
|
268
|
+
good(
|
|
269
|
+
"src/models.py",
|
|
270
|
+
"from dataclasses import dataclass, field\n\n\n@dataclass\nclass Cart:\n"
|
|
271
|
+
" items: list[str] = field(default_factory=list)\n\n\n"
|
|
272
|
+
"def tags(names=(), limit=10):\n return names[:limit]\n",
|
|
273
|
+
note="A default factory and an immutable tuple default.",
|
|
274
|
+
),
|
|
275
|
+
],
|
|
276
|
+
"python/bare-except": [
|
|
277
|
+
bad(
|
|
278
|
+
"src/loader.py",
|
|
279
|
+
"def load(path):\n try:\n return open(path).read()\n"
|
|
280
|
+
" except: # expect\n return None\n",
|
|
281
|
+
),
|
|
282
|
+
bad(
|
|
283
|
+
"src/notify.py",
|
|
284
|
+
"def notify(client, msg):\n try:\n client.send(msg)\n"
|
|
285
|
+
" except: # expect\n log.warning('send failed')\n",
|
|
286
|
+
old="def notify(client, msg):\n try:\n client.send(msg)\n"
|
|
287
|
+
" except ConnectionError:\n log.warning('send failed')\n",
|
|
288
|
+
note="Widening a handler to a bare except.",
|
|
289
|
+
),
|
|
290
|
+
good(
|
|
291
|
+
"src/tx.py",
|
|
292
|
+
"def commit(tx):\n try:\n tx.commit()\n except:\n"
|
|
293
|
+
" tx.rollback()\n raise\n",
|
|
294
|
+
note="Cleanup then a bare raise is the sound use.",
|
|
295
|
+
),
|
|
296
|
+
good(
|
|
297
|
+
"src/loader.py",
|
|
298
|
+
"def load(path):\n try:\n return open(path).read()\n"
|
|
299
|
+
" except OSError:\n return None\n",
|
|
300
|
+
),
|
|
301
|
+
],
|
|
302
|
+
"python/swallowed-exception": [
|
|
303
|
+
bad(
|
|
304
|
+
"src/metrics.py",
|
|
305
|
+
"def record(client, value):\n try:\n client.send(value)\n"
|
|
306
|
+
" except Exception: # expect\n pass\n",
|
|
307
|
+
),
|
|
308
|
+
bad(
|
|
309
|
+
"src/importer.py",
|
|
310
|
+
"def rows(lines):\n for line in lines:\n try:\n"
|
|
311
|
+
" yield parse(line)\n"
|
|
312
|
+
" except (ValueError, Exception): # expect\n continue\n",
|
|
313
|
+
note="A tuple containing Exception is just as broad.",
|
|
314
|
+
),
|
|
315
|
+
good(
|
|
316
|
+
"src/compat.py",
|
|
317
|
+
"try:\n import ujson as json\nexcept ImportError:\n pass\n\n\n"
|
|
318
|
+
"def get(d, key):\n try:\n return d[key]\n except KeyError:\n"
|
|
319
|
+
" pass\n",
|
|
320
|
+
note="Naming the expected exception is deliberate.",
|
|
321
|
+
),
|
|
322
|
+
good(
|
|
323
|
+
"src/metrics.py",
|
|
324
|
+
"def record(client, value):\n try:\n client.send(value)\n"
|
|
325
|
+
" except Exception:\n log.exception('metric dropped')\n",
|
|
326
|
+
note="Logged, so it leaves a trace.",
|
|
327
|
+
),
|
|
328
|
+
],
|
|
329
|
+
"python/unreachable-code": [
|
|
330
|
+
bad(
|
|
331
|
+
"src/report.py",
|
|
332
|
+
"def total(rows):\n return sum(rows) # expect\n print('total computed')\n",
|
|
333
|
+
note="In an added file the hit sits at the return that makes the code dead.",
|
|
334
|
+
),
|
|
335
|
+
bad(
|
|
336
|
+
"src/report.py",
|
|
337
|
+
"def total(rows):\n result = sum(rows)\n return result\n"
|
|
338
|
+
" log.info('total %s', result) # expect\n",
|
|
339
|
+
old="def total(rows):\n result = sum(rows)\n return result\n",
|
|
340
|
+
note="Code added after an existing return.",
|
|
341
|
+
),
|
|
342
|
+
good(
|
|
343
|
+
"src/streams.py",
|
|
344
|
+
"def nothing():\n return\n yield\n\n\ndef abstract():\n"
|
|
345
|
+
" raise NotImplementedError\n yield\n",
|
|
346
|
+
note="The empty-generator idiom.",
|
|
347
|
+
),
|
|
348
|
+
good(
|
|
349
|
+
"src/report.py",
|
|
350
|
+
"def total(rows):\n if not rows:\n return 0\n return sum(rows)\n",
|
|
351
|
+
),
|
|
352
|
+
],
|
|
353
|
+
"python/redefined-function": [
|
|
354
|
+
bad(
|
|
355
|
+
"tests/test_cart.py",
|
|
356
|
+
"def test_total():\n assert total([1]) == 1\n\n\n"
|
|
357
|
+
"def test_total(): # expect\n assert total([]) == 0\n",
|
|
358
|
+
note="The first test is replaced and never runs.",
|
|
359
|
+
),
|
|
360
|
+
bad(
|
|
361
|
+
"src/handlers.py",
|
|
362
|
+
"def handle(event):\n return event.id\n\n\nVERSION = 2\n\n\n"
|
|
363
|
+
"def handle(event): # expect\n return event.key\n",
|
|
364
|
+
),
|
|
365
|
+
good(
|
|
366
|
+
"src/shapes.py",
|
|
367
|
+
"class Box:\n @property\n def size(self):\n return self._size\n\n"
|
|
368
|
+
" @size.setter\n def size(self, value):\n self._size = value\n",
|
|
369
|
+
note="A property setter reuses the name on purpose.",
|
|
370
|
+
),
|
|
371
|
+
good(
|
|
372
|
+
"src/parse.py",
|
|
373
|
+
"from typing import overload\n\n\n@overload\ndef parse(x: int) -> int: ...\n"
|
|
374
|
+
"@overload\ndef parse(x: str) -> str: ...\ndef parse(x):\n return x\n\n\n"
|
|
375
|
+
"try:\n from fast import speedup\nexcept ImportError:\n"
|
|
376
|
+
" def speedup(x):\n return x\n",
|
|
377
|
+
note="Overload stubs, and a fallback in another block.",
|
|
378
|
+
),
|
|
379
|
+
],
|
|
380
|
+
"python/star-import": [
|
|
381
|
+
bad("src/paths.py", "from os.path import * # expect\n\nROOT = join('a', 'b')\n"),
|
|
382
|
+
bad(
|
|
383
|
+
"src/app/views.py",
|
|
384
|
+
"from .models import * # expect\n\n\ndef index():\n return User\n",
|
|
385
|
+
note="Relative star imports outside __init__.py count.",
|
|
386
|
+
),
|
|
387
|
+
good(
|
|
388
|
+
"src/app/__init__.py",
|
|
389
|
+
"from .models import *\nfrom .views import *\n",
|
|
390
|
+
note="Re-exports from a package's __init__.py.",
|
|
391
|
+
),
|
|
392
|
+
good("src/paths.py", "from os.path import join\n\nROOT = join('a', 'b')\n"),
|
|
393
|
+
],
|
|
394
|
+
"python/requests-without-timeout": [
|
|
395
|
+
bad(
|
|
396
|
+
"src/client.py",
|
|
397
|
+
"import requests\n\n\ndef fetch(url):\n"
|
|
398
|
+
" return requests.get(url).json() # expect\n",
|
|
399
|
+
),
|
|
400
|
+
bad(
|
|
401
|
+
"src/upload.py",
|
|
402
|
+
"import requests\n\n\ndef upload(url, data):\n"
|
|
403
|
+
" with requests.Session() as session:\n"
|
|
404
|
+
" session.post(url, data=data, timeout=None) # expect\n",
|
|
405
|
+
note="A session call with timeout=None waits forever too.",
|
|
406
|
+
),
|
|
407
|
+
bad(
|
|
408
|
+
"src/feeds.py",
|
|
409
|
+
"import httpx\n\n\ndef feed(url):\n client = httpx.Client(timeout=None)\n"
|
|
410
|
+
" return client.get(url) # expect\n",
|
|
411
|
+
note="An httpx client whose default timeout was turned off.",
|
|
412
|
+
),
|
|
413
|
+
good(
|
|
414
|
+
"src/client.py",
|
|
415
|
+
"import requests\n\nTIMEOUT = (3, 10)\n\n\ndef fetch(url, **options):\n"
|
|
416
|
+
" requests.post(url, **options)\n"
|
|
417
|
+
" return requests.get(url, timeout=TIMEOUT).json()\n",
|
|
418
|
+
note="An explicit timeout, and **options that may carry one.",
|
|
419
|
+
),
|
|
420
|
+
good(
|
|
421
|
+
"src/feeds.py",
|
|
422
|
+
"import httpx\n\n\ndef feed(url):\n with httpx.Client(timeout=10) as client:\n"
|
|
423
|
+
" return client.get(url)\n\n\ndef ping(url):\n return httpx.head(url)\n",
|
|
424
|
+
note="httpx has a 5 s default timeout; a configured client counts as set.",
|
|
425
|
+
),
|
|
426
|
+
],
|
|
427
|
+
"python/signature-break": [
|
|
428
|
+
bad(
|
|
429
|
+
"src/billing.py",
|
|
430
|
+
"def charge(amount, customer): # expect\n return customer.pay(amount)\n",
|
|
431
|
+
old="def charge(customer, amount):\n return customer.pay(amount)\n",
|
|
432
|
+
note="Swapped required parameters break positional callers.",
|
|
433
|
+
),
|
|
434
|
+
bad(
|
|
435
|
+
"src/mailer.py",
|
|
436
|
+
"class Mailer:\n def send(self, to, body, priority): # expect\n"
|
|
437
|
+
" return (to, body, priority)\n",
|
|
438
|
+
old="class Mailer:\n def send(self, to, body):\n return (to, body)\n",
|
|
439
|
+
note="A new required parameter breaks every existing call.",
|
|
440
|
+
),
|
|
441
|
+
bad(
|
|
442
|
+
"src/users.py",
|
|
443
|
+
"def rename(user, *, new_name): # expect\n user.name = new_name\n",
|
|
444
|
+
old="def rename(user, *, name):\n user.name = name\n",
|
|
445
|
+
note="A renamed required keyword parameter.",
|
|
446
|
+
),
|
|
447
|
+
good(
|
|
448
|
+
"src/mailer.py",
|
|
449
|
+
"class Mailer:\n def send(self, to, body, priority=0):\n"
|
|
450
|
+
" return (to, body, priority)\n",
|
|
451
|
+
old="class Mailer:\n def send(self, to, body):\n return (to, body)\n",
|
|
452
|
+
note="A new optional parameter keeps old calls working.",
|
|
453
|
+
),
|
|
454
|
+
good(
|
|
455
|
+
"src/billing.py",
|
|
456
|
+
"def _charge(amount, customer):\n return customer.pay(amount)\n",
|
|
457
|
+
old="def _charge(customer, amount):\n return customer.pay(amount)\n",
|
|
458
|
+
note="Private functions may change freely.",
|
|
459
|
+
),
|
|
460
|
+
good(
|
|
461
|
+
"tests/test_billing.py",
|
|
462
|
+
"def make_customer(name, balance):\n return (name, balance)\n",
|
|
463
|
+
old="def make_customer(name):\n return (name, 0)\n",
|
|
464
|
+
note="Test helpers are not public API.",
|
|
465
|
+
),
|
|
466
|
+
],
|
|
467
|
+
"python/loop-closure": [
|
|
468
|
+
bad(
|
|
469
|
+
"src/menu.py",
|
|
470
|
+
"def build(menu, names):\n for name in names:\n"
|
|
471
|
+
" menu.append(lambda: menu.open(name)) # expect\n",
|
|
472
|
+
note="Every callback opens the last name.",
|
|
473
|
+
),
|
|
474
|
+
bad(
|
|
475
|
+
"src/scale.py",
|
|
476
|
+
"SCALERS = [lambda x: x * factor for factor in range(3)] # expect\n",
|
|
477
|
+
),
|
|
478
|
+
bad(
|
|
479
|
+
"src/jobs.py",
|
|
480
|
+
"import threading\n\n\ndef start(jobs):\n for job in jobs:\n"
|
|
481
|
+
" def work(): # expect\n job.run()\n"
|
|
482
|
+
" threading.Thread(target=work).start()\n",
|
|
483
|
+
note="The thread may run after the loop moved on.",
|
|
484
|
+
),
|
|
485
|
+
good(
|
|
486
|
+
"src/menu.py",
|
|
487
|
+
"def build(menu, names):\n for name in names:\n"
|
|
488
|
+
" menu.append(lambda name=name: menu.open(name))\n",
|
|
489
|
+
note="Binding the value as a default fixes it.",
|
|
490
|
+
),
|
|
491
|
+
good(
|
|
492
|
+
"src/table.py",
|
|
493
|
+
"def sort_columns(rows, columns):\n for col in columns:\n"
|
|
494
|
+
" rows = sorted(rows, key=lambda row: row[col])\n return rows\n",
|
|
495
|
+
note="The lambda is used at once, inside the iteration.",
|
|
496
|
+
),
|
|
497
|
+
],
|
|
498
|
+
"python/raise-without-from": [
|
|
499
|
+
bad(
|
|
500
|
+
"src/config.py",
|
|
501
|
+
"def port(env):\n try:\n return int(env['PORT'])\n except KeyError:\n"
|
|
502
|
+
" raise ConfigError('PORT is not set') # expect\n",
|
|
503
|
+
),
|
|
504
|
+
bad(
|
|
505
|
+
"src/convert.py",
|
|
506
|
+
"def to_int(text):\n try:\n return int(text)\n except ValueError:\n"
|
|
507
|
+
" raise TypeError # expect\n",
|
|
508
|
+
),
|
|
509
|
+
good(
|
|
510
|
+
"src/config.py",
|
|
511
|
+
"def port(env):\n try:\n return int(env['PORT'])\n except KeyError as exc:\n"
|
|
512
|
+
" raise ConfigError('PORT is not set') from exc\n",
|
|
513
|
+
),
|
|
514
|
+
good(
|
|
515
|
+
"src/main.py",
|
|
516
|
+
"def main(run):\n try:\n run()\n except KeyboardInterrupt:\n"
|
|
517
|
+
" raise SystemExit(130)\n except OSError as exc:\n log(exc)\n"
|
|
518
|
+
" raise\n",
|
|
519
|
+
note="An exit and a bare re-raise.",
|
|
520
|
+
),
|
|
521
|
+
],
|
|
522
|
+
"python/eq-none": [
|
|
523
|
+
bad("src/users.py", "def anonymous(user):\n return user.id == None # expect\n"),
|
|
524
|
+
bad(
|
|
525
|
+
"src/orders.py",
|
|
526
|
+
"def shipped(order):\n if order.shipped_at != None: # expect\n return True\n"
|
|
527
|
+
" return False\n",
|
|
528
|
+
),
|
|
529
|
+
good("src/users.py", "def anonymous(user):\n return user.id is None\n"),
|
|
530
|
+
good(
|
|
531
|
+
"src/queries.py",
|
|
532
|
+
"def active(session, User):\n"
|
|
533
|
+
" return session.query(User).filter(User.deleted_at == None).all()\n",
|
|
534
|
+
note="SQLAlchemy filters need == None to build IS NULL.",
|
|
535
|
+
),
|
|
536
|
+
],
|
|
537
|
+
"python/utcnow": [
|
|
538
|
+
bad(
|
|
539
|
+
"src/audit.py",
|
|
540
|
+
"from datetime import datetime\n\n\ndef stamp():\n"
|
|
541
|
+
" return datetime.utcnow() # expect\n",
|
|
542
|
+
),
|
|
543
|
+
bad(
|
|
544
|
+
"src/events.py",
|
|
545
|
+
"import datetime\n\n\ndef when(ts):\n"
|
|
546
|
+
" return datetime.datetime.utcfromtimestamp(ts) # expect\n",
|
|
547
|
+
),
|
|
548
|
+
good(
|
|
549
|
+
"src/audit.py",
|
|
550
|
+
"from datetime import datetime, timezone\n\n\ndef stamp():\n"
|
|
551
|
+
" return datetime.now(timezone.utc)\n",
|
|
552
|
+
),
|
|
553
|
+
good(
|
|
554
|
+
"src/clock.py",
|
|
555
|
+
"import arrow\n\n\ndef stamp():\n return arrow.utcnow()\n",
|
|
556
|
+
note="arrow returns an aware time.",
|
|
557
|
+
),
|
|
558
|
+
],
|
|
559
|
+
"python/print-call": [
|
|
560
|
+
bad(
|
|
561
|
+
"src/app/service.py",
|
|
562
|
+
"def charge(order):\n print('charging', order.id) # expect\n"
|
|
563
|
+
" return order.total\n",
|
|
564
|
+
note="Opt-in: enable it in config.",
|
|
565
|
+
),
|
|
566
|
+
bad(
|
|
567
|
+
"src/app/cache.py",
|
|
568
|
+
"def get(cache, key):\n value = cache.get(key)\n"
|
|
569
|
+
" print(value) # expect\n return value\n",
|
|
570
|
+
old="def get(cache, key):\n value = cache.get(key)\n return value\n",
|
|
571
|
+
),
|
|
572
|
+
good(
|
|
573
|
+
"scripts/release.py",
|
|
574
|
+
"def main():\n print('released')\n",
|
|
575
|
+
note="Scripts print by design.",
|
|
576
|
+
),
|
|
577
|
+
good(
|
|
578
|
+
"src/app/report.py",
|
|
579
|
+
"import argparse\nimport sys\n\n\ndef warn(msg):\n print(msg, file=sys.stderr)\n\n\n"
|
|
580
|
+
"def main():\n parser = argparse.ArgumentParser()\n parser.parse_args()\n"
|
|
581
|
+
" print('ok')\n",
|
|
582
|
+
note="A module that imports argparse is a command-line module.",
|
|
583
|
+
),
|
|
584
|
+
Example(
|
|
585
|
+
kind="good",
|
|
586
|
+
files={
|
|
587
|
+
"src/app/__main__.py": Change(new="print('hello')\n"),
|
|
588
|
+
"src/app/tools/fmt.py": Change(
|
|
589
|
+
new="import sys\n\n\ndef warn(msg):\n print(msg, file=sys.stderr)\n"
|
|
590
|
+
),
|
|
591
|
+
},
|
|
592
|
+
note="__main__.py, and print with an explicit stream.",
|
|
593
|
+
),
|
|
594
|
+
],
|
|
595
|
+
}
|