def run_test(self, testcase): a = [] try: line = testcase.input[0] mask = '' if line.startswith('##'): mask = '(' + line[2:].strip() + ')$' src = '\n'.join(testcase.input) result = build.build(program_path='main', target=build.TYPE_CHECK, program_text=src, flags=[build.TEST_BUILTINS], alt_lib_path=testconfig.test_temp_dir) map = result.types kk = map.keys() keys = [] for k in kk: if k.line is not None and k.line != -1 and map[k]: if (re.match(mask, short_type(k)) or (isinstance(k, NameExpr) and re.match(mask, k.name))): keys.append(k) for key in sorted(keys, key=lambda n: (n.line, short_type(n), str(n) + str(map[n]))): ts = str(map[key]).replace('*', '') # Remove erased tags ts = ts.replace('__main__.', '') a.append('{}({}) : {}'.format(short_type(key), key.line, ts)) except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid type checker output ({}, line {})'.format(testcase.file, testcase.line))
def run_case(self, testcase: DataDrivenTestCase) -> None: try: line = testcase.input[0] mask = '' if line.startswith('##'): mask = '(' + line[2:].strip() + ')$' src = '\n'.join(testcase.input) options = Options() options.strict_optional = False # TODO: Enable strict optional checking options.use_builtins_fixtures = True options.show_traceback = True options.export_types = True result = build.build(sources=[BuildSource('main', None, src)], options=options, alt_lib_path=test_temp_dir) a = result.errors map = result.types nodes = map.keys() # Ignore NameExpr nodes of variables with explicit (trivial) types # to simplify output. searcher = SkippedNodeSearcher() for file in result.files.values(): file.accept(searcher) ignored = searcher.nodes # Filter nodes that should be included in the output. keys = [] for node in nodes: if node.line is not None and node.line != -1 and map[node]: if ignore_node(node) or node in ignored: continue if (re.match(mask, short_type(node)) or (isinstance(node, NameExpr) and re.match(mask, node.name))): # Include node in output. keys.append(node) for key in sorted(keys, key=lambda n: (n.line, short_type(n), str(n) + str(map[n]))): ts = str(map[key]).replace('*', '') # Remove erased tags ts = ts.replace('__main__.', '') a.append('{}({}) : {}'.format(short_type(key), key.line, ts)) except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid type checker output ({}, line {})'.format(testcase.file, testcase.line))
def run_test(self, testcase): implementation = testcase_python_implementation(testcase) a = [] try: line = testcase.input[0] mask = '' if line.startswith('##'): mask = '(' + line[2:].strip() + ')$' src = '\n'.join(testcase.input) result = build.build(target=build.TYPE_CHECK, sources=[BuildSource('main', None, src)], implementation=implementation, flags=[build.TEST_BUILTINS], alt_lib_path=config.test_temp_dir) map = result.types nodes = map.keys() # Ignore NameExpr nodes of variables with explicit (trivial) types # to simplify output. searcher = VariableDefinitionNodeSearcher() for file in result.files.values(): file.accept(searcher) ignored = searcher.nodes # Filter nodes that should be included in the output. keys = [] for node in nodes: if node.line is not None and node.line != -1 and map[node]: if ignore_node(node) or node in ignored: continue if (re.match(mask, short_type(node)) or (isinstance(node, NameExpr) and re.match(mask, node.name))): # Include node in output. keys.append(node) for key in sorted(keys, key=lambda n: (n.line, short_type(n), str(n) + str(map[n]))): ts = str(map[key]).replace('*', '') # Remove erased tags ts = ts.replace('__main__.', '') a.append('{}({}) : {}'.format(short_type(key), key.line, ts)) except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid type checker output ({}, line {})'.format(testcase.file, testcase.line))
def visit_name_expr(self, o: 'mypy.nodes.NameExpr') -> str: pretty = self.pretty_name(o.name, o.kind, o.fullname, o.is_inferred_def or o.is_special_form, o.node) if isinstance(o.node, mypy.nodes.Var) and o.node.is_final: pretty += ' = {}'.format(o.node.final_value) return short_type(o) + '(' + pretty + ')'
def dump(self, nodes, obj): """Convert an array of items to a multiline pretty-printed string. The tag is produced from the type name of obj and its line number. See util::DumpTagged for a description of the nodes argument. """ return dump_tagged(nodes, short_type(obj) + ':' + str(obj.line))
def dump_types(self, manager: BuildManager) -> List[str]: a = [] # To make the results repeatable, we try to generate unique and # deterministic sort keys. for module_id in sorted(manager.modules): if not is_dumped_module(module_id): continue type_map = manager.saved_cache[module_id][2] if type_map: a.append('## {}'.format(module_id)) for expr in sorted(type_map, key=lambda n: (n.line, short_type(n), str(n) + str(type_map[n]))): typ = type_map[expr] a.append('{}:{}: {}'.format(short_type(expr), expr.line, self.format_type(typ))) return a
def __str__(self): s = '{}/{}'.format(node_kinds[self.kind], short_type(self.node)) if self.mod_id is not None: s += ' ({})'.format(self.mod_id) # Include declared type of variables and functions. if self.type() is not None: s += ' : {}'.format(self.type()) return s
def __str__(self) -> str: s = "{}/{}".format(node_kinds[self.kind], short_type(self.node)) if self.mod_id is not None: s += " ({})".format(self.mod_id) # Include declared type of variables and functions. if self.type is not None: s += " : {}".format(self.type) return s
def dump(self, nodes: Sequence[object], obj: 'mypy.nodes.Context') -> str: """Convert a list of items to a multiline pretty-printed string. The tag is produced from the type name of obj and its line number. See mypy.util.dump_tagged for a description of the nodes argument. """ return dump_tagged(nodes, short_type(obj) + ':' + str(obj.get_line()))
def dump(self, nodes: Sequence[object], obj: 'mypy.nodes.Context') -> str: """Convert a list of items to a multiline pretty-printed string. The tag is produced from the type name of obj and its line number. See mypy.util.dump_tagged for a description of the nodes argument. """ tag = short_type(obj) + ':' + str(obj.get_line()) if self.show_ids: assert self.id_mapper is not None tag += '<{}>'.format(self.get_id(obj)) return dump_tagged(nodes, tag, self)
def visit_name_expr(self, o: 'mypy.nodes.NameExpr') -> str: return (short_type(o) + '(' + self.pretty_name(o.name, o.kind, o.fullname, o.is_def) + ')')
from mypy.util import short_type class Token: """Base class for all tokens""" str pre = '' # Space, comments etc. before token str string # Token string int line # Token line number void __init__(self, str string, str pre=''): self.string = string self.pre = pre str __repr__(self): """The representation is of form Keyword(' if').""" t = short_type(self) return t + '(' + self.fix(self.pre) + self.fix(self.string) + ')' str rep(self): return self.pre + self.string str fix(self, str s): """Replace common non-printable chars with escape sequences. Do not use repr() since we don't want do duplicate backslashes. """ return s.replace('\n', '\\n').replace('\t', '\\t').replace('\r', '\\r') # Token classes
else: return None mypy.types.Type type(self): # IDEA: Get rid of the any type. any node = self.node if self.type_override is not None: return self.type_override elif ((isinstance(node, Var) or isinstance(node, FuncDef)) and node.type is not None): return node.type else: return None str __str__(self): s = '{}/{}'.format(node_kinds[self.kind], short_type(self.node)) if self.mod_id is not None: s += ' ({})'.format(self.mod_id) # Include declared type of variables and functions. if self.type() is not None: s += ' : {}'.format(self.type()) return s str clean_up(str s): return re.sub('.*::', '', s) mypy.types.FunctionLike function_type(FuncBase func): if func.type: return (mypy.types.FunctionLike)func.type
def visit_name_expr(self, o: 'mypy.nodes.NameExpr') -> str: pretty = self.pretty_name(o.name, o.kind, o.fullname, o.is_inferred_def, o.node) return short_type(o) + '(' + pretty + ')'
def __repr__(self): """The representation is of form Keyword(' if').""" t = short_type(self) return t + '(' + self.fix(self.pre) + self.fix(self.string) + ')'
def __repr__(self) -> str: """The representation is of form 'Keyword( if)'.""" t = short_type(self) return t + '(' + self.fix(self.pre) + self.fix(self.string) + ')'
def visit_name_expr(self, o): return short_type(o) + "(" + self.pretty_name(o.name, o.kind, o.fullname, o.is_def) + ")"
def visit_name_expr(self, o): return (short_type(o) + '(' + self.pretty_name(o.name, o.kind, o.fullname, o.is_def) + ')')
def visit_name_expr(self, o: 'mypy.nodes.NameExpr') -> str: pretty = self.pretty_name(o.name, o.kind, o.fullname, o.is_def, o.node) return short_type(o) + '(' + pretty + ')'