def test_parse_error(testcase): try: # Compile temporary file. parse(bytes("\n".join(testcase.input), "ascii"), INPUT_FILE_NAME) raise AssertionFailure("No errors reported") except CompileError as e: # Verify that there was a compile error and that the error messages # are equivalent. assert_string_arrays_equal( testcase.output, e.messages, "Invalid compiler output ({}, line {})".format(testcase.file, testcase.line) )
def test_parse_error(testcase): try: # Compile temporary file. The test file contains non-ASCII characters. parse(bytes('\n'.join(testcase.input), 'utf-8'), INPUT_FILE_NAME, None, Options()) raise AssertionFailure('No errors reported') except CompileError as e: # Verify that there was a compile error and that the error messages # are equivalent. assert_string_arrays_equal( testcase.output, e.messages, 'Invalid compiler output ({}, line {})'.format(testcase.file, testcase.line))
def test_parse_error(testcase): try: # Compile temporary file. parse(bytes('\n'.join(testcase.input), 'ascii'), INPUT_FILE_NAME) raise AssertionFailure('No errors reported') except CompileError as e: # Verify that there was a compile error and that the error messages # are equivalent. assert_string_arrays_equal( testcase.output, e.messages, 'Invalid compiler output ({}, line {})'.format(testcase.file, testcase.line))
def test_parse_error(testcase: DataDrivenTestCase) -> None: try: # Compile temporary file. The test file contains non-ASCII characters. parse(bytes('\n'.join(testcase.input), 'utf-8'), INPUT_FILE_NAME, None, Options()) raise AssertionFailure('No errors reported') except CompileError as e: # Verify that there was a compile error and that the error messages # are equivalent. assert_string_arrays_equal( testcase.output, e.messages, 'Invalid compiler output ({}, line {})'.format( testcase.file, testcase.line))
def test_parse_error(testcase: DataDrivenTestCase) -> None: try: # Compile temporary file. The test file contains non-ASCII characters. parse(bytes('\n'.join(testcase.input), 'utf-8'), INPUT_FILE_NAME, '__main__', None, Options()) raise AssertionError('No errors reported') except CompileError as e: if e.module_with_blocker is not None: assert e.module_with_blocker == '__main__' # Verify that there was a compile error and that the error messages # are equivalent. assert_string_arrays_equal( testcase.output, e.messages, 'Invalid compiler output ({}, line {})'.format(testcase.file, testcase.line))
def apply_field_accessors(self, spec: ConversionSpecifier, repl: Expression, ctx: Context) -> Expression: """Transform and validate expr in '{.attr[item]}'.format(expr) into expr.attr['item']. If validation fails, return TempNode(AnyType). """ assert spec.key, "Keys must be auto-generated first!" if spec.field == spec.key: return repl assert spec.field # This is a bit of a dirty trick, but it looks like this is the simplest way. temp_errors = self.msg.clean_copy().errors dummy = DUMMY_FIELD_NAME + spec.field[len(spec.key):] temp_ast = parse(dummy, fnam='<format>', module=None, options=self.chk.options, errors=temp_errors) # type: Node if temp_errors.is_errors(): self.msg.fail('Syntax error in format specifier "{}"'.format(spec.field), ctx, code=codes.STRING_FORMATTING) return TempNode(AnyType(TypeOfAny.from_error)) # These asserts are guaranteed by the original regexp. assert isinstance(temp_ast, MypyFile) temp_ast = temp_ast.defs[0] assert isinstance(temp_ast, ExpressionStmt) temp_ast = temp_ast.expr if not self.validate_and_transform_accessors(temp_ast, repl, spec, ctx=ctx): return TempNode(AnyType(TypeOfAny.from_error)) # Check if there are any other errors (like missing members). # TODO: fix column to point to actual start of the format specifier _within_ string. temp_ast.line = ctx.line temp_ast.column = ctx.column self.exprchk.accept(temp_ast) return temp_ast
def test_parser(testcase: DataDrivenTestCase) -> None: """Perform a single parser test case. The argument contains the description of the test case. """ options = Options() if testcase.file.endswith('python2.test'): options.python_version = defaults.PYTHON2_VERSION else: options.python_version = defaults.PYTHON3_VERSION try: n = parse(bytes('\n'.join(testcase.input), 'ascii'), fnam='main', module='__main__', errors=None, options=options) a = str(n).split('\n') except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid parser output ({}, line {})'.format(testcase.file, testcase.line))
def test_parse_error(testcase: DataDrivenTestCase) -> None: try: options = parse_options('\n'.join(testcase.input), testcase, 0) if options.python_version != sys.version_info[:2]: skip() # Compile temporary file. The test file contains non-ASCII characters. parse(bytes('\n'.join(testcase.input), 'utf-8'), INPUT_FILE_NAME, '__main__', None, options) raise AssertionError('No errors reported') except CompileError as e: if e.module_with_blocker is not None: assert e.module_with_blocker == '__main__' # Verify that there was a compile error and that the error messages # are equivalent. assert_string_arrays_equal( testcase.output, e.messages, 'Invalid compiler output ({}, line {})'.format( testcase.file, testcase.line))
def dump(fname: str, python_version: Tuple[int, int], quiet: bool = False) -> None: options = Options() options.python_version = python_version with open(fname, 'rb') as f: s = f.read() tree = parse(s, fname, None, errors=None, options=options) if not quiet: print(tree)
def parse(self, source_text, fnam): """Parse the source of a file with the given name. Raise CompileError if there is a parse error. """ num_errs = self.errors().num_messages() tree = parse.parse(source_text, fnam, self.errors()) tree._full_name = self.id if self.errors().num_messages() != num_errs: self.errors().raise_error() return tree
def test_parser(testcase): """Perform a single parser test case. The argument contains the description of the test case. """ try: n = parse('\n'.join(testcase.input)) a = str(n).split('\n') except CompileError as e: a = e.messages assert_string_arrays_equal(testcase.output, a, 'Invalid parser output ({}, line {})'.format( testcase.file, testcase.line))
def parse(self, source_text: str, fnam: str) -> MypyFile: """Parse the source of a file with the given name. Raise CompileError if there is a parse error. """ num_errs = self.errors().num_messages() tree = parse.parse(source_text, fnam, self.errors(), pyversion=self.manager.pyversion) tree._fullname = self.id if self.errors().num_messages() != num_errs: self.errors().raise_error() return tree
def parse(self, source_text: Union[str, bytes], fnam: str) -> MypyFile: """Parse the source of a file with the given name. Raise CompileError if there is a parse error. """ num_errs = self.errors().num_messages() tree = parse.parse(source_text, fnam, self.errors(), dialect=self.manager.dialect, custom_typing_module=self.manager.custom_typing_module) tree._fullname = self.id if self.errors().num_messages() != num_errs: self.errors().raise_error() return tree
def parse(self, source_text: Union[str, bytes], fnam: str) -> MypyFile: """Parse the source of a file with the given name. Raise CompileError if there is a parse error. """ num_errs = self.errors().num_messages() tree = parse.parse(source_text, fnam, self.errors(), pyversion=self.manager.pyversion, custom_typing_module=self.manager.custom_typing_module, fast_parser=FAST_PARSER in self.manager.flags) tree._fullname = self.id if self.errors().num_messages() != num_errs: self.errors().raise_error() return tree
def test_output(testcase): """Perform an identity source code transformation test case.""" expected = testcase.output if expected == []: expected = testcase.input try: src = '\n'.join(testcase.input) # Parse and analyze the source program. # Parse and semantically analyze the source program. any trees, any symtable, any infos, any types # Test case names with a special suffix get semantically analyzed. This # lets us test that semantic analysis does not break source code pretty # printing. if testcase.name.endswith('_SemanticAnalyzer'): result = build.build('main', target=build.SEMANTIC_ANALYSIS, program_text=src, flags=[build.TEST_BUILTINS], alt_lib_path=test_temp_dir) files = result.files else: files = {'main': parse(src, 'main')} a = [] first = True # Produce an output containing the pretty-printed forms (with original # formatting) of all the relevant source files. for fnam in sorted(files.keys()): f = files[fnam] # Omit the builtins and files marked for omission. if (not f.path.endswith(os.sep + 'builtins.py') and '-skip.' not in f.path): # Add file name + colon for files other than the first. if not first: a.append('{}:'.format(fix_path(remove_prefix( f.path, test_temp_dir)))) v = OutputVisitor() f.accept(v) s = v.output() if s != '': a += s.split('\n') first = False except CompileError as e: a = e.messages assert_string_arrays_equal( expected, a, 'Invalid source code output ({}, line {})'.format( testcase.file, testcase.line))
def test_output(testcase): """Perform an identity source code transformation test case.""" expected = testcase.output if expected == []: expected = testcase.input try: src = '\n'.join(testcase.input) # Parse and semantically analyze the source program. # Test case names with a special suffix get semantically analyzed. This # lets us test that semantic analysis does not break source code pretty # printing. if testcase.name.endswith('_SemanticAnalyzer'): result = build.build('main', target=build.SEMANTIC_ANALYSIS, program_text=src, flags=[build.TEST_BUILTINS], alt_lib_path=test_temp_dir) files = result.files else: files = {'main': parse(src, 'main')} a = [] first = True # Produce an output containing the pretty-printed forms (with original # formatting) of all the relevant source files. for fnam in sorted(files.keys()): f = files[fnam] # Omit the builtins and files marked for omission. if (not f.path.endswith(os.sep + 'builtins.py') and '-skip.' not in f.path): # Add file name + colon for files other than the first. if not first: a.append('{}:'.format( fix_path(remove_prefix(f.path, test_temp_dir)))) v = OutputVisitor() f.accept(v) s = v.output() if s != '': a += s.split('\n') first = False except CompileError as e: a = e.messages assert_string_arrays_equal( expected, a, 'Invalid source code output ({}, line {})'.format( testcase.file, testcase.line))
def test_parser(testcase): """Perform a single parser test case. The argument contains the description of the test case. """ pyversion = 3 if testcase.file.endswith("python2.test"): pyversion = 2 try: n = parse(bytes("\n".join(testcase.input), "ascii"), pyversion=pyversion, fnam="main") a = str(n).split("\n") except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, "Invalid parser output ({}, line {})".format(testcase.file, testcase.line) )
def test_parser(testcase): """Perform a single parser test case. The argument contains the description of the test case. """ pyversion = 3 if testcase.file.endswith('python2.test'): pyversion = 2 try: n = parse(bytes('\n'.join(testcase.input), 'ascii'), pyversion=pyversion) a = str(n).split('\n') except CompileError as e: a = e.messages assert_string_arrays_equal(testcase.output, a, 'Invalid parser output ({}, line {})'.format( testcase.file, testcase.line))
def parse(self, source_text: Union[str, bytes], fnam: str) -> MypyFile: """Parse the source of a file with the given name. Raise CompileError if there is a parse error. """ num_errs = self.errors().num_messages() tree = parse.parse( source_text, fnam, self.errors(), pyversion=self.manager.pyversion, custom_typing_module=self.manager.custom_typing_module, implicit_any=self.manager.implicit_any, fast_parser=FAST_PARSER in self.manager.flags) tree._fullname = self.id if self.errors().num_messages() != num_errs: self.errors().raise_error() return tree
def test_parser(testcase): """Perform a single parser test case. The argument contains the description of the test case. """ pyversion = 3 if testcase.file.endswith('python2.test'): pyversion = 2 try: n = parse('\n'.join(testcase.input), pyversion=pyversion) a = str(n).split('\n') except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid parser output ({}, line {})'.format(testcase.file, testcase.line))
def test_parser(testcase): """Perform a single parser test case. The argument contains the description of the test case. """ if testcase.file.endswith('python2.test'): pyversion = defaults.PYTHON2_VERSION else: pyversion = defaults.PYTHON3_VERSION try: n = parse(bytes('\n'.join(testcase.input), 'ascii'), pyversion=pyversion, fnam='main') a = str(n).split('\n') except CompileError as e: a = e.messages assert_string_arrays_equal(testcase.output, a, 'Invalid parser output ({}, line {})'.format( testcase.file, testcase.line))
def test_parser(testcase): """Perform a single parser test case. The argument contains the description of the test case. """ if testcase.file.endswith('python2.test'): dialect = Dialect('2.7.0') else: dialect = default_dialect() try: n = parse(bytes('\n'.join(testcase.input), 'ascii'), dialect=dialect, fnam='main') a = str(n).split('\n') except CompileError as e: a = e.messages assert_string_arrays_equal(testcase.output, a, 'Invalid parser output ({}, line {})'.format( testcase.file, testcase.line))
def dump(fname: str, python_version: Tuple[int, int], quiet: bool = False) -> None: """ Parameters ---------- fname : str DESCRIPTION. python_version : Tuple[int, int] DESCRIPTION. quiet : bool, optional DESCRIPTION. The default is False. Returns ------- None DESCRIPTION. """ options = Options() options.python_version = python_version with open(fname, 'rb') as f_name: string = f_name.read() tree = parse(string, fname, None, errors=None, options=options) from pudb import set_trace seen = set() if not quiet: lst = tree.defs for xs in lst: if isinstance(xs, mypy.nodes.FuncDef): seen.add((xs.line, xs.name)) if isinstance(xs, mypy.nodes.ExpressionStmt): resp = xs.expr for ix in resp.args: seen.add((ix.line, ix)) if isinstance(xs, mypy.nodes.Decorator): for node in xs.decorators: seen.add((xs.line, xs.name)) for item in sorted(list(seen), key=lambda x: x[0]): print(item[0], item[1])
def test_parser(testcase): """Perform a single parser test case. The argument contains the description of the test case. """ options = Options() if testcase.file.endswith('python2.test'): options.python_version = defaults.PYTHON2_VERSION else: options.python_version = defaults.PYTHON3_VERSION try: n = parse(bytes('\n'.join(testcase.input), 'ascii'), fnam='main', errors=None, options=options) a = str(n).split('\n') except CompileError as e: a = e.messages assert_string_arrays_equal(testcase.output, a, 'Invalid parser output ({}, line {})'.format( testcase.file, testcase.line))
def print_nodes(file: str) -> None: mypy_file = parse(file, "ASD", None, Options()) for d in mypy_file.defs: print(d.expr.accept(evaluator))
if text is not None: info = StateInfo(path, id, self.errors().import_context(), self.manager) self.manager.states.append(UnprocessedFile(info, text)) self.manager.module_files[id] = path return True else: return False MypyFile parse(self, str source_text, str fnam): """Parse the source of a file with the given name. Raise CompileError if there is a parse error. """ num_errs = self.errors().num_messages() tree = parse.parse(source_text, fnam, self.errors()) tree._full_name = self.id if self.errors().num_messages() != num_errs: self.errors().raise_error() return tree int state(self): return UNPROCESSED_STATE class ParsedFile(State): MypyFile tree void __init__(self, StateInfo info, MypyFile tree): super().__init__(info) self.tree = tree
def run(filepath): with open(filepath) as rf: source = rf.read() sources, options = process_options([filepath]) return parse(source, filepath, None, options)