Skip to content

Commit 1e43187

Browse files
committed
Merge pull request #717 from o11c/stubgen
Stubgen fixes
2 parents 62fe068 + 8132600 commit 1e43187

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
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: 14 additions & 2 deletions
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])
@@ -105,7 +109,10 @@ def generate_c_function_stub(module, name, obj, output, self_var=None, sigs={},
105109
else:
106110
sig = sigs.get(name, '(*args, **kwargs)')
107111
sig = sig[1:-1]
108-
if not sig:
112+
if sig:
113+
if sig.split(',', 1)[0] == self_var:
114+
self_arg = ''
115+
else:
109116
self_arg = self_arg.replace(', ', '')
110117
output.append('def %s(%s%s): ...' % (name, self_arg, sig))
111118

@@ -125,6 +132,11 @@ def generate_c_type_stub(module, class_name, obj, output, sigs={}, class_sigs={}
125132
self_var = 'self'
126133
if attr == '__new__':
127134
# TODO: We should support __new__.
135+
if '__init__' in obj.__dict__:
136+
# Avoid duplicate functions if both are present.
137+
# But is there any case where .__new__() has a
138+
# better signature than __init__() ?
139+
continue
128140
attr = '__init__'
129141
generate_c_function_stub(module, attr, value, methods, self_var, sigs=sigs,
130142
class_name=class_name, class_sigs=class_sigs)

0 commit comments

Comments
 (0)