| 1 |
from textwrap import dedent |
| 2 |
import inspect |
| 3 |
from unittest import TestCase |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from jedi import cache |
| 8 |
from jedi.parser_utils import get_signature |
| 9 |
from jedi import Interpreter |
| 10 |
|
| 11 |
|
| 12 |
def assert_signature(Script, source, expected_name, expected_index=0, line=None, column=None): |
| 13 |
signatures = Script(source).get_signatures(line, column) |
| 14 |
|
| 15 |
assert len(signatures) <= 1 |
| 16 |
|
| 17 |
if not signatures: |
| 18 |
assert expected_name is None, \ |
| 19 |
'There are no signatures, but `%s` expected.' % expected_name |
| 20 |
else: |
| 21 |
assert signatures[0].name == expected_name |
| 22 |
assert signatures[0].index == expected_index |
| 23 |
return signatures[0] |
| 24 |
|
| 25 |
|
| 26 |
def test_valid_call(Script): |
| 27 |
assert_signature(Script, 'bool()', 'bool', column=5) |
| 28 |
|
| 29 |
|
| 30 |
def test_dunder_new(Script): |
| 31 |
|
| 32 |
s = dedent("""\ |
| 33 |
from typing import Self |
| 34 |
class C: |
| 35 |
def __new__(cls, b) -> Self: |
| 36 |
pass |
| 37 |
C( )""") |
| 38 |
assert_signature(Script, s, 'C', 0, line=5, column=2) |
| 39 |
|
| 40 |
|
| 41 |
class TestSignatures(TestCase): |
| 42 |
@pytest.fixture(autouse=True) |
| 43 |
def init(self, Script): |
| 44 |
self.Script = Script |
| 45 |
|
| 46 |
def _run_simple(self, source, name, index=0, column=None, line=1): |
| 47 |
assert_signature(self.Script, source, name, index, line, column) |
| 48 |
|
| 49 |
def test_simple(self): |
| 50 |
run = self._run_simple |
| 51 |
|
| 52 |
|
| 53 |
s1 = "tuple(a, bool(" |
| 54 |
run(s1, 'tuple', 0, 6) |
| 55 |
run(s1, 'tuple', None, 8) |
| 56 |
run(s1, 'tuple', None, 9) |
| 57 |
run(s1, 'bool', 0, 14) |
| 58 |
|
| 59 |
s2 = "abs(), " |
| 60 |
run(s2, 'abs', 0, 4) |
| 61 |
run(s2, None, column=5) |
| 62 |
run(s2, None) |
| 63 |
|
| 64 |
s3 = "abs()." |
| 65 |
run(s3, None, column=5) |
| 66 |
run(s3, None) |
| 67 |
|
| 68 |
def test_more_complicated(self): |
| 69 |
run = self._run_simple |
| 70 |
|
| 71 |
s4 = 'abs(bool(), , set,' |
| 72 |
run(s4, None, column=3) |
| 73 |
run(s4, 'abs', 0, 4) |
| 74 |
run(s4, 'bool', 0, 9) |
| 75 |
run(s4, 'abs', 0, 10) |
| 76 |
run(s4, 'abs', None, 11) |
| 77 |
|
| 78 |
s5 = "tuple(1,\nif 2:\n def a():" |
| 79 |
run(s5, 'tuple', 0, 6) |
| 80 |
run(s5, 'tuple', None, 8) |
| 81 |
|
| 82 |
s6 = "bool().__eq__(" |
| 83 |
run(s6, '__eq__', 0) |
| 84 |
run(s6, 'bool', 0, 5) |
| 85 |
|
| 86 |
|
| 87 |
s8 = "bool(int[abs(" |
| 88 |
|
| 89 |
run(s8, 'abs', 0) |
| 90 |
run(s8, 'bool', 0, 10) |
| 91 |
|
| 92 |
run("import time; abc = time; abc.sleep(", 'sleep', 0) |
| 93 |
|
| 94 |
def test_issue_57(self): |
| 95 |
|
| 96 |
s = "def func(alpha, beta): pass\n" \ |
| 97 |
"func(alpha='101'," |
| 98 |
self._run_simple(s, 'func', 0, column=13, line=2) |
| 99 |
|
| 100 |
def test_for(self): |
| 101 |
|
| 102 |
self._run_simple("for tuple(", 'tuple', 0) |
| 103 |
self._run_simple("for s in tuple(", 'tuple', 0) |
| 104 |
|
| 105 |
|
| 106 |
def test_with(Script): |
| 107 |
|
| 108 |
sigs = Script("with open(").get_signatures() |
| 109 |
assert sigs |
| 110 |
assert all(sig.name == 'open' for sig in sigs) |
| 111 |
|
| 112 |
|
| 113 |
def test_get_signatures_empty_parentheses_pre_space(Script): |
| 114 |
s = dedent("""\ |
| 115 |
def f(a, b): |
| 116 |
pass |
| 117 |
f( )""") |
| 118 |
assert_signature(Script, s, 'f', 0, line=3, column=3) |
| 119 |
|
| 120 |
|
| 121 |
def test_multiple_signatures(Script): |
| 122 |
s = dedent("""\ |
| 123 |
if x: |
| 124 |
def f(a, b): |
| 125 |
pass |
| 126 |
else: |
| 127 |
def f(a, b): |
| 128 |
pass |
| 129 |
f(""") |
| 130 |
assert len(Script(s).get_signatures()) == 2 |
| 131 |
|
| 132 |
|
| 133 |
def test_get_signatures_whitespace(Script): |
| 134 |
|
| 135 |
s = 'abs( \ndef x():\n pass\n' |
| 136 |
assert_signature(Script, s, 'abs', 0, line=1, column=5) |
| 137 |
|
| 138 |
|
| 139 |
def test_decorator_in_class(Script): |
| 140 |
""" |
| 141 |
There's still an implicit param, with a decorator. |
| 142 |
Github issue #319. |
| 143 |
""" |
| 144 |
s = dedent("""\ |
| 145 |
def static(func): |
| 146 |
def wrapped(obj, *args): |
| 147 |
return f(type(obj), *args) |
| 148 |
return wrapped |
| 149 |
|
| 150 |
class C(object): |
| 151 |
@static |
| 152 |
def test(cls): |
| 153 |
return 10 |
| 154 |
|
| 155 |
C().test(""") |
| 156 |
|
| 157 |
signatures = Script(s).get_signatures() |
| 158 |
assert len(signatures) == 1 |
| 159 |
x = [p.description for p in signatures[0].params] |
| 160 |
assert x == ['param *args'] |
| 161 |
|
| 162 |
|
| 163 |
def test_additional_brackets(Script): |
| 164 |
assert_signature(Script, 'abs((', 'abs', 0) |
| 165 |
|
| 166 |
|
| 167 |
def test_unterminated_strings(Script): |
| 168 |
assert_signature(Script, 'abs(";', 'abs', 0) |
| 169 |
|
| 170 |
|
| 171 |
def test_whitespace_before_bracket(Script): |
| 172 |
assert_signature(Script, 'abs (', 'abs', 0) |
| 173 |
assert_signature(Script, 'abs (";', 'abs', 0) |
| 174 |
assert_signature(Script, 'abs\n(', None) |
| 175 |
|
| 176 |
|
| 177 |
def test_brackets_in_string_literals(Script): |
| 178 |
assert_signature(Script, 'abs (" (', 'abs', 0) |
| 179 |
assert_signature(Script, 'abs (" )', 'abs', 0) |
| 180 |
|
| 181 |
|
| 182 |
def test_function_definitions_should_break(Script): |
| 183 |
""" |
| 184 |
Function definitions (and other tokens that cannot exist within call |
| 185 |
signatures) should break and not be able to return a signature. |
| 186 |
""" |
| 187 |
assert_signature(Script, 'abs(\ndef x', 'abs', 0) |
| 188 |
assert not Script('abs(\ndef x(): pass').get_signatures() |
| 189 |
|
| 190 |
|
| 191 |
def test_flow_call(Script): |
| 192 |
assert not Script('if (1').get_signatures() |
| 193 |
|
| 194 |
|
| 195 |
def test_chained_calls(Script): |
| 196 |
source = dedent(''' |
| 197 |
class B(): |
| 198 |
def test2(self, arg): |
| 199 |
pass |
| 200 |
|
| 201 |
class A(): |
| 202 |
def test1(self): |
| 203 |
return B() |
| 204 |
|
| 205 |
A().test1().test2(''') |
| 206 |
|
| 207 |
assert_signature(Script, source, 'test2', 0) |
| 208 |
|
| 209 |
|
| 210 |
def test_return(Script): |
| 211 |
source = dedent(''' |
| 212 |
def foo(): |
| 213 |
return (1).conjugate()''') |
| 214 |
|
| 215 |
assert_signature( |
| 216 |
Script, source, 'conjugate', expected_index=None, column=len(" return (1).conjugate(")) |
| 217 |
|
| 218 |
|
| 219 |
def test_find_signature_on_module(Script): |
| 220 |
"""github issue #240""" |
| 221 |
s = 'import datetime; datetime(' |
| 222 |
|
| 223 |
assert Script(s).get_signatures() == [] |
| 224 |
|
| 225 |
|
| 226 |
def test_complex(Script, environment): |
| 227 |
s = """ |
| 228 |
def abc(a,b): |
| 229 |
pass |
| 230 |
|
| 231 |
def a(self): |
| 232 |
abc( |
| 233 |
|
| 234 |
if 1: |
| 235 |
pass |
| 236 |
""" |
| 237 |
assert_signature(Script, s, 'abc', 0, line=6, column=20) |
| 238 |
s = """ |
| 239 |
import re |
| 240 |
def huhu(it): |
| 241 |
re.compile( |
| 242 |
return it * 2 |
| 243 |
""" |
| 244 |
sig1, sig2 = sorted(Script(s).get_signatures(line=4, column=27), key=lambda s: s.line) |
| 245 |
assert sig1.name == sig2.name == 'compile' |
| 246 |
assert sig1.index == sig2.index == 0 |
| 247 |
func1, = sig1._name.infer() |
| 248 |
func2, = sig2._name.infer() |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
assert get_signature(func1.tree_node) \ |
| 253 |
== 'compile(pattern: AnyStr, flags: _FlagsType = 0) -> Pattern[AnyStr]' |
| 254 |
assert get_signature(func2.tree_node) \ |
| 255 |
== 'compile(pattern: Pattern[AnyStr], flags: _FlagsType = 0) ->\nPattern[AnyStr]' |
| 256 |
|
| 257 |
|
| 258 |
s = """def foo(""" |
| 259 |
assert Script(s).get_signatures() == [] |
| 260 |
|
| 261 |
|
| 262 |
s = """import itertools; test = getattr(itertools, 'chain'); test(""" |
| 263 |
assert_signature(Script, s, 'chain', 0) |
| 264 |
|
| 265 |
|
| 266 |
def _params(Script, source, line=None, column=None): |
| 267 |
signatures = Script(source).get_signatures(line, column) |
| 268 |
assert len(signatures) == 1 |
| 269 |
return signatures[0].params |
| 270 |
|
| 271 |
|
| 272 |
def test_int_params(Script): |
| 273 |
sig1, sig2 = Script('int(').get_signatures() |
| 274 |
|
| 275 |
assert len(sig1.params) == 1 |
| 276 |
assert sig1.params[0].name == 'x' |
| 277 |
assert len(sig2.params) == 2 |
| 278 |
assert sig2.params[0].name == 'x' |
| 279 |
assert sig2.params[1].name == 'base' |
| 280 |
|
| 281 |
|
| 282 |
def test_pow_params(Script): |
| 283 |
|
| 284 |
for sig in Script('pow(').get_signatures(): |
| 285 |
param_names = [p.name for p in sig.params] |
| 286 |
assert param_names in (['base', 'exp'], ['base', 'exp', 'mod']) |
| 287 |
|
| 288 |
|
| 289 |
def test_param_name(Script): |
| 290 |
sigs = Script('open(something,').get_signatures() |
| 291 |
for sig in sigs: |
| 292 |
|
| 293 |
|
| 294 |
assert sig.params[0].name in ['file', 'name'] |
| 295 |
assert sig.params[1].name == 'mode' |
| 296 |
assert sig.params[2].name == 'buffering' |
| 297 |
|
| 298 |
|
| 299 |
def test_builtins(Script): |
| 300 |
""" |
| 301 |
The self keyword should be visible even for builtins, if not |
| 302 |
instantiated. |
| 303 |
""" |
| 304 |
p = _params(Script, 'str.endswith(') |
| 305 |
assert p[0].name == 'self' |
| 306 |
assert p[1].name == 'suffix' |
| 307 |
p = _params(Script, 'str().endswith(') |
| 308 |
assert p[0].name == 'suffix' |
| 309 |
|
| 310 |
|
| 311 |
def test_signature_is_definition(Script): |
| 312 |
""" |
| 313 |
Through inheritance, a signature is a sub class of Name. |
| 314 |
Check if the attributes match. |
| 315 |
""" |
| 316 |
s = """class Spam(): pass\nSpam""" |
| 317 |
signature = Script(s + '(').get_signatures()[0] |
| 318 |
definition = Script(s + '(').infer(column=0)[0] |
| 319 |
signature.line == 1 |
| 320 |
signature.column == 6 |
| 321 |
|
| 322 |
|
| 323 |
for attr_name in dir(definition): |
| 324 |
dont_scan = ['defined_names', 'parent', 'goto_assignments', 'infer', |
| 325 |
'params', 'get_signatures', 'execute', 'goto', |
| 326 |
'desc_with_module'] |
| 327 |
if attr_name.startswith('_') or attr_name in dont_scan: |
| 328 |
continue |
| 329 |
|
| 330 |
attribute = getattr(definition, attr_name) |
| 331 |
signature_attribute = getattr(signature, attr_name) |
| 332 |
if inspect.ismethod(attribute): |
| 333 |
assert attribute() == signature_attribute() |
| 334 |
else: |
| 335 |
assert attribute == signature_attribute |
| 336 |
|
| 337 |
|
| 338 |
def test_no_signature(Script): |
| 339 |
|
| 340 |
assert Script('str()(').get_signatures() == [] |
| 341 |
|
| 342 |
s = dedent("""\ |
| 343 |
class X(): |
| 344 |
pass |
| 345 |
X()(""") |
| 346 |
assert Script(s).get_signatures() == [] |
| 347 |
assert len(Script(s).get_signatures(column=2)) == 1 |
| 348 |
assert Script('').get_signatures() == [] |
| 349 |
|
| 350 |
|
| 351 |
def test_dict_literal_in_incomplete_call(Script): |
| 352 |
source = """\ |
| 353 |
import json |
| 354 |
|
| 355 |
def foo(): |
| 356 |
json.loads( |
| 357 |
|
| 358 |
json.load.return_value = {'foo': [], |
| 359 |
'bar': True} |
| 360 |
|
| 361 |
c = Foo() |
| 362 |
""" |
| 363 |
|
| 364 |
script = Script(dedent(source)) |
| 365 |
assert script.get_signatures(line=4, column=15) |
| 366 |
|
| 367 |
|
| 368 |
def test_completion_interference(Script): |
| 369 |
"""Seems to cause problems, see also #396.""" |
| 370 |
cache.parser_cache.pop(None, None) |
| 371 |
assert Script('open(').get_signatures() |
| 372 |
|
| 373 |
|
| 374 |
assert Script('from datetime import ').complete() |
| 375 |
|
| 376 |
assert Script('open(').get_signatures() |
| 377 |
|
| 378 |
|
| 379 |
def test_keyword_argument_index(Script, environment): |
| 380 |
def get(source, column=None): |
| 381 |
return Script(source).get_signatures(column=column)[0] |
| 382 |
|
| 383 |
assert get('sorted([], key=a').index == 1 |
| 384 |
assert get('sorted([], key=').index == 1 |
| 385 |
assert get('sorted([], no_key=a').index is None |
| 386 |
|
| 387 |
kw_func = 'def foo(a, b): pass\nfoo(b=3, a=4)' |
| 388 |
assert get(kw_func, column=len('foo(b')).index == 0 |
| 389 |
assert get(kw_func, column=len('foo(b=')).index == 1 |
| 390 |
assert get(kw_func, column=len('foo(b=3, a=')).index == 0 |
| 391 |
|
| 392 |
kw_func_simple = 'def foo(a, b): pass\nfoo(b=4)' |
| 393 |
assert get(kw_func_simple, column=len('foo(b')).index == 0 |
| 394 |
assert get(kw_func_simple, column=len('foo(b=')).index == 1 |
| 395 |
|
| 396 |
args_func = 'def foo(*kwargs): pass\n' |
| 397 |
assert get(args_func + 'foo(a').index == 0 |
| 398 |
assert get(args_func + 'foo(a, b').index == 0 |
| 399 |
|
| 400 |
kwargs_func = 'def foo(**kwargs): pass\n' |
| 401 |
assert get(kwargs_func + 'foo(a=2').index == 0 |
| 402 |
assert get(kwargs_func + 'foo(a=2, b=2').index == 0 |
| 403 |
|
| 404 |
both = 'def foo(*args, **kwargs): pass\n' |
| 405 |
assert get(both + 'foo(a=2').index == 1 |
| 406 |
assert get(both + 'foo(a=2, b=2').index == 1 |
| 407 |
assert get(both + 'foo(a=2, b=2)', column=len('foo(b=2, a=2')).index == 1 |
| 408 |
assert get(both + 'foo(a, b, c').index == 0 |
| 409 |
|
| 410 |
|
| 411 |
code1 = 'def f(u, /, v=3, *, abc, abd, xyz): pass' |
| 412 |
code2 = 'def g(u, /, v=3, *, abc, abd, xyz, **kwargs): pass' |
| 413 |
code3 = 'def h(u, /, v, *args, x=1, y): pass' |
| 414 |
code4 = 'def i(u, /, v, *args, x=1, y, **kwargs): pass' |
| 415 |
|
| 416 |
|
| 417 |
_calls = [ |
| 418 |
|
| 419 |
(code1, 'f(', 0), |
| 420 |
(code1, 'f(a', 0), |
| 421 |
(code1, 'f(a,', 1), |
| 422 |
(code1, 'f(a,b', 1), |
| 423 |
(code1, 'f(a,b,', 2), |
| 424 |
(code1, 'f(a,b,c', None), |
| 425 |
(code1, 'f(a,b,a', 2), |
| 426 |
(code1, 'f(a,b,a=', None), |
| 427 |
(code1, 'f(a,b,abc', 2), |
| 428 |
(code1, 'f(a,b,abc=(', 2), |
| 429 |
(code1, 'f(a,b,abc=(f,1,2,3', 2), |
| 430 |
(code1, 'f(a,b,abd', 3), |
| 431 |
(code1, 'f(a,b,x', 4), |
| 432 |
(code1, 'f(a,b,xy', 4), |
| 433 |
(code1, 'f(a,b,xyz=', 4), |
| 434 |
(code1, 'f(a,b,xy=', None), |
| 435 |
(code1, 'f(u=', None), |
| 436 |
(code1, 'f(v=', 1), |
| 437 |
|
| 438 |
|
| 439 |
(code2, 'g(a,b,a', 2), |
| 440 |
(code2, 'g(a,b,abc', 2), |
| 441 |
(code2, 'g(a,b,abd', 3), |
| 442 |
(code2, 'g(a,b,arr', 5), |
| 443 |
(code2, 'g(a,b,xy', 4), |
| 444 |
(code2, 'g(a,b,xyz=', 4), |
| 445 |
(code2, 'g(a,b,xy=', 5), |
| 446 |
(code2, 'g(a,b,abc=1,abd=4,', 4), |
| 447 |
(code2, 'g(a,b,abc=1,xyz=3,abd=4,', 5), |
| 448 |
(code2, 'g(a,b,abc=1,abd=4,lala', 5), |
| 449 |
(code2, 'g(a,b,abc=1,abd=4,lala=', 5), |
| 450 |
(code2, 'g(a,b,abc=1,abd=4,abd=', 5), |
| 451 |
(code2, 'g(a,b,kw', 5), |
| 452 |
(code2, 'g(a,b,kwargs=', 5), |
| 453 |
(code2, 'g(u=', 5), |
| 454 |
(code2, 'g(v=', 1), |
| 455 |
|
| 456 |
|
| 457 |
(code3, 'h(a,b,c', 2), |
| 458 |
(code3, 'h(a,b,c,', 2), |
| 459 |
(code3, 'h(a,b,c,d', 2), |
| 460 |
(code3, 'h(a,b,c,d[', 2), |
| 461 |
(code3, 'h(a,b,c,(3,', 2), |
| 462 |
(code3, 'h(a,b,c,(3,)', 2), |
| 463 |
(code3, 'h(a,b,args=', None), |
| 464 |
(code3, 'h(u,v=', 1), |
| 465 |
(code3, 'h(u=', None), |
| 466 |
(code3, 'h(u,*xxx', 1), |
| 467 |
(code3, 'h(u,*xxx,*yyy', 1), |
| 468 |
(code3, 'h(u,*[]', 1), |
| 469 |
(code3, 'h(u,*', 1), |
| 470 |
(code3, 'h(u,*, *', 1), |
| 471 |
(code3, 'h(u,1,**', 3), |
| 472 |
(code3, 'h(u,**y', 1), |
| 473 |
(code3, 'h(u,x=2,**', 1), |
| 474 |
(code3, 'h(u,x=2,**y', 1), |
| 475 |
(code3, 'h(u,v=2,**y', 3), |
| 476 |
(code3, 'h(u,x=2,**vv', 1), |
| 477 |
|
| 478 |
|
| 479 |
(code4, 'i(a,b,c,d', 2), |
| 480 |
(code4, 'i(a,b,c,d,e', 2), |
| 481 |
(code4, 'i(a,b,c,d,e=', 5), |
| 482 |
(code4, 'i(a,b,c,d,e=3', 5), |
| 483 |
(code4, 'i(a,b,c,d=,x=', 3), |
| 484 |
(code4, 'i(a,b,c,d=5,x=4', 3), |
| 485 |
(code4, 'i(a,b,c,d=5,x=4,y', 4), |
| 486 |
(code4, 'i(a,b,c,d=5,x=4,y=3,', 5), |
| 487 |
(code4, 'i(a,b,c,d=5,y=4,x=3,', 5), |
| 488 |
(code4, 'i(a,b,c,d=4,', 3), |
| 489 |
(code4, 'i(a,b,c,x=1,d=,', 4), |
| 490 |
|
| 491 |
|
| 492 |
(code4, 'i(1, [a,b', 1), |
| 493 |
(code4, 'i(1, [a,b=,', 2), |
| 494 |
(code4, 'i(1, [a?b,', 2), |
| 495 |
(code4, 'i(1, [a?b,*', 2), |
| 496 |
(code4, 'i(?b,*r,c', 1), |
| 497 |
(code4, 'i(?*', 0), |
| 498 |
(code4, 'i(?**', 1), |
| 499 |
|
| 500 |
|
| 501 |
(code4, 'i(()', 0), |
| 502 |
(code4, 'i((),', 1), |
| 503 |
(code4, 'i([(),', 0), |
| 504 |
(code4, 'i([(,', 1), |
| 505 |
(code4, 'i(x,()', 1), |
| 506 |
] |
| 507 |
|
| 508 |
|
| 509 |
@pytest.mark.parametrize('ending', ['', ')']) |
| 510 |
@pytest.mark.parametrize('code, call, expected_index', _calls) |
| 511 |
def test_signature_index(Script, environment, code, call, expected_index, ending): |
| 512 |
sig, = Script(code + '\n' + call + ending).get_signatures(column=len(call)) |
| 513 |
index = sig.index |
| 514 |
assert expected_index == index |
| 515 |
|
| 516 |
|
| 517 |
@pytest.mark.parametrize('code', ['foo', 'instance.foo']) |
| 518 |
def test_arg_defaults(Script, environment, code): |
| 519 |
def foo(arg="bla", arg1=1): |
| 520 |
pass |
| 521 |
|
| 522 |
class Klass: |
| 523 |
def foo(self, arg="bla", arg1=1): |
| 524 |
pass |
| 525 |
|
| 526 |
instance = Klass() |
| 527 |
|
| 528 |
src = dedent(""" |
| 529 |
def foo2(arg="bla", arg1=1): |
| 530 |
pass |
| 531 |
|
| 532 |
class Klass2: |
| 533 |
def foo2(self, arg="bla", arg1=1): |
| 534 |
pass |
| 535 |
|
| 536 |
instance = Klass2() |
| 537 |
""") |
| 538 |
|
| 539 |
executed_locals = dict() |
| 540 |
exec(src, None, executed_locals) |
| 541 |
locals_ = locals() |
| 542 |
|
| 543 |
def iter_scripts(): |
| 544 |
yield Interpreter(code + '(', namespaces=[locals_]) |
| 545 |
yield Script(src + code + "2(") |
| 546 |
yield Interpreter(code + '2(', namespaces=[executed_locals]) |
| 547 |
|
| 548 |
for script in iter_scripts(): |
| 549 |
signatures = script.get_signatures() |
| 550 |
assert signatures[0].params[0].description in ('param arg="bla"', "param arg='bla'") |
| 551 |
assert signatures[0].params[1].description == 'param arg1=1' |
| 552 |
|
| 553 |
|
| 554 |
def test_bracket_start(Script): |
| 555 |
def bracket_start(src): |
| 556 |
signatures = Script(src).get_signatures() |
| 557 |
assert len(signatures) == 1 |
| 558 |
return signatures[0].bracket_start |
| 559 |
|
| 560 |
assert bracket_start('abs(') == (1, 3) |
| 561 |
|
| 562 |
|
| 563 |
def test_different_caller(Script): |
| 564 |
""" |
| 565 |
It's possible to not use names, but another function result or an array |
| 566 |
index and then get the signature of it. |
| 567 |
""" |
| 568 |
|
| 569 |
assert_signature(Script, '[abs][0](', 'abs', 0) |
| 570 |
assert_signature(Script, '[abs][0]()', 'abs', 0, column=len('[abs][0](')) |
| 571 |
|
| 572 |
assert_signature(Script, '(abs)(', 'abs', 0) |
| 573 |
assert_signature(Script, '(abs)()', 'abs', 0, column=len('(abs)(')) |
| 574 |
|
| 575 |
|
| 576 |
def test_in_function(Script): |
| 577 |
code = dedent('''\ |
| 578 |
class X(): |
| 579 |
@property |
| 580 |
def func(''') |
| 581 |
assert not Script(code).get_signatures() |
| 582 |
|
| 583 |
|
| 584 |
def test_lambda_params(Script): |
| 585 |
code = dedent('''\ |
| 586 |
my_lambda = lambda x: x+1 |
| 587 |
my_lambda(1)''') |
| 588 |
sig, = Script(code).get_signatures(column=11) |
| 589 |
assert sig.index == 0 |
| 590 |
assert sig.name == '<lambda>' |
| 591 |
assert [p.name for p in sig.params] == ['x'] |
| 592 |
|
| 593 |
|
| 594 |
CLASS_CODE = dedent('''\ |
| 595 |
class X(): |
| 596 |
def __init__(self, foo, bar): |
| 597 |
self.foo = foo |
| 598 |
''') |
| 599 |
|
| 600 |
|
| 601 |
def test_class_creation(Script): |
| 602 |
|
| 603 |
sig, = Script(CLASS_CODE + 'X(').get_signatures() |
| 604 |
assert sig.index == 0 |
| 605 |
assert sig.name == 'X' |
| 606 |
assert [p.name for p in sig.params] == ['foo', 'bar'] |
| 607 |
|
| 608 |
|
| 609 |
def test_call_init_on_class(Script): |
| 610 |
sig, = Script(CLASS_CODE + 'X.__init__(').get_signatures() |
| 611 |
assert [p.name for p in sig.params] == ['self', 'foo', 'bar'] |
| 612 |
|
| 613 |
|
| 614 |
def test_call_init_on_instance(Script): |
| 615 |
sig, = Script(CLASS_CODE + 'X().__init__(').get_signatures() |
| 616 |
assert [p.name for p in sig.params] == ['foo', 'bar'] |
| 617 |
|
| 618 |
|
| 619 |
def test_call_magic_method(Script): |
| 620 |
code = dedent('''\ |
| 621 |
class X(): |
| 622 |
def __call__(self, baz): |
| 623 |
pass |
| 624 |
''') |
| 625 |
sig, = Script(code + 'X()(').get_signatures() |
| 626 |
assert sig.index == 0 |
| 627 |
assert sig.name == 'X' |
| 628 |
assert [p.name for p in sig.params] == ['baz'] |
| 629 |
|
| 630 |
sig, = Script(code + 'X.__call__(').get_signatures() |
| 631 |
assert [p.name for p in sig.params] == ['self', 'baz'] |
| 632 |
sig, = Script(code + 'X().__call__(').get_signatures() |
| 633 |
assert [p.name for p in sig.params] == ['baz'] |
| 634 |
|
| 635 |
|
| 636 |
@pytest.mark.parametrize('column', [6, 9]) |
| 637 |
def test_cursor_after_signature(Script, column): |
| 638 |
source = dedent(""" |
| 639 |
def foo(*args): |
| 640 |
pass |
| 641 |
foo() # _ |
| 642 |
""") |
| 643 |
|
| 644 |
script = Script(source) |
| 645 |
|
| 646 |
assert not script.get_signatures(4, column) |
| 647 |
|
| 648 |
|
| 649 |
@pytest.mark.parametrize( |
| 650 |
'code, line, column, name, index', [ |
| 651 |
('abs(()\ndef foo(): pass', 1, None, 'abs', 0), |
| 652 |
('abs(chr() \ndef foo(): pass', 1, 10, 'abs', 0), |
| 653 |
('abs(chr()\ndef foo(): pass', 1, None, 'abs', 0), |
| 654 |
('abs(chr()\ndef foo(): pass', 1, 8, 'chr', 0), |
| 655 |
('abs(chr()\ndef foo(): pass', 1, 7, 'abs', 0), |
| 656 |
('abs(chr ( \nclass y: pass', 1, None, 'chr', 0), |
| 657 |
('abs(chr ( \nclass y: pass', 1, 8, 'abs', 0), |
| 658 |
('abs(chr ( \nclass y: pass', 1, 9, 'abs', 0), |
| 659 |
('abs(chr ( \nclass y: pass', 1, 10, 'chr', 0), |
| 660 |
('abs(foo.bar=3)', 1, 13, 'abs', 0), |
| 661 |
] |
| 662 |
) |
| 663 |
def test_base_signatures(Script, code, line, column, name, index): |
| 664 |
sig, = Script(code).get_signatures(line=line, column=column) |
| 665 |
|
| 666 |
assert sig.name == name |
| 667 |
assert sig.index == index |