mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 14:34:31 +08:00
Fix issues with simple_getitem and mixed objects
This commit is contained in:
@@ -9,6 +9,7 @@ from jedi._compatibility import unicode, is_py3, builtins, \
|
|||||||
py_version, force_unicode
|
py_version, force_unicode
|
||||||
from jedi.evaluate.compiled.getattr_static import getattr_static
|
from jedi.evaluate.compiled.getattr_static import getattr_static
|
||||||
|
|
||||||
|
ALLOWED_GETITEM_TYPES = (str, list, tuple, unicode, bytes, bytearray, dict)
|
||||||
|
|
||||||
MethodDescriptorType = type(str.replace)
|
MethodDescriptorType = type(str.replace)
|
||||||
# These are not considered classes and access is granted even though they have
|
# These are not considered classes and access is granted even though they have
|
||||||
@@ -226,7 +227,7 @@ class DirectObjectAccess(object):
|
|||||||
return self.py__iter__list()
|
return self.py__iter__list()
|
||||||
|
|
||||||
def py__simple_getitem__(self, index):
|
def py__simple_getitem__(self, index):
|
||||||
if type(self._obj) not in (str, list, tuple, unicode, bytes, bytearray, dict):
|
if type(self._obj) not in ALLOWED_GETITEM_TYPES:
|
||||||
# Get rid of side effects, we won't call custom `__getitem__`s.
|
# Get rid of side effects, we won't call custom `__getitem__`s.
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -236,7 +237,7 @@ class DirectObjectAccess(object):
|
|||||||
if not hasattr(self._obj, '__getitem__'):
|
if not hasattr(self._obj, '__getitem__'):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if type(self._obj) not in (str, list, tuple, unicode, bytes, bytearray, dict):
|
if type(self._obj) not in ALLOWED_GETITEM_TYPES:
|
||||||
# Get rid of side effects, we won't call custom `__getitem__`s.
|
# Get rid of side effects, we won't call custom `__getitem__`s.
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|||||||
@@ -12,10 +12,12 @@ from jedi.evaluate import compiled
|
|||||||
from jedi.cache import underscore_memoization
|
from jedi.cache import underscore_memoization
|
||||||
from jedi.file_io import FileIO
|
from jedi.file_io import FileIO
|
||||||
from jedi.evaluate.base_context import ContextSet, ContextWrapper
|
from jedi.evaluate.base_context import ContextSet, ContextWrapper
|
||||||
|
from jedi.evaluate.helpers import SimpleGetItemNotFound
|
||||||
from jedi.evaluate.context import ModuleContext
|
from jedi.evaluate.context import ModuleContext
|
||||||
from jedi.evaluate.cache import evaluator_function_cache
|
from jedi.evaluate.cache import evaluator_function_cache
|
||||||
from jedi.evaluate.compiled.getattr_static import getattr_static
|
from jedi.evaluate.compiled.getattr_static import getattr_static
|
||||||
from jedi.evaluate.compiled.access import compiled_objects_cache
|
from jedi.evaluate.compiled.access import compiled_objects_cache, \
|
||||||
|
ALLOWED_GETITEM_TYPES
|
||||||
from jedi.evaluate.compiled.context import create_cached_compiled_object
|
from jedi.evaluate.compiled.context import create_cached_compiled_object
|
||||||
from jedi.evaluate.gradual.conversion import to_stub
|
from jedi.evaluate.gradual.conversion import to_stub
|
||||||
|
|
||||||
@@ -56,6 +58,12 @@ class MixedObject(ContextWrapper):
|
|||||||
else:
|
else:
|
||||||
return self.compiled_object.get_safe_value(default)
|
return self.compiled_object.get_safe_value(default)
|
||||||
|
|
||||||
|
def py__simple_getitem__(self, index):
|
||||||
|
python_object = self.compiled_object.access_handle.access._obj
|
||||||
|
if type(python_object) in ALLOWED_GETITEM_TYPES:
|
||||||
|
return self.compiled_object.py__simple_getitem__(index)
|
||||||
|
raise SimpleGetItemNotFound
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<%s: %s>' % (
|
return '<%s: %s>' % (
|
||||||
type(self).__name__,
|
type(self).__name__,
|
||||||
|
|||||||
@@ -176,7 +176,6 @@ def test_getattr():
|
|||||||
_assert_interpreter_complete('getattr(Foo1, baz).app', locals(), ['append'])
|
_assert_interpreter_complete('getattr(Foo1, baz).app', locals(), ['append'])
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.xfail(reason='For now slicing on strings is not supported for mixed objects')
|
|
||||||
def test_slice():
|
def test_slice():
|
||||||
class Foo1:
|
class Foo1:
|
||||||
bar = []
|
bar = []
|
||||||
|
|||||||
Reference in New Issue
Block a user