Skip to content

Commit 2dd833c

Browse files
authored
Merge pull request #231 from unihd-cag/issue-230
Repair static completion after mypy behavior change
2 parents 36e5323 + b9c25a5 commit 2dd833c

1 file changed

Lines changed: 41 additions & 9 deletions

File tree

skillbridge/__init__.py

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
from keyword import iskeyword
12
from os import chdir
3+
from pathlib import Path
4+
from re import fullmatch, sub
5+
from sys import executable, version_info
26

37
from .client.functions import FunctionCollection, keys
48
from .client.globals import Globals, GlobalVar
@@ -38,19 +42,47 @@
3842

3943

4044
def generate_static_completion() -> None:
41-
from keyword import iskeyword
42-
from pathlib import Path
43-
from re import fullmatch
44-
from subprocess import run
45+
from mypy.stubgen import Options, generate_stubs
4546

46-
ident = r'[a-zA-Z_][a-zA-Z0-9_]*'
47+
base = Path(__file__).parent.absolute() / 'client'
48+
annotation = base / 'workspace.pyi'
49+
50+
try:
51+
annotation.unlink()
52+
except FileNotFoundError:
53+
pass
54+
55+
chdir(base)
56+
57+
o = Options(
58+
(version_info.major, version_info.minor),
59+
no_import=True,
60+
doc_dir='',
61+
search_path=[],
62+
interpreter=executable,
63+
parse_only=False,
64+
ignore_errors=False,
65+
include_private=False,
66+
output_dir='.',
67+
modules=['workspace'],
68+
packages=[],
69+
files=[],
70+
verbose=True,
71+
quiet=False,
72+
export_less=False,
73+
)
4774

48-
client = Path(__file__).parent.absolute() / 'client'
49-
chdir(client)
50-
run(['stubgen', 'workspace.py', '-o', '.'])
75+
generate_stubs(o)
76+
77+
ident = r'[a-zA-Z_][a-zA-Z0-9_]*'
5178

5279
ws = Workspace.open()
53-
with open('workspace.pyi', 'a') as fout:
80+
81+
text = annotation.read_text()
82+
text = sub(r' {4}[a-z][a-zA-Z]+: FunctionCollection\n', '', text)
83+
annotation.write_text(text)
84+
85+
with open(annotation, 'a') as fout:
5486
for key, value in ws.__dict__.items():
5587
if not isinstance(value, FunctionCollection):
5688
continue

0 commit comments

Comments
 (0)