forked from VimPlug/jedi
Compare commits
5
Commits
3102215478
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ed47f89162 | ||
|
|
112d255414 | ||
|
|
5c37b736a3 | ||
|
|
19236b472b | ||
|
|
ef5d361121 |
@@ -66,6 +66,9 @@ Code Contributors
|
||||
- Martin Vielsmaier (@moser) <martin@vielsmaier.net>
|
||||
- TingJia Wu (@WutingjiaX) <wutingjia@bytedance.com>
|
||||
- Nguyễn Hồng Quân <ng.hong.quan@gmail.com>
|
||||
- haoran3160-afk (@haoran3160-afk)
|
||||
- Eric3-jp (@Eric3-jp) (with OpenAI Codex assistance)
|
||||
- Dhanraj E (@Dhanu-dynamic) <dhanraj@labs.digiscrypt.com>
|
||||
|
||||
And a few more "anonymous" contributors.
|
||||
|
||||
|
||||
@@ -502,6 +502,8 @@ class Script:
|
||||
tree_name = name.tree_name
|
||||
if tree_name is not None: # Happens with lambdas.
|
||||
scope = tree_name.get_definition()
|
||||
if scope.parent.type in ('async_stmt', 'async_funcdef'):
|
||||
scope = scope.parent
|
||||
if scope.start_pos[1] < column:
|
||||
break
|
||||
definition = definition.parent()
|
||||
|
||||
+2
-2
@@ -551,7 +551,7 @@ class BaseName:
|
||||
return ''.join(lines[start_index:index + after + 1])
|
||||
|
||||
def _get_signatures(self, for_docstring=False):
|
||||
if self._name.api_type == 'property':
|
||||
if self._name.api_type in ('property', 'param'):
|
||||
return []
|
||||
if for_docstring and self._name.api_type == 'statement' and not self.is_stub():
|
||||
# For docstrings we don't resolve signatures if they are simple
|
||||
@@ -831,7 +831,7 @@ class Signature(BaseSignature):
|
||||
def index(self):
|
||||
"""
|
||||
Returns the param index of the current cursor position.
|
||||
Returns None if the index cannot be found in the curent call.
|
||||
Returns None if the index cannot be found in the current call.
|
||||
|
||||
:rtype: int
|
||||
"""
|
||||
|
||||
+15
-2
@@ -63,6 +63,9 @@ class Project:
|
||||
Additionally there are functions to search a whole project.
|
||||
"""
|
||||
_environment = None
|
||||
# Set for projects loaded from a ``.jedi/project.json``. That file can be
|
||||
# part of a checked out repository, so it is not trusted to run binaries.
|
||||
_loaded_from_file = False
|
||||
|
||||
@staticmethod
|
||||
def _get_config_folder_path(base_path):
|
||||
@@ -86,7 +89,13 @@ class Project:
|
||||
version, data = json.load(f)
|
||||
|
||||
if version == 1:
|
||||
return cls(**data)
|
||||
# A code base must not be able to enable the loading of its own
|
||||
# extensions, see the docs about security.
|
||||
if data.pop('load_unsafe_extensions', False):
|
||||
debug.warning('load_unsafe_extensions is ignored for loaded projects')
|
||||
project = cls(**data)
|
||||
project._loaded_from_file = True
|
||||
return project
|
||||
else:
|
||||
raise WrongVersion(
|
||||
"The Jedi version of this project seems newer than what we can handle."
|
||||
@@ -98,6 +107,7 @@ class Project:
|
||||
"""
|
||||
data = dict(self.__dict__)
|
||||
data.pop('_environment', None)
|
||||
data.pop('_loaded_from_file', None)
|
||||
data.pop('_django', None) # TODO make django setting public?
|
||||
data = {k.lstrip('_'): v for k, v in data.items()}
|
||||
data['path'] = str(data['path'])
|
||||
@@ -242,7 +252,10 @@ class Project:
|
||||
def get_environment(self):
|
||||
if self._environment is None:
|
||||
if self._environment_path is not None:
|
||||
self._environment = create_environment(self._environment_path, safe=False)
|
||||
# An environment path from a loaded project file is checked
|
||||
# like a scanned virtualenv, see find_virtualenvs.
|
||||
self._environment = create_environment(
|
||||
self._environment_path, safe=self._loaded_from_file)
|
||||
else:
|
||||
self._environment = get_cached_default_environment()
|
||||
return self._environment
|
||||
|
||||
@@ -125,7 +125,7 @@ class InferenceState:
|
||||
debug.dbg('execute result: %s in %s', value_set, value)
|
||||
return value_set
|
||||
|
||||
# mypy doesn't suppport decorated propeties (https://github.com/python/mypy/issues/1362)
|
||||
# mypy doesn't support decorated properties (https://github.com/python/mypy/issues/1362)
|
||||
@property
|
||||
@inference_state_function_cache()
|
||||
def builtins_module(self):
|
||||
|
||||
@@ -110,7 +110,7 @@ def _check_for_setattr(instance):
|
||||
|
||||
def add_attribute_error(name_context, lookup_value, name):
|
||||
message = ('AttributeError: %s has no attribute %s.' % (lookup_value, name))
|
||||
# Check for __getattr__/__getattribute__ existance and issue a warning
|
||||
# Check for __getattr__/__getattribute__ existence and issue a warning
|
||||
# instead of an error, if that happens.
|
||||
typ = Error
|
||||
if lookup_value.is_instance() and not lookup_value.is_compiled():
|
||||
|
||||
@@ -170,6 +170,8 @@ class AbstractTreeName(AbstractNameDefinition):
|
||||
trailer = par.parent
|
||||
if trailer.type == 'arglist':
|
||||
trailer = trailer.parent
|
||||
if trailer.type == 'error_node':
|
||||
return []
|
||||
if trailer.type != 'classdef':
|
||||
if trailer.type == 'decorator':
|
||||
value_set = context.infer_node(trailer.children[1])
|
||||
|
||||
@@ -224,7 +224,7 @@ class _BaseTreeInstance(AbstractInstanceValue):
|
||||
elif isinstance(f, CompiledValueFilter):
|
||||
yield CompiledInstanceClassFilter(self, f)
|
||||
else:
|
||||
# Propably from the metaclass.
|
||||
# Probably from the metaclass.
|
||||
yield f
|
||||
|
||||
@inference_state_method_cache()
|
||||
|
||||
@@ -231,6 +231,19 @@ def test_goto_follow_imports(Script):
|
||||
assert d.name == 'a'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('prefix', ['', 'def foo(bar): pass\n', 'def f():\n '])
|
||||
@pytest.mark.parametrize('call', ['foo(bar=1', 'foo(bar=1,', 'foo(foo(bar=1'])
|
||||
def test_goto_incomplete_named_argument(Script, prefix, call):
|
||||
code = prefix + call
|
||||
column = code.splitlines()[-1].index('bar')
|
||||
assert Script(code).goto(column=column) == []
|
||||
|
||||
|
||||
def test_goto_named_argument_in_complete_inner_call(Script):
|
||||
param, = Script('def foo(bar): pass\nfoo(foo(bar=1)').goto(2, 8)
|
||||
assert (param.name, param.line, param.column) == ('bar', 1, 8)
|
||||
|
||||
|
||||
def test_goto_module(Script):
|
||||
def check(line, expected, follow_imports=False):
|
||||
script = Script(path=path)
|
||||
|
||||
@@ -462,3 +462,21 @@ def test_module_completions(Script, module):
|
||||
|
||||
def test_whitespace_at_end_after_dot(Script):
|
||||
assert 'strip' in [c.name for c in Script('str. ').complete()]
|
||||
|
||||
|
||||
def test_param_docstring_in_completion(Script):
|
||||
# From Github #2063
|
||||
code = '''\
|
||||
from typing import Optional
|
||||
|
||||
def func(arg: Optional[int]):
|
||||
pass
|
||||
|
||||
func(arg'''
|
||||
script = Script(code=code)
|
||||
completions = script.complete(
|
||||
line=len(code.splitlines()),
|
||||
column=len(code.splitlines()[-1])
|
||||
)
|
||||
c, = completions
|
||||
assert c.docstring() == ''
|
||||
|
||||
@@ -53,6 +53,21 @@ def x():
|
||||
]
|
||||
'''
|
||||
|
||||
async_code = '''\
|
||||
async def coro():
|
||||
return None
|
||||
'''
|
||||
async_method = '''\
|
||||
class C:
|
||||
async def coro(self):
|
||||
return None
|
||||
'''
|
||||
async_nested = '''\
|
||||
async def outer():
|
||||
async def inner():
|
||||
return None
|
||||
'''
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'code, line, column, full_name, expected_parents', [
|
||||
@@ -106,6 +121,21 @@ def x():
|
||||
(with_brackets, 3, None, 'myfile', []),
|
||||
(with_brackets, 4, 4, 'myfile.x', ['x']),
|
||||
(with_brackets, 4, 5, 'myfile.x', ['x']),
|
||||
|
||||
(async_code, 2, 0, 'myfile', []),
|
||||
(async_code, 2, 4, 'myfile.coro', ['coro']),
|
||||
(async_code, 2, 5, 'myfile.coro', ['coro']),
|
||||
(async_code, 2, None, 'myfile.coro', ['coro']),
|
||||
('@decorator\n' + async_code, 3, 0, 'myfile', []),
|
||||
('@decorator\n' + async_code, 3, 4, 'myfile.coro', ['coro']),
|
||||
('@decorator\n' + async_code, 3, 5, 'myfile.coro', ['coro']),
|
||||
(async_method, 3, 4, 'myfile.C', ['C']),
|
||||
(async_method, 3, 8, 'myfile.C.coro', ['C', 'coro']),
|
||||
(async_method, 3, 9, 'myfile.C.coro', ['C', 'coro']),
|
||||
(async_nested, 3, 0, 'myfile', []),
|
||||
(async_nested, 3, 4, 'myfile.outer', ['outer']),
|
||||
(async_nested, 3, 8, None, ['outer', 'inner']),
|
||||
(async_nested, 3, 9, None, ['outer', 'inner']),
|
||||
]
|
||||
)
|
||||
def test_context(Script, code, line, column, full_name, expected_parents):
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
@@ -6,6 +7,7 @@ import pytest
|
||||
from ..helpers import get_example_dir, set_cwd, root_dir, test_dir
|
||||
from jedi import Interpreter
|
||||
from jedi.api import Project, get_default_project
|
||||
from jedi.api.environment import InvalidPythonEnvironment
|
||||
from jedi.api.project import _is_potential_project, _CONTAINS_POTENTIAL_PROJECT
|
||||
|
||||
|
||||
@@ -54,6 +56,33 @@ def test_load_save_project(tmpdir):
|
||||
assert loaded.added_sys_path == ['/foo']
|
||||
|
||||
|
||||
def test_load_project_checks_environment_path(tmpdir, monkeypatch):
|
||||
# The project file can be part of a checked out repository, so the binary
|
||||
# it points to has to pass the same check as in find_virtualenvs.
|
||||
monkeypatch.setattr('jedi.api.environment._is_safe', lambda executable_path: False)
|
||||
|
||||
def _get_subprocess(self):
|
||||
raise RuntimeError('Should not get called!')
|
||||
|
||||
monkeypatch.setattr('jedi.api.environment.Environment._get_subprocess',
|
||||
_get_subprocess)
|
||||
|
||||
Project(tmpdir.strpath, environment_path=sys.executable).save()
|
||||
with pytest.raises(InvalidPythonEnvironment):
|
||||
Project.load(tmpdir.strpath).get_environment()
|
||||
|
||||
|
||||
def test_load_project_safe_environment_path(tmpdir):
|
||||
Project(tmpdir.strpath, environment_path=sys.executable).save()
|
||||
environment = Project.load(tmpdir.strpath).get_environment()
|
||||
assert environment.executable == sys.executable
|
||||
|
||||
|
||||
def test_load_project_ignores_unsafe_extensions(tmpdir):
|
||||
Project(tmpdir.strpath, load_unsafe_extensions=True).save()
|
||||
assert Project.load(tmpdir.strpath).load_unsafe_extensions is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'string, full_names, kwargs', [
|
||||
('test_load_save_project', ['test_api.test_project.test_load_save_project'], {}),
|
||||
|
||||
Reference in New Issue
Block a user