Skip to content

Commit c033dbd

Browse files
committed
Fix stubgen for non-top-level C modules
1 parent 241e076 commit c033dbd

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

mypy/stubgen.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import glob
3030
import imp
31+
import importlib
3132
import os.path
3233
import sys
3334

@@ -50,10 +51,9 @@ def generate_stub(path, output_dir, _all_=None, target=None, add_header=False, m
5051
ast.accept(gen)
5152
if not target:
5253
target = os.path.join(output_dir, os.path.basename(path))
53-
for i in range(target.count('/')):
54-
subdir = os.path.dirname(target)
55-
if subdir and not os.path.isdir(subdir):
56-
os.makedirs(subdir)
54+
subdir = os.path.dirname(target)
55+
if subdir and not os.path.isdir(subdir):
56+
os.makedirs(subdir)
5757
with open(target, 'w') as file:
5858
if add_header:
5959
write_header(file, module)
@@ -62,26 +62,22 @@ def generate_stub(path, output_dir, _all_=None, target=None, add_header=False, m
6262

6363
def generate_stub_for_module(module, output_dir, quiet=False, add_header=False, sigs={},
6464
class_sigs={}):
65-
mod = __import__(module)
65+
mod = importlib.import_module(module)
6666
imp.reload(mod)
67-
components = module.split('.')
68-
for attr in components[1:]:
69-
mod = getattr(mod, attr)
7067
if is_c_module(mod):
71-
target = '/'.join(components[:-1] + [components[-1] + '.pyi'])
68+
target = module.replace('.', '/') + '.pyi'
7269
target = os.path.join(output_dir, target)
7370
generate_stub_for_c_module(module_name=module,
7471
target=target,
7572
add_header=add_header,
7673
sigs=sigs,
7774
class_sigs=class_sigs)
7875
else:
79-
target = '/'.join(module.split('.')[:-1])
80-
modfnam = os.path.basename(mod.__file__)
81-
if modfnam == '__init__.py':
82-
target = os.path.join(target, module.split('.')[-1], '__init__.pyi')
76+
target = module.replace('.', '/')
77+
if os.path.basename(mod.__file__) == '__init__.py':
78+
target += '/__init__.pyi'
8379
else:
84-
target = os.path.join(target, modfnam.replace('.py', '.pyi'))
80+
target += '.pyi'
8581
target = os.path.join(output_dir, target)
8682
generate_stub(mod.__file__, output_dir, getattr(mod, '__all__', None),
8783
target=target, add_header=add_header, module=module)

mypy/stubgenc.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
The public interface is via the mypy.stubgen module.
44
"""
55

6+
import importlib
67
import os.path
78
import re
89

@@ -14,8 +15,11 @@
1415

1516

1617
def generate_stub_for_c_module(module_name, target, add_header=True, sigs={}, class_sigs={}):
17-
module = __import__(module_name)
18+
module = importlib.import_module(module_name)
1819
assert is_c_module(module), '%s is not a C module' % module_name
20+
subdir = os.path.dirname(target)
21+
if subdir and not os.path.isdir(subdir):
22+
os.makedirs(subdir)
1923
functions = []
2024
done = set()
2125
items = sorted(module.__dict__.items(), key=lambda x: x[0])

0 commit comments

Comments
 (0)