def run_case(self, testcase: DataDrivenTestCase) -> None: extra = [] mods = [] source = '\n'.join(testcase.input) for file, content in testcase.files + [('./main.py', source)]: mod = os.path.basename(file)[:-3] mods.append(mod) extra.extend(['-m', mod]) with open(file, 'w') as f: f.write(content) options = self.parse_flags(source, extra) out_dir = 'out' try: try: if not testcase.name.endswith('_import'): options.no_import = True if not testcase.name.endswith('_semanal'): options.parse_only = True generate_stubs(options, quiet=True, add_header=False) a = [] # type: List[str] self.add_file(os.path.join(out_dir, 'main.pyi'), a) except CompileError as e: a = e.messages assert_string_arrays_equal(testcase.output, a, 'Invalid output ({}, line {})'.format( testcase.file, testcase.line)) finally: for mod in mods: if mod in sys.modules: del sys.modules[mod] shutil.rmtree(out_dir)
def run_case(self, testcase: DataDrivenTestCase) -> None: extra = [] mods = [] source = '\n'.join(testcase.input) for file, content in testcase.files + [('./main.py', source)]: mod = os.path.basename(file)[:-3] mods.append(mod) extra.extend(['-m', mod]) with open(file, 'w') as f: f.write(content) options = self.parse_flags(source, extra) out_dir = 'out' try: try: if not testcase.name.endswith('_import'): options.no_import = True if not testcase.name.endswith('_semanal'): options.parse_only = True generate_stubs(options, quiet=True, add_header=False) a = [] # type: List[str] self.add_file(os.path.join(out_dir, 'main.pyi'), a) except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid output ({}, line {})'.format(testcase.file, testcase.line)) finally: for mod in mods: if mod in sys.modules: del sys.modules[mod] shutil.rmtree(out_dir)
def call_stubgen(command_line_args: List[str]) -> None: """ Call stubgen like the command line tool. :param command_line_args: list of command line args """ generate_stubs(parse_options(command_line_args))
def stubgen_main(opts): mypy_util.check_python_version('stubgen') # Make sure that the current directory is in sys.path so that # stubgen can be run on packages in the current directory. if not ('' in sys.path or '.' in sys.path): sys.path.insert(0, '') options = stubgen.parse_options(opts) stubgen.generate_stubs(options)
def generate_stubs_from_sample(sample, python2=True, *args): full_sample_path = os.path.join('.', 'samples', sample) options = parse_options((['--py2'] if python2 else []) + list(args) + [full_sample_path]) generate_stubs(options) try: yield __import__(f'out.{sample.split(".py")[0]}', fromlist=['a']) finally: shutil.rmtree('out')
def run_case_inner(self, testcase: DataDrivenTestCase) -> None: extra = [] # Extra command-line args mods = [] # Module names to process source = '\n'.join(testcase.input) for file, content in testcase.files + [('./main.py', source)]: # Strip ./ prefix and .py suffix. mod = file[2:-3].replace('/', '.') if mod.endswith('.__init__'): mod, _, _ = mod.rpartition('.') mods.append(mod) if '-p ' not in source: extra.extend(['-m', mod]) with open(file, 'w') as f: f.write(content) options = self.parse_flags(source, extra) modules = self.parse_modules(source) out_dir = 'out' try: try: if not testcase.name.endswith('_import'): options.no_import = True if not testcase.name.endswith('_semanal'): options.parse_only = True generate_stubs(options) a = [] # type: List[str] for module in modules: fnam = module_to_path(out_dir, module) self.add_file(fnam, a, header=len(modules) > 1) except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid output ({}, line {})'.format(testcase.file, testcase.line)) finally: for mod in mods: if mod in sys.modules: del sys.modules[mod] shutil.rmtree(out_dir)
def clone_and_generate(clone_dir: Path, package_dir: Path, dst_dir: Path, branch: str): """ Clone source repo and generate stubs for package :param Path clone_dir: Where to clone source repo :param Path package_dir: Path to module that needs stubs generated :param Path dst_dir: Destination path for stubs :param str branch: Which branch to clone """ if clone_dir.exists(): shutil.rmtree(clone_dir) clone_dir.mkdir(exist_ok=True, parents=False) repo = Repo.clone_from( "https://github.com/kubernetes-client/python.git", clone_dir, progress=RemoteProgress(), branch=branch, depth=1, ) for submodule in repo.submodules: submodule.update(init=True) stubgen_options = parse_options([ f"{package_dir}", f"-o={dst_dir}", ]) generate_stubs(stubgen_options) for dirpath, _, filenames in os.walk(dst_dir): for fn in filenames: with open(os.path.join(dirpath, fn), "r") as original: data = original.read() with open(os.path.join(dirpath, fn), "w") as modified: modified.write( "# Code generated by `stubgen`. DO NOT EDIT.\n" + data)
def generate_pyi_from_file(file: Path) -> bool: """Generate a .pyi stubfile from a single .py module using mypy/stubgen""" sg_opt = stubgen.Options( pyversion=(3, 6), no_import=False, include_private=True, doc_dir="", search_path=[], interpreter=sys.executable, parse_only=False, ignore_errors=True, modules=[], packages=[], files=[], output_dir="", verbose=True, quiet=False, export_less=False, ) # Deal with generator passed in if not isinstance(file, Path): raise TypeError sg_opt.files = [str(file)] sg_opt.output_dir = str(file.parent) try: print(f"Calling stubgen on {str(file)}") # WORKAROUND: Stubgen.generate_stubs does not provide a way to return the errors # such as `cannot perform relative import` # UPSTREAM: robust.py needs this to do a relative import if file.name == "robust.py" and file.parent.name == "umqtt": with open(file.with_name("__init__.py"), "a") as init: init.write("# force __init__.py") stubgen.generate_stubs(sg_opt) return True except (Exception, CompileError, SystemExit) as e: print(e) return False
from argparse import ArgumentParser from mypy.stubgen import generate_stubs, parse_options from scripts.git_helpers import checkout_django_branch from scripts.paths import DJANGO_SOURCE_DIRECTORY if __name__ == "__main__": parser = ArgumentParser() parser.add_argument("--django_version", required=True) parser.add_argument("--commit_sha", required=False) args = parser.parse_args() checkout_django_branch(args.django_version, args.commit_sha) stubgen_options = parse_options([f"{DJANGO_SOURCE_DIRECTORY}", "-o=stubgen"]) generate_stubs(stubgen_options)