| 1 |
""" |
| 2 |
Test all things related to the ``jedi.api`` module. |
| 3 |
""" |
| 4 |
|
| 5 |
import os |
| 6 |
from textwrap import dedent |
| 7 |
|
| 8 |
import pytest |
| 9 |
from pytest import raises |
| 10 |
from parso import cache |
| 11 |
|
| 12 |
from jedi import preload_module |
| 13 |
from jedi.inference.gradual import typeshed |
| 14 |
from test.helpers import test_dir, get_example_dir |
| 15 |
|
| 16 |
|
| 17 |
def test_preload_modules(): |
| 18 |
def check_loaded(*module_names): |
| 19 |
for grammar_cache in cache.parser_cache.values(): |
| 20 |
if None in grammar_cache: |
| 21 |
break |
| 22 |
|
| 23 |
typeshed_cache_count = sum( |
| 24 |
1 for path in grammar_cache |
| 25 |
if path is not None and str(path).startswith(str(typeshed.TYPESHED_PATH)) |
| 26 |
) |
| 27 |
|
| 28 |
assert len(grammar_cache) - typeshed_cache_count == len(module_names) + 1 |
| 29 |
for i in module_names: |
| 30 |
assert [i in str(k) for k in grammar_cache.keys() if k is not None] |
| 31 |
|
| 32 |
old_cache = cache.parser_cache.copy() |
| 33 |
cache.parser_cache.clear() |
| 34 |
|
| 35 |
try: |
| 36 |
preload_module('sys') |
| 37 |
check_loaded() |
| 38 |
preload_module('types', 'token') |
| 39 |
check_loaded('types', 'token') |
| 40 |
finally: |
| 41 |
cache.parser_cache.update(old_cache) |
| 42 |
|
| 43 |
|
| 44 |
def test_empty_script(Script): |
| 45 |
assert Script('') |
| 46 |
|
| 47 |
|
| 48 |
def test_line_number_errors(Script): |
| 49 |
""" |
| 50 |
Script should raise a ValueError if line/column numbers are not in a |
| 51 |
valid range. |
| 52 |
""" |
| 53 |
s = 'hello' |
| 54 |
|
| 55 |
with raises(ValueError): |
| 56 |
Script(s).complete(2, 0) |
| 57 |
with raises(ValueError): |
| 58 |
Script(s).complete(0, 0) |
| 59 |
|
| 60 |
|
| 61 |
with raises(ValueError): |
| 62 |
Script(s).infer(1, len(s) + 1) |
| 63 |
with raises(ValueError): |
| 64 |
Script(s).goto(1, -1) |
| 65 |
|
| 66 |
|
| 67 |
Script(s).get_signatures(1, 0) |
| 68 |
Script(s).get_references(1, len(s)) |
| 69 |
|
| 70 |
|
| 71 |
def _check_number(Script, source, result='float'): |
| 72 |
completions = Script(source).complete() |
| 73 |
assert completions[0].parent().name == result |
| 74 |
|
| 75 |
|
| 76 |
def test_completion_on_number_literals(Script): |
| 77 |
|
| 78 |
assert [c.name for c in Script('1. ').complete()] \ |
| 79 |
== ['and', 'if', 'in', 'is', 'not', 'or'] |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
_check_number(Script, '1..') |
| 84 |
_check_number(Script, '1.0.') |
| 85 |
|
| 86 |
|
| 87 |
_check_number(Script, '1.e14.') |
| 88 |
_check_number(Script, '1.e-3.') |
| 89 |
_check_number(Script, '9e3.') |
| 90 |
assert Script('1.e3..').complete() == [] |
| 91 |
assert Script('1.e-13..').complete() == [] |
| 92 |
|
| 93 |
|
| 94 |
def test_completion_on_hex_literals(Script): |
| 95 |
assert Script('0x1..').complete() == [] |
| 96 |
_check_number(Script, '0x1.', 'int') |
| 97 |
|
| 98 |
|
| 99 |
assert Script('0b2.b').complete() == [] |
| 100 |
_check_number(Script, '0b1.', 'int') |
| 101 |
|
| 102 |
_check_number(Script, '0x2e.', 'int') |
| 103 |
_check_number(Script, '0xE7.', 'int') |
| 104 |
_check_number(Script, '0xEa.', 'int') |
| 105 |
|
| 106 |
assert Script('0x.').complete() == [] |
| 107 |
|
| 108 |
|
| 109 |
def test_completion_on_complex_literals(Script): |
| 110 |
assert Script('1j..').complete() == [] |
| 111 |
_check_number(Script, '1j.', 'complex') |
| 112 |
_check_number(Script, '44.j.', 'complex') |
| 113 |
_check_number(Script, '4.0j.', 'complex') |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
assert not Script('4j').complete() |
| 119 |
assert ({c.name for c in Script('4j ').complete()} |
| 120 |
== {'if', 'and', 'in', 'is', 'not', 'or'}) |
| 121 |
|
| 122 |
|
| 123 |
def test_goto_non_name(Script, environment): |
| 124 |
assert Script('for').goto() == [] |
| 125 |
|
| 126 |
assert Script('assert').goto() == [] |
| 127 |
assert Script('True').goto() == [] |
| 128 |
|
| 129 |
|
| 130 |
def test_infer_on_non_name(Script): |
| 131 |
assert Script('import x').infer(column=0) == [] |
| 132 |
|
| 133 |
|
| 134 |
def test_infer_on_generator(Script, environment): |
| 135 |
script = Script('def x(): yield 1\ny=x()\ny') |
| 136 |
def_, = script.infer() |
| 137 |
assert def_.name == 'GeneratorType' |
| 138 |
def_, = script.infer(only_stubs=True) |
| 139 |
assert def_.name == 'Generator' |
| 140 |
|
| 141 |
|
| 142 |
def test_goto_definition_not_multiple(Script): |
| 143 |
""" |
| 144 |
There should be only one result if it leads back to the same |
| 145 |
origin (e.g. instance method) |
| 146 |
""" |
| 147 |
|
| 148 |
s = dedent('''\ |
| 149 |
import random |
| 150 |
class A(): |
| 151 |
def __init__(self, a): |
| 152 |
self.a = 3 |
| 153 |
|
| 154 |
def foo(self): |
| 155 |
pass |
| 156 |
|
| 157 |
if random.randint(0, 1): |
| 158 |
a = A(2) |
| 159 |
else: |
| 160 |
a = A(1) |
| 161 |
a''') |
| 162 |
assert len(Script(s).infer()) == 1 |
| 163 |
|
| 164 |
|
| 165 |
def test_reference_description(Script): |
| 166 |
descs = [u.description for u in Script("foo = ''; foo").get_references()] |
| 167 |
assert set(descs) == {"foo = ''", 'foo'} |
| 168 |
|
| 169 |
|
| 170 |
def test_get_line_code(Script): |
| 171 |
def get_line_code(source, line=None, **kwargs): |
| 172 |
|
| 173 |
return Script(source).complete(line=line)[0].get_line_code(**kwargs).replace('\r', '') |
| 174 |
|
| 175 |
|
| 176 |
assert get_line_code('abs') == 'def abs(x: SupportsAbs[_T], /) -> _T: ...\n' |
| 177 |
|
| 178 |
|
| 179 |
first_line = 'def foo():\n' |
| 180 |
line = ' foo' |
| 181 |
code = first_line + line |
| 182 |
assert get_line_code(code) == first_line |
| 183 |
|
| 184 |
|
| 185 |
code = code + '\nother_line' |
| 186 |
assert get_line_code(code, line=2) == first_line |
| 187 |
assert get_line_code(code, line=2, after=1) == first_line + line + '\n' |
| 188 |
assert get_line_code(code, line=2, after=2, before=1) == code |
| 189 |
|
| 190 |
|
| 191 |
assert get_line_code(code, line=2, after=3, before=3) == code |
| 192 |
|
| 193 |
|
| 194 |
def test_get_line_code_on_builtin(Script, disable_typeshed): |
| 195 |
abs_ = Script('abs').complete()[0] |
| 196 |
assert abs_.name == 'abs' |
| 197 |
assert abs_.get_line_code() == '' |
| 198 |
assert abs_.line is None |
| 199 |
|
| 200 |
|
| 201 |
def test_goto_follow_imports(Script): |
| 202 |
code = dedent(""" |
| 203 |
import inspect |
| 204 |
inspect.isfunction""") |
| 205 |
definition, = Script(code).goto(column=0, follow_imports=True) |
| 206 |
assert definition.module_path.name == 'inspect.py' |
| 207 |
assert (definition.line, definition.column) == (1, 0) |
| 208 |
|
| 209 |
definition, = Script(code).goto(follow_imports=True) |
| 210 |
assert definition.module_path.name == 'inspect.py' |
| 211 |
assert (definition.line, definition.column) > (1, 0) |
| 212 |
|
| 213 |
code = '''def param(p): pass\nparam(1)''' |
| 214 |
start_pos = 1, len('def param(') |
| 215 |
|
| 216 |
script = Script(code) |
| 217 |
definition, = script.goto(*start_pos, follow_imports=True) |
| 218 |
assert (definition.line, definition.column) == start_pos |
| 219 |
assert definition.name == 'p' |
| 220 |
result, = definition.goto() |
| 221 |
assert result.name == 'p' |
| 222 |
result, = definition.infer() |
| 223 |
assert result.name == 'int' |
| 224 |
result, = result.infer() |
| 225 |
assert result.name == 'int' |
| 226 |
|
| 227 |
definition, = script.goto(*start_pos) |
| 228 |
assert (definition.line, definition.column) == start_pos |
| 229 |
|
| 230 |
d, = Script('a = 1\na').goto(follow_imports=True) |
| 231 |
assert d.name == 'a' |
| 232 |
|
| 233 |
|
| 234 |
def test_goto_module(Script): |
| 235 |
def check(line, expected, follow_imports=False): |
| 236 |
script = Script(path=path) |
| 237 |
module, = script.goto(line=line, follow_imports=follow_imports) |
| 238 |
assert module.module_path == expected |
| 239 |
|
| 240 |
base_path = get_example_dir('simple_import') |
| 241 |
path = base_path.joinpath('__init__.py') |
| 242 |
|
| 243 |
check(1, base_path.joinpath('module.py')) |
| 244 |
check(1, base_path.joinpath('module.py'), follow_imports=True) |
| 245 |
check(5, base_path.joinpath('module2.py')) |
| 246 |
|
| 247 |
|
| 248 |
def test_goto_definition_cursor(Script): |
| 249 |
|
| 250 |
s = ("class A():\n" |
| 251 |
" def _something(self):\n" |
| 252 |
" return\n" |
| 253 |
" def different_line(self,\n" |
| 254 |
" b):\n" |
| 255 |
" return\n" |
| 256 |
"A._something\n" |
| 257 |
"A.different_line" |
| 258 |
) |
| 259 |
|
| 260 |
in_name = 2, 9 |
| 261 |
under_score = 2, 8 |
| 262 |
cls = 2, 7 |
| 263 |
should1 = 7, 10 |
| 264 |
diff_line = 4, 10 |
| 265 |
should2 = 8, 10 |
| 266 |
|
| 267 |
def get_def(pos): |
| 268 |
return [d.description for d in Script(s).infer(*pos)] |
| 269 |
|
| 270 |
in_name = get_def(in_name) |
| 271 |
under_score = get_def(under_score) |
| 272 |
should1 = get_def(should1) |
| 273 |
should2 = get_def(should2) |
| 274 |
|
| 275 |
diff_line = get_def(diff_line) |
| 276 |
|
| 277 |
assert should1 == in_name |
| 278 |
assert should1 == under_score |
| 279 |
|
| 280 |
assert should2 == diff_line |
| 281 |
|
| 282 |
assert get_def(cls) == [] |
| 283 |
|
| 284 |
|
| 285 |
def test_no_statement_parent(Script): |
| 286 |
source = dedent(""" |
| 287 |
def f(): |
| 288 |
pass |
| 289 |
|
| 290 |
class C: |
| 291 |
pass |
| 292 |
|
| 293 |
variable = f if random.choice([0, 1]) else C""") |
| 294 |
defs = Script(source).infer(column=3) |
| 295 |
defs = sorted(defs, key=lambda d: d.line) |
| 296 |
assert [d.description for d in defs] == ['def f', 'class C'] |
| 297 |
|
| 298 |
|
| 299 |
def test_backslash_continuation_and_bracket(Script): |
| 300 |
code = dedent(r""" |
| 301 |
x = 0 |
| 302 |
a = \ |
| 303 |
[1, 2, 3, (x)]""") |
| 304 |
|
| 305 |
lines = code.splitlines() |
| 306 |
column = lines[-1].index('(') |
| 307 |
def_, = Script(code).infer(line=len(lines), column=column) |
| 308 |
assert def_.name == 'int' |
| 309 |
|
| 310 |
|
| 311 |
def test_goto_follow_builtin_imports(Script): |
| 312 |
s = Script('import sys; sys') |
| 313 |
d, = s.goto(follow_imports=True) |
| 314 |
assert d.in_builtin_module() is True |
| 315 |
d, = s.goto(follow_imports=True, follow_builtin_imports=True) |
| 316 |
assert d.in_builtin_module() is True |
| 317 |
|
| 318 |
|
| 319 |
def test_docstrings_for_completions(Script): |
| 320 |
for c in Script('').complete(): |
| 321 |
assert isinstance(c.docstring(), str) |
| 322 |
|
| 323 |
|
| 324 |
def test_completions_order_most_resemblance_on_top(Script): |
| 325 |
"""Test that the completion which resembles the in-typing the most will come first.""" |
| 326 |
code = "from pathlib import Path\npath = Path('hello.txt')\n\npat" |
| 327 |
script = Script(code) |
| 328 |
|
| 329 |
assert ['path', 'Path'] == [comp.name for comp in script.complete()] |
| 330 |
|
| 331 |
|
| 332 |
def test_fuzzy_completion(Script): |
| 333 |
script = Script('string = "hello"\nstring.upper') |
| 334 |
|
| 335 |
assert ['upper', |
| 336 |
'isupper'] == [comp.name for comp in script.complete(fuzzy=True)] |
| 337 |
|
| 338 |
|
| 339 |
def test_math_fuzzy_completion(Script, environment): |
| 340 |
script = Script('import math\nmath.og') |
| 341 |
expected = ['copysign', 'log', 'log10', 'log1p', 'log2'] |
| 342 |
completions = script.complete(fuzzy=True) |
| 343 |
assert expected == [comp.name for comp in completions] |
| 344 |
for c in completions: |
| 345 |
assert c.complete is None |
| 346 |
|
| 347 |
|
| 348 |
def test_file_fuzzy_completion(Script): |
| 349 |
path = os.path.join(test_dir, 'completion') |
| 350 |
script = Script('"{}/ep08_i'.format(path)) |
| 351 |
expected = [ |
| 352 |
'pep0484_basic.py"', |
| 353 |
'pep0484_generic_mismatches.py"', |
| 354 |
'pep0484_generic_parameters.py"', |
| 355 |
'pep0484_generic_passthroughs.py"', |
| 356 |
'pep0484_typing.py"', |
| 357 |
] |
| 358 |
assert expected == [comp.name for comp in script.complete(fuzzy=True)] |
| 359 |
|
| 360 |
|
| 361 |
@pytest.mark.parametrize( |
| 362 |
'code, column', [ |
| 363 |
('"foo"', 0), |
| 364 |
('"foo"', 3), |
| 365 |
('"foo"', None), |
| 366 |
('"""foo"""', 5), |
| 367 |
('"""foo"""', 1), |
| 368 |
('"""foo"""', 2), |
| 369 |
] |
| 370 |
) |
| 371 |
def test_goto_on_string(Script, code, column): |
| 372 |
script = Script(code) |
| 373 |
assert not script.infer(column=column) |
| 374 |
assert not script.goto(column=column) |
| 375 |
|
| 376 |
|
| 377 |
def test_multi_goto(Script): |
| 378 |
script = Script('x = 1\ny = 1.0\nx\ny') |
| 379 |
x, = script.goto(line=3) |
| 380 |
y, = script.goto(line=4) |
| 381 |
assert x.line == 1 |
| 382 |
assert y.line == 2 |
| 383 |
|
| 384 |
|
| 385 |
@pytest.mark.parametrize( |
| 386 |
'code, column, expected', [ |
| 387 |
('str() ', 3, 'str'), |
| 388 |
('str() ', 4, 'str'), |
| 389 |
('str() ', 5, 'str'), |
| 390 |
('str() ', 6, None), |
| 391 |
('str( ) ', 6, None), |
| 392 |
(' 1', 1, None), |
| 393 |
('str(1) ', 3, 'str'), |
| 394 |
('str(1) ', 4, 'int'), |
| 395 |
('str(1) ', 5, 'int'), |
| 396 |
('str(1) ', 6, 'str'), |
| 397 |
('str(1) ', 7, None), |
| 398 |
('str( 1) ', 4, 'str'), |
| 399 |
('str( 1) ', 5, 'int'), |
| 400 |
('str(+1) ', 4, 'str'), |
| 401 |
('str(+1) ', 5, 'int'), |
| 402 |
('str(1, 1.) ', 3, 'str'), |
| 403 |
('str(1, 1.) ', 4, 'int'), |
| 404 |
('str(1, 1.) ', 5, 'int'), |
| 405 |
('str(1, 1.) ', 6, None), |
| 406 |
('str(1, 1.) ', 7, 'float'), |
| 407 |
] |
| 408 |
) |
| 409 |
def test_infer_after_parentheses(Script, code, column, expected): |
| 410 |
completions = Script(code).infer(column=column) |
| 411 |
if expected is None: |
| 412 |
assert completions == [] |
| 413 |
else: |
| 414 |
assert [c.name for c in completions] == [expected] |