Skip to content

Stubgen fixes #717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import glob
import imp
import importlib
import os.path
import sys

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

def generate_stub_for_module(module, output_dir, quiet=False, add_header=False, sigs={},
class_sigs={}):
mod = __import__(module)
mod = importlib.import_module(module)
imp.reload(mod)
components = module.split('.')
for attr in components[1:]:
mod = getattr(mod, attr)
if is_c_module(mod):
target = '/'.join(components[:-1] + [components[-1] + '.pyi'])
target = module.replace('.', '/') + '.pyi'
target = os.path.join(output_dir, target)
generate_stub_for_c_module(module_name=module,
target=target,
add_header=add_header,
sigs=sigs,
class_sigs=class_sigs)
else:
target = '/'.join(module.split('.')[:-1])
modfnam = os.path.basename(mod.__file__)
if modfnam == '__init__.py':
target = os.path.join(target, module.split('.')[-1], '__init__.pyi')
target = module.replace('.', '/')
if os.path.basename(mod.__file__) == '__init__.py':
target += '/__init__.pyi'
else:
target = os.path.join(target, modfnam.replace('.py', '.pyi'))
target += '.pyi'
target = os.path.join(output_dir, target)
generate_stub(mod.__file__, output_dir, getattr(mod, '__all__', None),
target=target, add_header=add_header, module=module)
Expand Down
16 changes: 14 additions & 2 deletions mypy/stubgenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
The public interface is via the mypy.stubgen module.
"""

import importlib
import os.path
import re

Expand All @@ -14,8 +15,11 @@


def generate_stub_for_c_module(module_name, target, add_header=True, sigs={}, class_sigs={}):
module = __import__(module_name)
module = importlib.import_module(module_name)
assert is_c_module(module), '%s is not a C module' % module_name
subdir = os.path.dirname(target)
if subdir and not os.path.isdir(subdir):
os.makedirs(subdir)
functions = []
done = set()
items = sorted(module.__dict__.items(), key=lambda x: x[0])
Expand Down Expand Up @@ -105,7 +109,10 @@ def generate_c_function_stub(module, name, obj, output, self_var=None, sigs={},
else:
sig = sigs.get(name, '(*args, **kwargs)')
sig = sig[1:-1]
if not sig:
if sig:
if sig.split(',', 1)[0] == self_var:
self_arg = ''
else:
self_arg = self_arg.replace(', ', '')
output.append('def %s(%s%s): pass' % (name, self_arg, sig))

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