-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathissues.py
More file actions
456 lines (407 loc) · 13.7 KB
/
issues.py
File metadata and controls
456 lines (407 loc) · 13.7 KB
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
from __future__ import annotations
import asyncio
import logging
import re
import subprocess
import sys
from collections.abc import AsyncIterable, AsyncIterator, Awaitable, Callable
from datetime import UTC, datetime
from typing import Any, Final, NamedTuple, ParamSpec, Protocol, TypeVar
from githubkit import GitHub
from githubkit.exception import GitHubException
from githubkit.utils import UNSET
from githubkit.versions.latest.models import (
Commit,
GistSimple,
GistSimplePropFiles,
Issue,
IssueComment,
PullRequest,
)
from markdown_it import MarkdownIt
from .config import (
INVENTORY_FILE,
ISSUES_FILE,
SNIPPETS_ROOT,
InventoryItem,
IssueWithComments,
load_inventory,
load_issues,
save_inventory,
save_issues,
)
LOG = logging.getLogger("issues")
PAGE_SIZE: Final = 100
RETRIES: Final = 5
P = ParamSpec("P")
T = TypeVar("T")
T_co = TypeVar("T_co", covariant=True)
async def download_snippets( # noqa: C901
token: str,
*,
limit: int | None = None,
org: str = "python",
repo: str = "mypy",
since: datetime | None = None,
) -> None:
SNIPPETS_ROOT.mkdir(exist_ok=True)
# Github API will sometimes hang for a long time to fail with timeout.
# Set smaller timeout and retry.
gh = GitHub(token, timeout=2)
inventory: list[InventoryItem] = []
issues: dict[int, IssueWithComments] = {}
seen: set[str] = set()
last_known_issues: set[int] = set()
removed_count = 0
if INVENTORY_FILE.is_file() and ISSUES_FILE.is_file():
inventory = load_inventory()
issues = load_issues()
removed = set()
if since is None:
since = max(
(iss.issue.created_at for iss in issues.values()),
default=datetime.fromtimestamp(0, tz=UTC),
)
else:
removed.update({
iss.issue.number
for iss in issues.values()
if iss.issue.created_at > since
})
removed.update({
iss.number async for iss in _get_closed_issues(gh, org, repo, since)
})
removed_count = sum(n in issues for n in removed)
comments = await _get_comments(gh, org, repo, since)
# Just sync issues with new comments from scratch, that should be cheap.
removed.update(comments.keys())
last_known_issues = set(issues.keys())
inventory, issues = _incremental_update(inventory, issues, removed)
seen = {snip["filename"] for snip in inventory}
event = asyncio.Event()
new_issues: set[int] = set()
async for batch in abatch(_get_issues(gh, event, org, repo, since), size=64):
blocks = await asyncio.gather(*[
extract_snippets(plain_issue, gh=gh, event=event) for plain_issue in batch
])
for block in blocks:
for snip, issue in block:
if snip.filename in seen:
continue
if store_snippet(snip):
inventory.append({
"filename": snip.filename,
"issue": issue.issue.number,
"mypy_version": snip.mypy_version,
"created_at": int(snip.date.timestamp()),
})
if len(inventory) == limit:
event.set()
break
issues[issue.issue.number] = issue
new_issues.add(issue.issue.number)
if len(inventory) == limit:
break
if len(inventory) == limit:
break
LOG.info(
"Added %d issues, removed %d issues, refreshed %d issues.",
len(new_issues - last_known_issues),
removed_count,
len(new_issues & last_known_issues),
)
LOG.info("Now %d snippets in %s.", len(inventory), SNIPPETS_ROOT)
save_inventory(inventory)
save_issues(issues)
def _incremental_update(
inventory: list[InventoryItem],
issues: dict[int, IssueWithComments],
removed: set[int],
) -> tuple[list[InventoryItem], dict[int, IssueWithComments]]:
inventory = [snip for snip in inventory if snip["issue"] not in removed]
issues = {n: iss for n, iss in issues.items() if n not in removed}
for n in removed:
for file in SNIPPETS_ROOT.glob(f"gh_{n}_*.py"):
file.unlink()
return inventory, issues
class Snippet(NamedTuple):
issue: int
comment: int | None
date: datetime
id: int
body: str
mypy_version: str | None
@property
def filename(self) -> str:
if self.comment is None:
return f"gh_{self.issue}_body_{self.id}.py"
return f"gh_{self.issue}_c{self.comment}_{self.id}.py"
async def _get_issues(
gh: GitHub[Any],
event: asyncio.Event,
org: str,
repo: str,
since: datetime | None = None,
) -> AsyncIterator[Issue]:
page = 1
issues: list[Issue] = []
while not event.is_set():
issues = await retry(
gh.rest.issues.async_list_for_repo,
org,
repo,
state="open",
sort="created",
direction="asc",
since=since or UNSET,
per_page=PAGE_SIZE,
page=page,
)
for iss in issues:
if iss.pull_request is UNSET:
yield iss
if len(issues) < PAGE_SIZE:
break
page += 1
async def _get_closed_issues(
gh: GitHub[Any], org: str, repo: str, since: datetime
) -> AsyncIterator[Issue]:
page = 1
issues: list[Issue] = []
while True:
issues = await retry(
gh.rest.issues.async_list_for_repo,
org,
repo,
state="closed",
sort="created",
direction="asc",
since=since,
per_page=PAGE_SIZE,
page=page,
)
for iss in issues:
if iss.pull_request is UNSET:
yield iss
if len(issues) < PAGE_SIZE:
break
page += 1
async def _get_comments(
gh: GitHub[Any], org: str, repo: str, since: datetime
) -> dict[int, list[IssueComment]]:
result: dict[int, list[IssueComment]] = {}
page = 1
comments: list[IssueComment] = []
while True:
comments = await retry(
gh.rest.issues.async_list_comments_for_repo,
org,
repo,
sort="created",
direction="asc",
since=since,
per_page=PAGE_SIZE,
page=page,
)
for comm in comments:
_, issue_no = comm.issue_url.rsplit("/", 1)
result.setdefault(int(issue_no), []).append(comm)
if len(comments) < PAGE_SIZE:
break
page += 1
return result
async def _get_comments_for_issue(
issue: Issue, gh: GitHub[Any]
) -> AsyncIterator[IssueComment]:
_, org, repo = issue.repository_url.rsplit("/", 2)
page = 1
comments: list[IssueComment] = []
while True:
comments = await retry(
gh.rest.issues.async_list_comments,
org,
repo,
issue.number,
per_page=PAGE_SIZE,
page=page,
)
for com in comments:
yield com
if len(comments) < PAGE_SIZE:
break
page += 1
async def _get_gist(gist: str, gh: GitHub[Any]) -> GistSimple:
return await retry(gh.rest.gists.async_get, gist)
def get_pr(
gh_token: str, pr_number: int, *, org: str = "python", repo: str = "mypy"
) -> PullRequest:
gh = GitHub(gh_token)
return gh.rest.pulls.get(org, repo, pr_number).parsed_data
def get_commit(
gh_token: str, commit_sha: str, *, org: str = "python", repo: str = "mypy"
) -> Commit:
gh = GitHub(gh_token)
return gh.rest.repos.get_commit(org, repo, commit_sha).parsed_data
async def extract_snippets(
issue: Issue, gh: GitHub[Any], event: asyncio.Event
) -> list[tuple[Snippet, IssueWithComments]]:
if event.is_set():
return []
result: list[Snippet] = []
version = _extract_mypy_version(issue.body) if issue.body else None
seen_snippets = {""} # Ignore empty
if issue.body:
result += [
Snippet(
issue=issue.number,
comment=None,
date=issue.created_at,
id=i,
body=snip,
mypy_version=version,
)
for i, snip in enumerate(
await _extract_snippets(issue.body, gh, seen_snippets)
)
]
comments = []
if issue.comments > 0 and not event.is_set():
comments = [com async for com in _get_comments_for_issue(issue, gh) if com.body]
snippets = await asyncio.gather(*[
_extract_snippets(com.body or "", gh, seen_snippets) for com in comments
])
result += [
Snippet(
issue=issue.number,
comment=com.id,
date=issue.created_at,
id=i,
body=snip,
mypy_version=version,
)
for com, snips in zip(comments, snippets, strict=True)
for i, snip in enumerate(snips)
]
return [(b, IssueWithComments(issue, comments)) for b in result]
async def _extract_snippets(
text: str, gh: GitHub[Any], seen_snippets: set[str]
) -> list[str]:
result: list[str] = []
md = MarkdownIt("gfm-like")
for token in md.parse(text):
if (
token.type == "fence"
and token.tag == "code"
and token.info in {"", "py", "pyi", "python", "python3"}
and _is_relevant(token.content)
):
norm_body = _normalize(token.content)
if norm_body in seen_snippets:
continue
seen_snippets.add(norm_body)
result.append(token.content)
gist_ids = [
*re.findall(r"https://mypy-play\.net/\?.+?gist=([\da-f]{32})", text),
*re.findall(r"https://gist\.github\.com/[\w-]+/([\da-f]{32})", text),
]
if not gist_ids:
return result
gists = await asyncio.gather(*[_get_gist(gist_id, gh) for gist_id in gist_ids])
for gist in gists:
assert isinstance(gist.files, GistSimplePropFiles)
files = list(gist.files.model_dump().values())
if len(files) != 1:
continue
(file,) = files
norm_body = _normalize(file["content"])
if norm_body in seen_snippets:
continue
seen_snippets.add(norm_body)
if _is_relevant(token.content):
result.append(file["content"])
return result
def _extract_mypy_version(body: str) -> str | None:
if m := re.search(r"Mypy versions? used:.*?(\d+\.\d+(\.\d+)?)", body):
return m.group(1)
if m := re.search(r"[mM]ypy\s*==?\s*?(\d+\.\d+(\.\d+)?)", body):
return m.group(1)
if re.search(r"Mypy versions? used:.*?master", body):
return "master"
return None
def _is_relevant(code: str) -> bool:
return (
not code.startswith("$")
and not re.search(r"^[\w-]+\.py:\d+: (error|warning|note):", code)
and not re.search(r"^\[(tool\.)?mypy\]", code)
)
def _normalize(snippet: str) -> str:
# strip comments (yes, this is invalid, should parse ast, but this is fast)
# we don't care if something goes wrong, we check orig snippets - this is only
# needed to reduce duplicates
snippet = re.sub(r"#.+\n", "\n", snippet)
# Strip trailing whitespace
snippet = re.sub(r"\s+\n", "\n", snippet)
return re.sub(r"\n+", "\n", snippet.replace("\r", "")).strip()
def store_snippet(snip: Snippet) -> bool:
dest = SNIPPETS_ROOT / snip.filename
dest.write_text(snip.body)
try:
# fmt: off
subprocess.check_output(
[
sys.executable, "-m", "ruff",
"check",
str(dest.resolve()),
"--select", "F821",
"--output-format", "concise",
"--target-version", "py313",
],
text=True,
stderr=subprocess.STDOUT,
env={"NO_COLOR": "1"},
)
# fmt: on
except subprocess.CalledProcessError as exc:
if "SyntaxError" in exc.stdout:
LOG.debug("Rejecting snippet %s: syntax error", dest.name)
dest.unlink()
return False
has_undef_names = any(
"F821" in line
for line in exc.stdout.splitlines()
if "reveal_type" not in line
)
if has_undef_names:
# Try to recover
import_typing = "from typing import * # Added by us"
lines = snip.body.splitlines()
for i, line in enumerate(lines):
if not line.startswith("#") and "__future__" not in line:
lines.insert(i, import_typing)
break
else:
lines.append(import_typing)
dest.write_text("\n".join(lines))
LOG.debug("Added snippet %s.", dest.name)
return True
async def abatch(aiterable: AsyncIterable[T], size: int) -> AsyncIterable[list[T]]:
batch = []
async for element in aiterable:
batch.append(element)
if len(batch) == size:
yield batch
batch = []
if batch:
yield batch
class _WithParsedData(Protocol[T_co]):
@property
def parsed_data(self) -> T_co: ...
async def retry(
fn: Callable[P, Awaitable[_WithParsedData[T]]], *args: P.args, **kwargs: P.kwargs
) -> T:
for i in range(RETRIES):
try:
return (await fn(*args, **kwargs)).parsed_data
except GitHubException:
LOG.warning("Request failed, retrying %d more times...", RETRIES - 1 - i)
raise RuntimeError(f"Request failed {RETRIES} times, aborting.")