Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR 3 for #570: the name 'ast' #580

Closed
wants to merge 14 commits into from
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
9 changes: 6 additions & 3 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ disable=
modified-iterating-list,
used-before-assignment,
unsubscriptable-object,
use-sequence-for-iteration,
unpacking-non-sequence,

# Rope: to be removed.
Expand All @@ -70,8 +71,6 @@ disable=
superfluous-parens,
unused-import,
unused-wildcard-import,
useless-super-delegation,

useless-else-on-loop, #Else clause on loop without a break statement, remove the else and de-indent all the code inside it.
unidiomatic-typecheck,
wildcard-import,
Expand All @@ -93,7 +92,11 @@ disable=
super-init-not-called,
unnecessary-lambda,
unsupported-assignment-operation,


# Rope: recently enabled checks.

# useless-super-delegation,

# Leo: Standard suppressions.

arguments-renamed, # cursesGui2.py
Expand Down
20 changes: 11 additions & 9 deletions rope/base/arguments.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import rope.base.evaluate
from rope.base import ast
import ast

from rope.base import builtins, evaluate, pyobjects


class Arguments:
Expand Down Expand Up @@ -36,9 +37,10 @@ def get_pynames(self, parameters):
def get_instance_pyname(self):
if self.args:
return self._evaluate(self.args[0])
return None

def _evaluate(self, ast_node):
return rope.base.evaluate.eval_node(self.scope, ast_node)
return evaluate.eval_node(self.scope, ast_node)


def create_arguments(primary, pyfunction, call_node, scope):
Expand Down Expand Up @@ -99,13 +101,13 @@ def _is_method_call(primary, pyfunction):
return False
pyobject = primary.get_object()
if (
isinstance(pyobject.get_type(), rope.base.pyobjects.PyClass)
and isinstance(pyfunction, rope.base.pyobjects.PyFunction)
and isinstance(pyfunction.parent, rope.base.pyobjects.PyClass)
isinstance(pyobject.get_type(), pyobjects.PyClass)
and isinstance(pyfunction, pyobjects.PyFunction)
and isinstance(pyfunction.parent, pyobjects.PyClass)
):
return True
if isinstance(
pyobject.get_type(), rope.base.pyobjects.AbstractClass
) and isinstance(pyfunction, rope.base.builtins.BuiltinFunction):
if isinstance(pyobject.get_type(), pyobjects.AbstractClass) and isinstance(
pyfunction, builtins.BuiltinFunction
):
return True
return False
18 changes: 0 additions & 18 deletions rope/base/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,6 @@
from rope.base import fscommands


def parse(source, filename="<string>"):
# NOTE: the raw string should be given to `compile` function
if isinstance(source, str):
source = fscommands.unicode_to_file_data(source)
if b"\r" in source:
source = source.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
if not source.endswith(b"\n"):
source += b"\n"
try:
return ast.parse(source, filename="<unknown>")
except (TypeError, ValueError) as e:
error = SyntaxError()
error.lineno = 1
error.filename = filename
error.msg = str(e)
raise error


def call_for_nodes(node, callback, recursive=False):
"""If callback returns `True` the child nodes are skipped"""
result = callback(node)
Expand Down
33 changes: 33 additions & 0 deletions rope/base/astwrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Wrappers for stdlib.ast.parse and stdlib.ast.walk."""
import ast

from rope.base import astutils, fscommands


def parse(source, filename="<string>"):
# NOTE: the raw string should be given to `compile` function
if isinstance(source, str):
source = fscommands.unicode_to_file_data(source)
if b"\r" in source:
source = source.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
if not source.endswith(b"\n"):
source += b"\n"
try:
return ast.parse(source, filename="<unknown>")
except (TypeError, ValueError) as e:
error = SyntaxError()
error.lineno = 1
error.filename = filename
error.msg = str(e)
raise error


def walk(node, walker) -> None:
"""Walk the syntax tree"""
method_name = "_" + node.__class__.__name__
method = getattr(walker, method_name, None)
if method is not None:
method(node)
return
for child in astutils.get_child_nodes(node):
walk(child, walker)
5 changes: 3 additions & 2 deletions 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 ast, pynames, pyobjects, arguments, utils
from rope.base import arguments, pynames, pyobjects, utils


try:
Expand Down Expand Up @@ -39,7 +40,7 @@ def attributes(self):
if self.pycore is not None:
submodules = self.pycore._builtin_submodules(self.name)
for name, module in submodules.items():
result[name] = rope.base.builtins.BuiltinName(module)
result[name] = BuiltinName(module)
return result

@property
Expand Down
4 changes: 1 addition & 3 deletions rope/base/libutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

import rope.base.project
import rope.base.pycore
from rope.base import pyobjectsdef
from rope.base import utils
from rope.base import taskhandle
from rope.base import pyobjectsdef, taskhandle, utils


def path_to_resource(project, path, type=None):
Expand Down
2 changes: 1 addition & 1 deletion rope/base/oi/soi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import rope.base.builtins
import rope.base.pynames
import rope.base.pyobjects
from rope.base import evaluate, utils, arguments
from rope.base import arguments, evaluate, utils
from rope.base.oi.type_hinting.factory import get_type_hinting_factory


Expand Down
15 changes: 8 additions & 7 deletions rope/base/pycore.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import rope.base.oi.doa
import rope.base.oi.objectinfo
import rope.base.oi.soa
from rope.base import builtins
from rope.base import exceptions
from rope.base import pyobjectsdef
from rope.base import stdmods
from rope.base import taskhandle
from rope.base import utils
from rope.base.exceptions import ModuleNotFoundError
from rope.base import (
builtins,
exceptions,
pyobjectsdef,
stdmods,
taskhandle,
utils,
)


class PyCore:
Expand Down
2 changes: 2 additions & 0 deletions rope/base/pynames.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# These imports are tricky. It's easy to cause circular imports.
from __future__ import annotations

import typing
Expand All @@ -7,6 +8,7 @@


if typing.TYPE_CHECKING:
# pyobjectsdef appears only in annotations.
from typing import Union
from rope.base import pyobjectsdef

Expand Down
11 changes: 6 additions & 5 deletions rope/base/pyobjectsdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import rope.base.oi.soi
import rope.base.pyscopes
from rope.base import (
pynamesdef,
exceptions,
arguments,
ast,
astwrapper,
astutils,
pyobjects,
exceptions,
fscommands,
arguments,
pynamesdef,
pyobjects,
utils,
)

Expand Down Expand Up @@ -197,7 +198,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 = astwrapper.parse(source_bytes, filename=filename)
except SyntaxError as e:
raise exceptions.ModuleSyntaxError(filename, e.lineno, e.msg)
except UnicodeDecodeError as e:
Expand Down
4 changes: 1 addition & 3 deletions rope/base/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import re
import warnings

from rope.base import change
from rope.base import exceptions
from rope.base import fscommands
from rope.base import change, exceptions, fscommands
from pathlib import Path


Expand Down
2 changes: 1 addition & 1 deletion rope/base/taskhandle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractmethod
from typing import Optional, Sequence

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


class BaseJobSet(ABC):
Expand Down
4 changes: 1 addition & 3 deletions rope/contrib/autoimport/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Can extract names from source code of a python file, .so object, or builtin module.
"""

import ast
import inspect
import logging
import pathlib
Expand All @@ -20,8 +20,6 @@
PartialName,
Source,
)
from rope.base import ast


logger = logging.getLogger(__name__)

Expand Down
18 changes: 10 additions & 8 deletions rope/contrib/autoimport/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@

import re

from rope.base import builtins
from rope.base import exceptions
from rope.base import libutils
from rope.base import pynames
from rope.base import pyobjects
from rope.base import resources
from rope.base import resourceobserver
from rope.base import taskhandle
from rope.base import (
builtins,
exceptions,
libutils,
pynames,
pyobjects,
resourceobserver,
resources,
taskhandle,
)
from rope.refactor import importutils


Expand Down
7 changes: 6 additions & 1 deletion rope/contrib/autoimport/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
from pathlib import Path
from typing import Generator, Iterable, List, Optional, Set, Tuple, Iterator

from rope.base import exceptions, libutils, resourceobserver, taskhandle
from rope.base import (
exceptions,
libutils,
resourceobserver,
taskhandle,
)
from rope.base.project import Project
from rope.base.resources import Resource
from rope.contrib.autoimport.defs import (
Expand Down
20 changes: 11 additions & 9 deletions rope/contrib/codeassist.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

import rope.base.codeanalyze
import rope.base.evaluate
from rope.base import builtins
from rope.base import exceptions
from rope.base import libutils
from rope.base import pynames
from rope.base import pynamesdef
from rope.base import pyobjects
from rope.base import pyobjectsdef
from rope.base import pyscopes
from rope.base import worder
from rope.base import (
builtins,
exceptions,
libutils,
pynames,
pynamesdef,
pyobjects,
pyobjectsdef,
pyscopes,
worder,
)
from rope.contrib import fixsyntax
from rope.refactor import functionutils

Expand Down
2 changes: 1 addition & 1 deletion rope/contrib/findit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import rope.base.codeanalyze
import rope.base.evaluate
import rope.base.pyobjects
from rope.base import taskhandle, exceptions, worder
from rope.base import exceptions, taskhandle, worder
from rope.contrib import fixsyntax
from rope.refactor import occurrences

Expand Down
10 changes: 6 additions & 4 deletions rope/contrib/fixsyntax.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import rope.base.codeanalyze
import rope.base.evaluate
from rope.base import exceptions
from rope.base import libutils
from rope.base import utils
from rope.base import worder
from rope.base import (
exceptions,
libutils,
utils,
worder,
)
from rope.base.codeanalyze import ArrayLinesAdapter, LogicalLineFinder


Expand Down
11 changes: 9 additions & 2 deletions rope/contrib/generate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import rope.base.evaluate
from rope.base import libutils
from rope.base import change, pyobjects, exceptions, pynames, worder, codeanalyze
from rope.base import (
change,
codeanalyze,
exceptions,
libutils,
pynames,
pyobjects,
worder,
)
from rope.refactor import sourceutils, importutils, functionutils, suites


Expand Down
14 changes: 8 additions & 6 deletions rope/refactor/change_signature.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import copy

import rope.base.exceptions
from rope.base import codeanalyze
from rope.base import evaluate
from rope.base import pyobjects
from rope.base import taskhandle
from rope.base import utils
from rope.base import worder
from rope.base import (
codeanalyze,
evaluate,
pyobjects,
taskhandle,
utils,
worder,
)
from rope.base.change import ChangeContents, ChangeSet
from rope.refactor import occurrences, functionutils

Expand Down
Loading