Skip to content

Commit 74d66da

Browse files
ilevkivskyiddfisher
authored andcommitted
Allow using --fast-parser in stubgen (#2277)
Allow using fast parser in stubgen, as proposed on Gitter (mainly to generate stubs for modules that use ``async/await`` syntax).
1 parent 3b92e79 commit 74d66da

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

mypy/stubgen.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@
7070
('modules', List[str]),
7171
('ignore_errors', bool),
7272
('recursive', bool),
73+
('fast_parser', bool),
7374
])
7475

7576

7677
def generate_stub_for_module(module: str, output_dir: str, quiet: bool = False,
7778
add_header: bool = False, sigs: Dict[str, str] = {},
7879
class_sigs: Dict[str, str] = {},
7980
pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION,
81+
fast_parser: bool = False,
8082
no_import: bool = False,
8183
search_path: List[str] = [],
8284
interpreter: str = sys.executable) -> None:
@@ -103,7 +105,8 @@ def generate_stub_for_module(module: str, output_dir: str, quiet: bool = False,
103105
target += '.pyi'
104106
target = os.path.join(output_dir, target)
105107
generate_stub(module_path, output_dir, module_all,
106-
target=target, add_header=add_header, module=module, pyversion=pyversion)
108+
target=target, add_header=add_header, module=module,
109+
pyversion=pyversion, fast_parser=fast_parser)
107110
if not quiet:
108111
print('Created %s' % target)
109112

@@ -168,10 +171,12 @@ def load_python_module_info(module: str, interpreter: str) -> Tuple[str, Optiona
168171

169172
def generate_stub(path: str, output_dir: str, _all_: Optional[List[str]] = None,
170173
target: str = None, add_header: bool = False, module: str = None,
171-
pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION) -> None:
174+
pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION,
175+
fast_parser: bool = False) -> None:
172176
source = open(path, 'rb').read()
173177
options = MypyOptions()
174178
options.python_version = pyversion
179+
options.fast_parser = fast_parser
175180
try:
176181
ast = mypy.parse.parse(source, fnam=path, errors=None, options=options)
177182
except mypy.errors.CompileError as e:
@@ -612,6 +617,7 @@ def main() -> None:
612617
sigs=sigs,
613618
class_sigs=class_sigs,
614619
pyversion=options.pyversion,
620+
fast_parser=options.fast_parser,
615621
no_import=options.no_import,
616622
search_path=options.search_path,
617623
interpreter=options.interpreter)
@@ -631,6 +637,7 @@ def parse_options() -> Options:
631637
doc_dir = ''
632638
search_path = [] # type: List[str]
633639
interpreter = ''
640+
fast_parser = False
634641
while args and args[0].startswith('-'):
635642
if args[0] == '--doc-dir':
636643
doc_dir = args[1]
@@ -645,6 +652,8 @@ def parse_options() -> Options:
645652
args = args[1:]
646653
elif args[0] == '--recursive':
647654
recursive = True
655+
elif args[0] == '--fast-parser':
656+
fast_parser = True
648657
elif args[0] == '--ignore-errors':
649658
ignore_errors = True
650659
elif args[0] == '--py2':
@@ -667,7 +676,8 @@ def parse_options() -> Options:
667676
interpreter=interpreter,
668677
modules=args,
669678
ignore_errors=ignore_errors,
670-
recursive=recursive)
679+
recursive=recursive,
680+
fast_parser=fast_parser)
671681

672682

673683
def default_python2_interpreter() -> str:
@@ -695,6 +705,7 @@ def usage() -> None:
695705
Options:
696706
--py2 run in Python 2 mode (default: Python 3 mode)
697707
--recursive traverse listed modules to generate inner package modules as well
708+
--fast-parser enable experimental fast parser
698709
--ignore-errors ignore errors when trying to generate stubs for modules
699710
--no-import don't import the modules, just parse and analyze them
700711
(doesn't work with C extension modules and doesn't

mypy/test/teststubgen.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ def test_stubgen(testcase):
125125
try:
126126
if testcase.name.endswith('_import'):
127127
generate_stub_for_module(name, out_dir, quiet=True)
128+
elif testcase.name.endswith('_fast_parser'):
129+
generate_stub(path, out_dir, fast_parser=True)
128130
else:
129131
generate_stub(path, out_dir)
130132
a = load_output(out_dir)

test-data/unit/stubgen.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,12 @@ def syslog(a): pass
543543
[out]
544544
def syslog(a): ...
545545

546+
[case testAsyncAwait_fast_parser]
547+
async def f(a):
548+
x = await y
549+
[out]
550+
def f(a): ...
551+
546552
[case testInferOptionalOnlyFunc]
547553
class A:
548554
x = None

0 commit comments

Comments
 (0)