def test_stubgen(testcase): source = '\n'.join(testcase.input) name = 'prog%d' % random.randrange(1000 * 1000 * 1000) path = '%s.py' % name out_dir = '_out' os.mkdir(out_dir) try: with open(path, 'w') as file: file.write(source) file.close() # Without this we may sometimes be unable to import the module below, as importlib # caches os.listdir() results in Python 3.3+ (Guido explained this to me). reset_importlib_caches() try: if testcase.name.endswith('_import'): generate_stub_for_module(name, out_dir, quiet=True) else: generate_stub(path, out_dir) a = load_output(out_dir) except CompileError as e: a = e.messages assert_string_arrays_equal(testcase.output, a, 'Invalid output ({}, line {})'.format( testcase.file, testcase.line)) finally: shutil.rmtree(out_dir) os.remove(path)
def test_stubgen(testcase): if 'stubgen-test-path' not in sys.path: sys.path.insert(0, 'stubgen-test-path') os.mkdir('stubgen-test-path') source = '\n'.join(testcase.input) handle = tempfile.NamedTemporaryFile(prefix='prog_', suffix='.py', dir='stubgen-test-path') assert os.path.isabs(handle.name) path = os.path.basename(handle.name) name = path[:-3] path = os.path.join('stubgen-test-path', path) out_dir = '_out' os.mkdir(out_dir) try: with open(path, 'w') as file: file.write(source) file.close() # Without this we may sometimes be unable to import the module below, as importlib # caches os.listdir() results in Python 3.3+ (Guido explained this to me). reset_importlib_caches() try: if testcase.name.endswith('_import'): generate_stub_for_module(name, out_dir, quiet=True) else: generate_stub(path, out_dir) a = load_output(out_dir) except CompileError as e: a = e.messages assert_string_arrays_equal(testcase.output, a, 'Invalid output ({}, line {})'.format( testcase.file, testcase.line)) finally: shutil.rmtree(out_dir) handle.close()
def test_stubgen(testcase: DataDrivenTestCase) -> None: if 'stubgen-test-path' not in sys.path: sys.path.insert(0, 'stubgen-test-path') os.mkdir('stubgen-test-path') source = '\n'.join(testcase.input) options = parse_flags(source) handle = tempfile.NamedTemporaryFile(prefix='prog_', suffix='.py', dir='stubgen-test-path', delete=False) assert os.path.isabs(handle.name) path = os.path.basename(handle.name) name = path[:-3] path = os.path.join('stubgen-test-path', path) out_dir = '_out' os.mkdir(out_dir) try: handle.write(bytes(source, 'ascii')) handle.close() # Without this we may sometimes be unable to import the module below, as importlib # caches os.listdir() results in Python 3.3+ (Guido explained this to me). reset_importlib_caches() try: if testcase.name.endswith('_import'): generate_stub_for_module( name, out_dir, quiet=True, no_import=options.no_import, include_private=options.include_private) else: generate_stub(path, out_dir, include_private=options.include_private) a = load_output(out_dir) except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid output ({}, line {})'.format(testcase.file, testcase.line)) finally: handle.close() os.unlink(handle.name) shutil.rmtree(out_dir)
def test_stubgen(testcase: DataDrivenTestCase) -> None: if 'stubgen-test-path' not in sys.path: sys.path.insert(0, 'stubgen-test-path') os.mkdir('stubgen-test-path') source = '\n'.join(testcase.input) options = parse_flags(source) handle = tempfile.NamedTemporaryFile(prefix='prog_', suffix='.py', dir='stubgen-test-path', delete=False) assert os.path.isabs(handle.name) path = os.path.basename(handle.name) name = path[:-3] path = os.path.join('stubgen-test-path', path) out_dir = '_out' os.mkdir(out_dir) try: handle.write(bytes(source, 'ascii')) handle.close() # Without this we may sometimes be unable to import the module below, as importlib # caches os.listdir() results in Python 3.3+ (Guido explained this to me). reset_importlib_cache('stubgen-test-path') try: if testcase.name.endswith('_import'): generate_stub_for_module(name, out_dir, quiet=True, no_import=options.no_import, include_private=options.include_private) else: generate_stub(path, out_dir, include_private=options.include_private) a = load_output(out_dir) except CompileError as e: a = e.messages assert_string_arrays_equal(testcase.output, a, 'Invalid output ({}, line {})'.format( testcase.file, testcase.line)) finally: handle.close() os.unlink(handle.name) shutil.rmtree(out_dir)
def generate_stub(module: str, config: PostgresConfig, output_dir: str, line_length=80, suppress_errors=False, limit_rows=300): stubgen.generate_stub_for_module(module, output_dir=output_dir) path_to_stub_file = pathlib.Path(get_path_to_stubfile(module, output_dir=output_dir)) shutil.move(path_to_stub_file, path_to_stub_file.with_suffix('.py')) stub_content = None stub = generate_stub_for_module(config, module, suppress_errors=suppress_errors, limit_rows=limit_rows, suppressed_exceptions=[TemplateDoesNotExist]) if stub is not None: stub_content = stub.render() if stub_content: stub_content = stub_content.replace('Ellipsis', '...') with open(path_to_stub_file, 'w') as stub_file: stub_file.write(stub_content) else: print('Stub file content is empty') pyi_dir = path_to_stub_file.parent retype.Config.incremental = True try: retype.retype_file(path_to_stub_file.with_suffix('.py'), pyi_dir, pyi_dir) except (ValueError, SyntaxError): shutil.move(path_to_stub_file, path_to_stub_file.with_suffix('.py')) # subprocess.run(['retype', '--pyi-dir', pyi_dir, '--target-dir', pyi_dir, # path_to_stub_file.with_suffix('.py')]) shutil.move(path_to_stub_file.with_suffix('.py'), path_to_stub_file) subprocess.run(['black', '--line-length', str(line_length), '--pyi', path_to_stub_file]) subprocess.run(['isort', '-a', 'from typing import Optional', path_to_stub_file]) return path_to_stub_file
def test_stubgen(testcase): if 'stubgen-test-path' not in sys.path: sys.path.insert(0, 'stubgen-test-path') os.mkdir('stubgen-test-path') source = '\n'.join(testcase.input) handle = tempfile.NamedTemporaryFile(prefix='prog_', suffix='.py', dir='stubgen-test-path') assert os.path.isabs(handle.name) path = os.path.basename(handle.name) name = path[:-3] path = os.path.join('stubgen-test-path', path) out_dir = '_out' os.mkdir(out_dir) try: with open(path, 'w') as file: file.write(source) file.close() # Without this we may sometimes be unable to import the module below, as importlib # caches os.listdir() results in Python 3.3+ (Guido explained this to me). reset_importlib_caches() try: if testcase.name.endswith('_import'): generate_stub_for_module(name, out_dir, quiet=True) else: generate_stub(path, out_dir) a = load_output(out_dir) except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid output ({}, line {})'.format(testcase.file, testcase.line)) finally: shutil.rmtree(out_dir) handle.close()
def read_module_source_from_file(id: str, lib_path: List[str]) -> Tuple[str, str]: """Find and read the source file of a module. Return a pair (path, file contents). Return (None, None) if the module could not be found or read. Args: id: module name, a string of form 'foo' or 'foo.bar' lib_path: library search path """ path = find_module(id, lib_path) if path is None and False: outdir = '/Users/yang/proj/sales/stubs/' # +'/'.join(id.split('.'))+'.pyi' from mypy.stubgen import generate_stub_for_module path = generate_stub_for_module(id, outdir, add_header=True, sigs={}, class_sigs={}, pyversion=PYTHON2_VERSION) if path is not None: text = '' try: f = open(path) try: text = f.read() finally: f.close() except IOError: return None, None except UnicodeDecodeError: f = open(path, encoding='latin1') try: text = f.read() except: return None, None finally: f.close() return path, text else: return None, None