Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rope/base/arguments.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ast

import rope.base.evaluate
from rope.base import ast


class Arguments:
Expand Down
1 change: 0 additions & 1 deletion rope/base/ast.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ast
from ast import * # noqa: F401,F403

from rope.base import fscommands

Expand Down
3 changes: 2 additions & 1 deletion rope/base/builtins.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""This module tries to support builtin types and functions."""
import ast
import inspect
import io

import rope.base.evaluate
from rope.base import arguments, ast, pynames, pyobjects, utils
from rope.base import arguments, pynames, pyobjects, utils


class BuiltinModule(pyobjects.AbstractModule):
Expand Down
8 changes: 5 additions & 3 deletions rope/base/evaluate.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import ast
from operator import itemgetter
from typing import Optional, Tuple

import rope.base
import rope.base.builtins
import rope.base.pynames
import rope.base.pyobjects
from rope.base import (
arguments,
ast,
exceptions,
nameanalyze,
pyobjects,
pyobjectsdef,
worder,
)
from rope.base.ast import RopeNodeVisitor

BadIdentifierError = exceptions.BadIdentifierError

Expand Down Expand Up @@ -49,7 +51,7 @@ def eval_str(holding_scope, name):
def eval_str2(holding_scope, name):
try:
# parenthesizing for handling cases like 'a_var.\nattr'
node = ast.parse("(%s)" % name)
node = rope.base.ast.parse("(%s)" % name)
except SyntaxError:
raise BadIdentifierError("Not a resolvable python identifier selected.")
return eval_node2(holding_scope, node)
Expand Down Expand Up @@ -157,7 +159,7 @@ def _find_module(self, module_name):
)


class StatementEvaluator(ast.RopeNodeVisitor):
class StatementEvaluator(RopeNodeVisitor):
def __init__(self, scope):
self.scope = scope
self.result = None
Expand Down
6 changes: 4 additions & 2 deletions rope/base/nameanalyze.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from rope.base import ast
import ast

from rope.base.ast import RopeNodeVisitor


def get_name_levels(node):
Expand All @@ -18,7 +20,7 @@ def get_name_levels(node):
return visitor.names


class _NodeNameCollector(ast.RopeNodeVisitor):
class _NodeNameCollector(RopeNodeVisitor):
def __init__(self, levels=None):
self.names = []
self.levels = levels
Expand Down
18 changes: 10 additions & 8 deletions rope/base/oi/soa.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import rope.base.ast
import ast

import rope.base.oi.soi
import rope.base.pynames
from rope.base import arguments, evaluate, nameanalyze, pyobjects
from rope.base.ast import RopeNodeVisitor


def analyze_module(pycore, pymodule, should_analyze, search_subscopes, followed_calls):
Expand Down Expand Up @@ -31,11 +33,11 @@ def _follow(pyfunction):
)

visitor = SOAVisitor(pycore, pydefined, _follow if followed_calls else None)
for child in rope.base.ast.iter_child_nodes(pydefined.get_ast()):
for child in ast.iter_child_nodes(pydefined.get_ast()):
visitor.visit(child)


class SOAVisitor(rope.base.ast.RopeNodeVisitor):
class SOAVisitor(RopeNodeVisitor):
def __init__(self, pycore, pydefined, follow_callback=None):
self.pycore = pycore
self.pymodule = pydefined.get_module()
Expand All @@ -49,7 +51,7 @@ def _ClassDef(self, node):
pass

def _Call(self, node):
for child in rope.base.ast.iter_child_nodes(node):
for child in ast.iter_child_nodes(node):
self.visit(child)
primary, pyname = evaluate.eval_node2(self.scope, node.func)
if pyname is None:
Expand Down Expand Up @@ -97,7 +99,7 @@ def _parameter_objects(self, pyfunction):
]

def _AnnAssign(self, node):
for child in rope.base.ast.iter_child_nodes(node):
for child in ast.iter_child_nodes(node):
self.visit(child)
visitor = _SOAAssignVisitor()
nodes = []
Expand All @@ -108,7 +110,7 @@ def _AnnAssign(self, node):
self._evaluate_assign_value(node, nodes, type_hint=node.annotation)

def _Assign(self, node):
for child in rope.base.ast.iter_child_nodes(node):
for child in ast.iter_child_nodes(node):
self.visit(child)
visitor = _SOAAssignVisitor()
nodes = []
Expand Down Expand Up @@ -143,7 +145,7 @@ def __init__(self):
self.nodes = []

def _added(self, node, levels):
if isinstance(node, rope.base.ast.Subscript) and isinstance(
node.slice, (rope.base.ast.Index, rope.base.ast.expr)
if isinstance(node, ast.Subscript) and isinstance(
node.slice, (ast.Index, ast.expr)
):
self.nodes.append((node, levels))
4 changes: 2 additions & 2 deletions rope/base/oi/type_hinting/providers/numpydocstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
https://github.com/davidhalter/jedi/blob/b489019f5bd5750051122b94cc767df47751ecb7/jedi/evaluate/docstrings.py
Thanks to @davidhalter for this utils under MIT License.
"""
import ast
import re

from rope.base.ast import literal_eval
from rope.base.oi.type_hinting.providers import docstrings

try:
Expand All @@ -27,7 +27,7 @@ def __call__(self, docstring, param_name):
p_type = m.group(1)

if p_type.startswith("{"):
types = {type(x).__name__ for x in literal_eval(p_type)}
types = {type(x).__name__ for x in ast.literal_eval(p_type)}
return list(types)
else:
return [p_type]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import re

from rope.base.oi.type_hinting import utils
from rope.base.oi.type_hinting.providers import interfaces


Expand Down
3 changes: 2 additions & 1 deletion rope/base/pyobjects.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ast
from typing import Optional

from rope.base import ast, exceptions, utils
from rope.base import exceptions, utils


class PyObject:
Expand Down
16 changes: 9 additions & 7 deletions rope/base/pyobjectsdef.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ast

import rope.base.builtins
import rope.base.codeanalyze
import rope.base.evaluate
Expand All @@ -6,14 +8,14 @@
import rope.base.pyscopes
from rope.base import (
arguments,
ast,
exceptions,
fscommands,
nameanalyze,
pynamesdef,
pyobjects,
utils,
)
from rope.base.ast import RopeNodeVisitor


class PyFunction(pyobjects.PyFunction):
Expand Down Expand Up @@ -177,7 +179,7 @@ def __init__(self, pycore, source=None, resource=None, force_errors=False):
raise
else:
source = "\n"
node = ast.parse("\n")
node = rope.base.ast.parse("\n")
self.source_code = source
self.star_imports = []
self.visitor_class = _GlobalVisitor
Expand All @@ -197,7 +199,7 @@ def _init_source(self, pycore, source_code, resource):
source_bytes = fscommands.unicode_to_file_data(source_code)
else:
source_bytes = source_code
ast_node = ast.parse(source_bytes, filename=filename)
ast_node = rope.base.ast.parse(source_bytes, filename=filename)
except SyntaxError as e:
raise exceptions.ModuleSyntaxError(filename, e.lineno, e.msg)
except UnicodeDecodeError as e:
Expand Down Expand Up @@ -239,7 +241,7 @@ def __init__(self, pycore, resource=None, force_errors=False):
init_dot_py, force_errors=force_errors
).get_ast()
else:
ast_node = ast.parse("\n")
ast_node = rope.base.ast.parse("\n")
super().__init__(pycore, ast_node, resource)

def _create_structural_attributes(self):
Expand Down Expand Up @@ -291,7 +293,7 @@ def get_name(self):
return rope.base.libutils.modname(self.resource) if self.resource else ""


class _AnnAssignVisitor(ast.RopeNodeVisitor):
class _AnnAssignVisitor(RopeNodeVisitor):
def __init__(self, scope_visitor):
self.scope_visitor = scope_visitor
self.assigned_ast = None
Expand Down Expand Up @@ -333,7 +335,7 @@ def _Slice(self, node):
pass


class _ExpressionVisitor(ast.RopeNodeVisitor):
class _ExpressionVisitor(RopeNodeVisitor):
def __init__(self, scope_visitor):
self.scope_visitor = scope_visitor

Expand All @@ -360,7 +362,7 @@ def _NamedExpr(self, node):
self.visit(node.value)


class _AssignVisitor(ast.RopeNodeVisitor):
class _AssignVisitor(RopeNodeVisitor):
def __init__(self, scope_visitor):
self.scope_visitor = scope_visitor
self.assigned_ast = None
Expand Down
3 changes: 2 additions & 1 deletion rope/base/pyscopes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ast
import rope.base.builtins # Use full qualification for clarity.
from rope.base import ast, codeanalyze, exceptions, pynames, utils
from rope.base import codeanalyze, exceptions, pynames, utils
from rope.refactor import patchedast


Expand Down
1 change: 0 additions & 1 deletion rope/contrib/autoimport/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""AutoImport module for rope."""
from .pickle import AutoImport as _PickleAutoImport
from .sqlite import AutoImport as _SqliteAutoImport

AutoImport = _PickleAutoImport

Expand Down
5 changes: 3 additions & 2 deletions rope/contrib/autoimport/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
Can extract names from source code of a python file, .so object, or builtin module.
"""

import ast
import inspect
import logging
import pathlib
from importlib import import_module
from typing import Generator, List

from rope.base import ast
import rope.base

from .defs import (
ModuleCompiled,
Expand Down Expand Up @@ -45,7 +46,7 @@ def get_names_from_file(
) -> Generator[PartialName, None, None]:
"""Get all the names from a given file using ast."""
try:
root_node = ast.parse(module.read_bytes())
root_node = rope.base.ast.parse(module.read_bytes())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I am aware, this should use the stdlib ast parser

Copy link
Contributor Author

@edreamleo edreamleo Jan 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bagel897 Thanks for you comment! rope.base.ast.parse wraps stdlib.ast.parse: it regularizes the source argument and catches two exceptions. These seem like reasonable things to do.

@lieryan would like to replace the one remaining call to stdlib.ast.parse with a call to rope.base.ast.parse.

I see two remaining tasks before marking this PR as ready for review:

except SyntaxError as error:
print(error)
return
Expand Down
7 changes: 5 additions & 2 deletions rope/contrib/finderrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
* ... ;-)

"""
from rope.base import ast, evaluate, pyobjects
import ast

from rope.base import evaluate, pyobjects
from rope.base.ast import RopeNodeVisitor


def find_errors(project, resource):
Expand All @@ -37,7 +40,7 @@ def find_errors(project, resource):
return finder.errors


class _BadAccessFinder(ast.RopeNodeVisitor):
class _BadAccessFinder(RopeNodeVisitor):
def __init__(self, pymodule):
self.pymodule = pymodule
self.scope = pymodule.get_scope()
Expand Down
21 changes: 12 additions & 9 deletions rope/refactor/extract.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import ast
import re
from contextlib import contextmanager
from itertools import chain

from rope.base import ast, codeanalyze
import rope.base
from rope.base import codeanalyze
from rope.base.ast import RopeNodeVisitor
from rope.base.change import ChangeContents, ChangeSet
from rope.base.exceptions import RefactoringError
from rope.base.utils.datastructures import OrderedSet
Expand Down Expand Up @@ -514,7 +517,7 @@ def _is_on_a_word(self, info, offset):
return next.isalnum() or next == "_"


class _ExtractMethodParts(ast.RopeNodeVisitor):
class _ExtractMethodParts(RopeNodeVisitor):
def __init__(self, info):
self.info = info
self.info_collector = self._create_info_collector()
Expand Down Expand Up @@ -777,7 +780,7 @@ def get_checks(self):
return {}


class _FunctionInformationCollector(ast.RopeNodeVisitor):
class _FunctionInformationCollector(RopeNodeVisitor):
def __init__(self, start, end, is_global):
self.start = start
self.end = end
Expand Down Expand Up @@ -958,7 +961,7 @@ def _get_argnames(arguments):
return result


class _VariableReadsAndWritesFinder(ast.RopeNodeVisitor):
class _VariableReadsAndWritesFinder(RopeNodeVisitor):
def __init__(self):
self.written = set()
self.read = set()
Expand Down Expand Up @@ -998,7 +1001,7 @@ def find_reads_for_one_liners(code):
return visitor.read


class _BaseErrorFinder(ast.RopeNodeVisitor):
class _BaseErrorFinder(RopeNodeVisitor):
@classmethod
def has_errors(cls, code):
if code.strip() == "":
Expand Down Expand Up @@ -1066,7 +1069,7 @@ def _ClassDef(self, node):
pass


class _GlobalFinder(ast.RopeNodeVisitor):
class _GlobalFinder(RopeNodeVisitor):
def __init__(self):
self.globals_ = OrderedSet()

Expand All @@ -1081,13 +1084,13 @@ def _get_function_kind(scope):
def _parse_text(body):
body = sourceutils.fix_indentation(body, 0)
try:
node = ast.parse(body)
node = rope.base.ast.parse(body)
except SyntaxError:
# needed to parse expression containing := operator
try:
node = ast.parse("(" + body + ")")
node = rope.base.ast.parse("(" + body + ")")
except SyntaxError:
node = ast.parse(
node = rope.base.ast.parse(
"async def __rope_placeholder__():\n"
+ sourceutils.fix_indentation(body, 4)
)
Expand Down
Loading