Skip to content

Commit

Permalink
Merge pull request #283 from mcepl/247_isAlive-completion
Browse files Browse the repository at this point in the history
247 is alive completion
  • Loading branch information
mcepl committed Jan 30, 2020
2 parents d7890ee + 431d35d commit 67aeb33
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 32 deletions.
15 changes: 8 additions & 7 deletions rope/base/ast.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ast
from _ast import *
from __future__ import absolute_import
import ast
from ast import *

from rope.base import fscommands

Expand All @@ -18,7 +19,7 @@ def parse(source, filename='<string>'):
if not source.endswith(b'\n'):
source += b'\n'
try:
return compile(source, filename, 'exec', _ast.PyCF_ONLY_AST)
return ast.parse(source, filename='<unknown>')
except (TypeError, ValueError) as e:
error = SyntaxError()
error.lineno = 1
Expand All @@ -32,7 +33,7 @@ def walk(node, walker):
method_name = '_' + node.__class__.__name__
method = getattr(walker, method_name, None)
if method is not None:
if isinstance(node, _ast.ImportFrom) and node.module is None:
if isinstance(node, ast.ImportFrom) and node.module is None:
# In python < 2.7 ``node.module == ''`` for relative imports
# but for python 2.7 it is None. Generalizing it to ''.
node.module = ''
Expand All @@ -42,17 +43,17 @@ def walk(node, walker):


def get_child_nodes(node):
if isinstance(node, _ast.Module):
if isinstance(node, ast.Module):
return node.body
result = []
if node._fields is not None:
for name in node._fields:
child = getattr(node, name)
if isinstance(child, list):
for entry in child:
if isinstance(entry, _ast.AST):
if isinstance(entry, ast.AST):
result.append(entry)
if isinstance(child, _ast.AST):
if isinstance(child, ast.AST):
result.append(child)
return result

Expand Down
50 changes: 34 additions & 16 deletions rope/base/oi/type_hinting/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import rope.base.builtins
import logging
try:
from typing import Union, Optional
except ImportError:
pass
import rope.base.utils as base_utils
from rope.base.evaluate import ScopeNameFinder
from rope.base.exceptions import AttributeNotFoundError
from rope.base.pyobjects import PyClass, PyFunction
from rope.base.pyobjects import PyClass, PyDefinedObject, PyFunction, PyObject
from rope.base.utils import pycompat


Expand Down Expand Up @@ -66,33 +70,47 @@ def get_lineno_for_node(assign_node):

def get_mro(pyclass):
# FIXME: to use real mro() result
l = [pyclass]
for cls in l:
class_list = [pyclass]
for cls in class_list:
for super_cls in cls.get_superclasses():
if isinstance(super_cls, PyClass) and super_cls not in l:
l.append(super_cls)
return l
if isinstance(super_cls, PyClass) and super_cls not in class_list:
class_list.append(super_cls)
return class_list


def resolve_type(type_name, pyobject):
# type: (str, Union[PyDefinedObject, PyObject]) -> Optional[PyDefinedObject, PyObject]
"""
:type type_name: str
:type pyobject: rope.base.pyobjects.PyDefinedObject | rope.base.pyobjects.PyObject
:rtype: rope.base.pyobjects.PyDefinedObject | rope.base.pyobjects.PyObject or None
Find proper type object from its name.
"""
deprecated_aliases = {'collections': 'collections.abc'}
ret_type = None
logging.debug('Looking for %s', type_name)
if '.' not in type_name:
try:
return pyobject.get_module().get_scope().get_name(type_name).get_object()
except Exception:
pass
ret_type = pyobject.get_module().get_scope().get_name(
type_name).get_object()
except AttributeNotFoundError:
logging.exception('Cannot resolve type %s', type_name)
else:
mod_name, attr_name = type_name.rsplit('.', 1)
try:
mod_finder = ScopeNameFinder(pyobject.get_module())
mod = mod_finder._find_module(mod_name).get_object()
return mod.get_attribute(attr_name).get_object()
except Exception:
pass
ret_type = mod.get_attribute(attr_name).get_object()
except AttributeNotFoundError:
if mod_name in deprecated_aliases:
try:
logging.debug('Looking for %s in %s',
attr_name, deprecated_aliases[mod_name])
mod = mod_finder._find_module(
deprecated_aliases[mod_name]).get_object()
ret_type = mod.get_attribute(attr_name).get_object()
except AttributeNotFoundError:
logging.exception('Cannot resolve type %s in %s',
attr_name, dir(mod))
logging.debug('ret_type = %s', ret_type)
return ret_type


class ParametrizeType(object):
Expand Down
7 changes: 5 additions & 2 deletions rope/base/utils/datastructures.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# this snippet was taken from this link
# http://code.activestate.com/recipes/576694/

import collections
try:
from collections import MutableSet
except ImportError:
from collections.abc import MutableSet


class OrderedSet(collections.MutableSet):
class OrderedSet(MutableSet):

def __init__(self, iterable=None):
self.end = end = []
Expand Down
6 changes: 3 additions & 3 deletions rope/base/utils/pycompat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import _ast
import ast
# from rope.base import ast

PY2 = sys.version_info[0] == 2
Expand All @@ -15,7 +15,7 @@
str = str
string_types = (str,)
import builtins
ast_arg_type = _ast.arg
ast_arg_type = ast.arg

def execfile(fn, global_vars=None, local_vars=None):
with open(fn) as f:
Expand All @@ -34,7 +34,7 @@ def get_ast_with_items(node):

string_types = (basestring,)
builtins = __import__('__builtin__')
ast_arg_type = _ast.Name
ast_arg_type = ast.Name
execfile = execfile

def get_ast_arg_arg(node):
Expand Down
8 changes: 4 additions & 4 deletions ropetest/type_hinting_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,18 @@ def test_hint_parametrized_iterable(self):
+ self._make_class_hint('collections.Iterable[threading.Thread]') + \
' def a_method(self):\n' \
' for i in self.a_attr:\n' \
' i.isA'
' i.is_a'
result = self._assist(code)
self.assert_completion_in_result('isAlive', 'attribute', result)
self.assert_completion_in_result('is_alive', 'attribute', result)

def test_hint_parametrized_iterator(self):
code = 'class Sample(object):\n' \
+ self._make_class_hint('collections.Iterator[threading.Thread]') + \
' def a_method(self):\n' \
' for i in self.a_attr:\n' \
' i.isA'
' i.is_a'
result = self._assist(code)
self.assert_completion_in_result('isAlive', 'attribute', result)
self.assert_completion_in_result('is_alive', 'attribute', result)

def test_hint_parametrized_dict_key(self):
code = 'class Sample(object):\n' \
Expand Down

0 comments on commit 67aeb33

Please sign in to comment.