Skip to content

Commit 34ddbd6

Browse files
authored
Merge pull request #285 from asottile/more-mock
rewrite more mock attributes
2 parents 1ce025f + f46ca12 commit 34ddbd6

2 files changed

Lines changed: 50 additions & 23 deletions

File tree

reorder_python_imports.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def _fix_file(
619619
# END GENERATED
620620

621621
# GENERATED VIA generate-mock-info
622-
# Using mock==4.0.3
622+
# up until cpython 3.10.0
623623
REPLACES[(3,)].update((
624624
'mock.mock=unittest.mock:ANY',
625625
'mock.mock=unittest.mock:DEFAULT',
@@ -648,6 +648,14 @@ def _fix_file(
648648
'mock=unittest.mock:patch',
649649
'mock=unittest.mock:sentinel',
650650
))
651+
REPLACES[(3, 7)].update((
652+
'mock.mock=unittest.mock:seal',
653+
'mock=unittest.mock:seal',
654+
))
655+
REPLACES[(3, 8)].update((
656+
'mock.mock=unittest.mock:AsyncMock',
657+
'mock=unittest.mock:AsyncMock',
658+
))
651659
# END GENERATED
652660

653661
# GENERATED VIA generate-deprecated

testing/generate-mock-info

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,54 @@
11
#!/usr/bin/env python3
22
from __future__ import annotations
33

4+
import argparse
5+
import ast
46
import os
7+
import re
8+
import subprocess
59
import sys
6-
import unittest.mock
710

11+
ALL_RE = re.compile(r'__all__ = (\([^)]+\))')
812

9-
def main() -> int:
10-
assert sys.version_info[:2] == (3, 6), 'needs py36'
11-
12-
mock = __import__('mock') # avoid self-rewriting!
1313

14-
mock_all, mock_version = mock.__all__, mock.__version__
15-
unittest_mock_all = unittest.mock.__all__
14+
def _get_all(cpython: str, version: str) -> frozenset[str]:
15+
cmd = ('git', '-C', cpython, 'show', f'{version}:Lib/unittest/mock.py')
16+
out = subprocess.check_output(cmd, stderr=subprocess.DEVNULL).decode()
17+
match = ALL_RE.search(out)
18+
assert match is not None
19+
return frozenset(ast.literal_eval(match[1]))
1620

17-
attrs = set(mock_all).intersection(unittest_mock_all)
1821

19-
renames_s = '\n'.join(
20-
sorted(
21-
f" '{orig_mod}=unittest.mock:{attr}',"
22-
for orig_mod in ('mock', 'mock.mock')
23-
for attr in attrs
24-
),
25-
)
26-
27-
print(
28-
f'# GENERATED VIA {os.path.basename(sys.argv[0])}\n'
29-
f'# Using mock=={mock_version}\n'
30-
f'REPLACES[(3,)].update((\n{renames_s}\n))\n'
31-
f'# END GENERATED\n',
32-
)
22+
def main() -> int:
23+
parser = argparse.ArgumentParser()
24+
parser.add_argument('cpython')
25+
args = parser.parse_args()
26+
27+
prev = _get_all(args.cpython, 'v3.3.0')
28+
to_add: dict[tuple[int, ...], frozenset[str]] = {(3,): prev}
29+
30+
for minor in range(6, 999):
31+
try:
32+
new = _get_all(args.cpython, f'v3.{minor}.0')
33+
except subprocess.CalledProcessError:
34+
break
35+
36+
if new != prev:
37+
to_add[(3, minor)] = new - prev
38+
prev = new
39+
40+
print(f'# GENERATED VIA {os.path.basename(sys.argv[0])}')
41+
print(f'# up until cpython 3.{minor - 1}.0')
42+
for k, v in sorted(to_add.items()):
43+
print(f'REPLACES[{k}].update((')
44+
for s in sorted(
45+
f" '{orig_mod}=unittest.mock:{attr}',"
46+
for orig_mod in ('mock', 'mock.mock')
47+
for attr in v
48+
):
49+
print(s)
50+
print('))')
51+
print('# END GENERATED')
3352

3453
return 0
3554

0 commit comments

Comments
 (0)