Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Github issue #319.
"""
s = dedent("""\
def static(func):
def wrapped(obj, *args):
return f(type(obj), *args)
return wrapped
class C(object):
@static
def test(cls):
return 10
C().test(""")
signatures = Script(s).call_signatures()
assert len(signatures) == 1
x = [p.description for p in signatures[0].params]
assert x == ['*args']
return ""
params = post_data[:idx].split(",")
if len(params) != 3:
print("jedi-server/complete: invalid post code=2")
return ""
line, column, filename = params
line, column = int(line), int(column)
source = post_data[idx + 2:]
print("jedi-server/complete: ", line, column, filename)
# do completion
def print_candidate(type_, typed_text, arguement):
# row format: type$typed_text$arguemnt
return "{0}${1}${2}\n".format(type_, typed_text, arguement)
script = jedi.Script(source, line, column)
completions = script.completions()
# output compltion results
result = ""
for c in completions:
if c.type in ["function", "class"]:
if hasattr(c, "params"): # c is callable
sig = "({0})".format(
", ".join(map(lambda x: x.description, c.params)))
result += print_candidate(c.type, c.name, sig)
else:
result += print_candidate(c.type, c.name, "")
elif c.type in ["statement", "instance", "module", "import", "param"]:
result += print_candidate(c.type, c.name, "")
elif c.type == "keyword":
pass # ignore keyword
else:
def get_script(self, source, line, col, filename, environment):
return jedi.Script(source, line, col, filename, environment=self._env)
if self.workspace.is_local():
sys_path.insert(0, self.workspace.root)
if os.path.exists(document.path):
path = document.path
kwargs = {
'source': document.source,
'path': path,
'sys_path': sys_path
}
if position:
kwargs['line'] = position['line'] + 1
kwargs['column'] = position['character']
return jedi.Script(**kwargs)
'''
def _get_indent(text):
'''int: The leading indentation of some string.'''
return len(text) - len(text.lstrip())
indent = _get_indent(lines[-1])
# We need to insert our fake line ONE line above the last line
fake_line = '{indent}{name}\n'.format(indent=(' ' * indent), name=name)
fake_lines = lines[:-1] + [fake_line] + [lines[-1]]
row = len(fake_lines) - 1 # This is the row of our 'fake_line'
column = len(fake_line) - 1
code = ''.join(fake_lines)
new_script = jedi.Script(code, row, column)
for completion in new_script.completions():
if completion.name == name:
return name
return fallback
def getJediScript(code, srcPath):
"""Provides the jedi Script object considering the current project"""
if not os.path.isabs(srcPath):
# Pretend it is in the user home dir
srcPath = os.path.realpath(QDir.homePath()) + os.path.sep + srcPath
return jedi.Script(code=code, path=srcPath, project=getJediProject())
if path.startswith('--'):
continue
if isdir(path):
import fnmatch
import os
paths = []
for root, dirnames, filenames in os.walk(path):
for filename in fnmatch.filter(filenames, '*.py'):
paths.append(os.path.join(root, filename))
else:
paths = [path]
try:
for path in paths:
for error in jedi.Script(path=path)._analysis():
print(error)
except Exception:
if '--pdb' in sys.argv:
import pdb
pdb.post_mortem()
else:
raise
def _GetJediScript( self, request_data ):
path = request_data[ 'filepath' ]
source = request_data[ 'file_data' ][ path ][ 'contents' ]
line = request_data[ 'line_num' ]
# Jedi expects columns to start at 0, not 1, and for them to be Unicode
# codepoint offsets.
column = request_data[ 'start_codepoint' ] - 1
environment = self._EnvironmentForRequest( request_data )
sys_path = self._SysPathForFile( request_data, environment )
return jedi.Script( source,
line,
column,
path,
sys_path = sys_path,
environment = environment )
def _search_func(self, string, complete=False, all_scopes=False):
# Using a Script is they easiest way to get an empty module context.
from jedi import Script
s = Script('', project=self)
inference_state = s._inference_state
empty_module_context = s._get_module_context()
if inference_state.grammar.version_info < (3, 6) or sys.version_info < (3, 6):
raise NotImplementedError(
"No support for refactorings/search on Python 2/3.5"
)
debug.dbg('Search for string %s, complete=%s', string, complete)
wanted_type, wanted_names = split_search_string(string)
name = wanted_names[0]
stub_folder_name = name + '-stubs'
ios = recurse_find_python_folders_and_files(FolderIO(self._path))
file_ios = []
# 1. Search for modules in the current project