def _parse_string(data, type_comments=True): try: node = _parse(data + "\n", type_comments=type_comments) except SyntaxError as exc: # If the type annotations are misplaced for some reason, we do not want # to fail the entire parsing of the file, so we need to retry the parsing without # type comment support. if exc.args[0] != MISPLACED_TYPE_ANNOTATION_ERROR or not type_comments: raise node = _parse(data + "\n", type_comments=False) return node
def _data_build(self, data, modname, path): """Build tree node from data and add some informations""" try: node = _parse(data + "\n") except (TypeError, ValueError, SyntaxError) as exc: raise exceptions.AstroidSyntaxError( "Parsing Python code failed:\n{error}", source=data, modname=modname, path=path, error=exc, ) from exc if path is not None: node_file = os.path.abspath(path) else: node_file = "<?>" if modname.endswith(".__init__"): modname = modname[:-9] package = True else: package = ( path is not None and os.path.splitext(os.path.basename(path))[0] == "__init__" ) builder = rebuilder.TreeRebuilder(self._manager) module = builder.visit_module(node, modname, node_file, package) module._import_from_nodes = builder._import_from_nodes module._delayed_assattr = builder._delayed_assattr return module
def _data_build(self, data, modname, path): """Build tree node from data and add some informations""" try: node = _parse(data + '\n') except (TypeError, ValueError, SyntaxError) as exc: raise exceptions.AstroidSyntaxError( 'Parsing Python code failed:\n{error}', source=data, modname=modname, path=path, error=exc) from exc if path is not None: node_file = os.path.abspath(path) else: node_file = '<?>' if modname.endswith('.__init__'): modname = modname[:-9] package = True else: package = path is not None and os.path.splitext( os.path.basename(path))[0] == '__init__' builder = rebuilder.TreeRebuilder(self._manager) module = builder.visit_module(node, modname, node_file, package) module._import_from_nodes = builder._import_from_nodes module._delayed_assattr = builder._delayed_assattr return module
def check_type_comment(self, node): type_comment = getattr(node, 'type_comment', None) if not type_comment: return None try: type_comment_ast = _parse(type_comment) except SyntaxError: # Invalid type comment, just skip it. return None type_object = self.visit(type_comment_ast.body[0], node) if not isinstance(type_object, nodes.Expr): return None return type_object.value