| 1 |
""" Test all things related to the ``jedi.api_classes`` module. |
| 2 |
""" |
| 3 |
|
| 4 |
from textwrap import dedent |
| 5 |
from inspect import cleandoc |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
import jedi |
| 10 |
from jedi import __doc__ as jedi_doc |
| 11 |
from jedi.inference.compiled import CompiledValueName |
| 12 |
from ..helpers import get_example_dir |
| 13 |
|
| 14 |
|
| 15 |
def test_is_keyword(Script): |
| 16 |
results = Script('str', path=None).infer(1, 1) |
| 17 |
assert len(results) == 1 and results[0].is_keyword is False |
| 18 |
|
| 19 |
|
| 20 |
def test_basedefinition_type(Script, get_names): |
| 21 |
def make_definitions(): |
| 22 |
""" |
| 23 |
Return a list of definitions for parametrized tests. |
| 24 |
|
| 25 |
:rtype: [jedi.api_classes.BaseName] |
| 26 |
""" |
| 27 |
source = dedent(""" |
| 28 |
import sys |
| 29 |
|
| 30 |
class C: |
| 31 |
pass |
| 32 |
|
| 33 |
x = C() |
| 34 |
|
| 35 |
def f(): |
| 36 |
pass |
| 37 |
|
| 38 |
def g(): |
| 39 |
yield |
| 40 |
|
| 41 |
h = lambda: None |
| 42 |
""") |
| 43 |
|
| 44 |
definitions = [] |
| 45 |
definitions += get_names(source) |
| 46 |
|
| 47 |
source += dedent(""" |
| 48 |
variable = sys or C or x or f or g or g() or h""") |
| 49 |
lines = source.splitlines() |
| 50 |
script = Script(source, path=None) |
| 51 |
definitions += script.infer(len(lines), len('variable')) |
| 52 |
|
| 53 |
script2 = Script(source, path=None) |
| 54 |
definitions += script2.get_references(4, len('class C')) |
| 55 |
|
| 56 |
source_param = "def f(a): return a" |
| 57 |
script_param = Script(source_param, path=None) |
| 58 |
definitions += script_param.goto(1, len(source_param)) |
| 59 |
|
| 60 |
return definitions |
| 61 |
|
| 62 |
for definition in make_definitions(): |
| 63 |
assert definition.type in ('module', 'class', 'instance', 'function', |
| 64 |
'generator', 'statement', 'import', 'param') |
| 65 |
|
| 66 |
|
| 67 |
@pytest.mark.parametrize( |
| 68 |
('src', 'expected_result', 'column'), [ |
| 69 |
|
| 70 |
('import t', 'module', None), |
| 71 |
('import ', 'module', None), |
| 72 |
('import datetime; datetime', 'module', None), |
| 73 |
|
| 74 |
|
| 75 |
('from datetime import timedelta', 'class', None), |
| 76 |
('from datetime import timedelta; timedelta', 'class', None), |
| 77 |
('from json import tool', 'module', None), |
| 78 |
('from json import tool; tool', 'module', None), |
| 79 |
|
| 80 |
|
| 81 |
('import json.tool; json', 'module', None), |
| 82 |
('import json.tool; json.tool', 'module', None), |
| 83 |
('import json.tool; json.tool.main', 'function', None), |
| 84 |
('import json.tool', 'module', None), |
| 85 |
('import json.tool', 'module', 9), |
| 86 |
] |
| 87 |
|
| 88 |
) |
| 89 |
def test_basedefinition_type_import(Script, src, expected_result, column): |
| 90 |
types = {t.type for t in Script(src).complete(column=column)} |
| 91 |
assert types == {expected_result} |
| 92 |
|
| 93 |
|
| 94 |
def test_function_signature_in_doc(Script): |
| 95 |
defs = Script(""" |
| 96 |
def f(x, y=1, z='a'): |
| 97 |
pass |
| 98 |
f""").infer() |
| 99 |
doc = defs[0].docstring() |
| 100 |
assert "f(x, y=1, z='a')" in str(doc) |
| 101 |
|
| 102 |
|
| 103 |
def test_param_docstring(get_names): |
| 104 |
param = get_names("def test(parameter): pass", all_scopes=True)[1] |
| 105 |
assert param.name == 'parameter' |
| 106 |
assert param.docstring() == '' |
| 107 |
|
| 108 |
|
| 109 |
def test_class_signature(Script): |
| 110 |
defs = Script(""" |
| 111 |
class Foo: |
| 112 |
def __init__(self, x, y=1, z='a'): |
| 113 |
pass |
| 114 |
Foo""").infer() |
| 115 |
doc = defs[0].docstring() |
| 116 |
assert doc == "Foo(x, y=1, z='a')" |
| 117 |
|
| 118 |
|
| 119 |
def test_position_none_if_builtin(Script): |
| 120 |
gotos = Script('import sys; sys.path').goto() |
| 121 |
assert gotos[0].in_builtin_module() |
| 122 |
assert gotos[0].line is not None |
| 123 |
assert gotos[0].column is not None |
| 124 |
|
| 125 |
|
| 126 |
def test_completion_docstring(Script, jedi_path): |
| 127 |
""" |
| 128 |
Jedi should follow imports in certain conditions |
| 129 |
""" |
| 130 |
def docstr(src, result): |
| 131 |
c = Script(src, project=project).complete()[0] |
| 132 |
assert c.docstring(raw=True, fast=False) == cleandoc(result) |
| 133 |
|
| 134 |
project = jedi.Project('.', sys_path=[jedi_path]) |
| 135 |
c = Script('import jedi\njed', project=project).complete()[0] |
| 136 |
assert c.docstring(fast=False) == cleandoc(jedi_doc) |
| 137 |
|
| 138 |
docstr('import jedi\njedi.Scr', cleandoc(jedi.Script.__doc__)) |
| 139 |
|
| 140 |
docstr('abcd=3;abcd', '') |
| 141 |
docstr('"hello"\nabcd=3\nabcd', '') |
| 142 |
docstr( |
| 143 |
dedent(''' |
| 144 |
def x(): |
| 145 |
"hello" |
| 146 |
0 |
| 147 |
x'''), |
| 148 |
'hello' |
| 149 |
) |
| 150 |
docstr( |
| 151 |
dedent(''' |
| 152 |
def x(): |
| 153 |
"hello";0 |
| 154 |
x'''), |
| 155 |
'hello' |
| 156 |
) |
| 157 |
|
| 158 |
docstr( |
| 159 |
dedent(''' |
| 160 |
def x(): |
| 161 |
"hello",0 |
| 162 |
x'''), |
| 163 |
'' |
| 164 |
) |
| 165 |
|
| 166 |
docstr( |
| 167 |
dedent(''' |
| 168 |
def x(): |
| 169 |
"hello" |
| 170 |
y = x |
| 171 |
y'''), |
| 172 |
'' |
| 173 |
) |
| 174 |
|
| 175 |
|
| 176 |
def test_completion_params(Script): |
| 177 |
c = Script('import string; string.capwords').complete()[0] |
| 178 |
assert [p.name for p in c.get_signatures()[0].params] == ['s', 'sep'] |
| 179 |
|
| 180 |
|
| 181 |
def test_functions_should_have_params(Script): |
| 182 |
for c in Script('bool.').complete(): |
| 183 |
if c.type == 'function': |
| 184 |
if c.name in ('denominator', 'numerator', 'imag', 'real', '__class__'): |
| 185 |
|
| 186 |
assert not c.get_signatures() |
| 187 |
else: |
| 188 |
assert c.get_signatures() |
| 189 |
|
| 190 |
|
| 191 |
def test_hashlib_params(Script, environment): |
| 192 |
script = Script('from hashlib import sha256') |
| 193 |
c, = script.complete() |
| 194 |
sig, = c.get_signatures() |
| 195 |
if environment.version_info >= (3, 13): |
| 196 |
wanted = ['data', 'usedforsecurity', 'string'] |
| 197 |
else: |
| 198 |
wanted = ['string', 'usedforsecurity'] |
| 199 |
assert [p.name for p in sig.params] == wanted |
| 200 |
|
| 201 |
|
| 202 |
def test_signature_params(Script): |
| 203 |
def check(defs): |
| 204 |
signature, = defs[0].get_signatures() |
| 205 |
assert len(signature.params) == 1 |
| 206 |
assert signature.params[0].name == 'bar' |
| 207 |
|
| 208 |
s = dedent(''' |
| 209 |
def foo(bar): |
| 210 |
pass |
| 211 |
foo''') |
| 212 |
|
| 213 |
check(Script(s).infer()) |
| 214 |
|
| 215 |
check(Script(s).goto()) |
| 216 |
check(Script(s + '\nbar=foo\nbar').goto()) |
| 217 |
|
| 218 |
|
| 219 |
def test_param_endings(Script): |
| 220 |
""" |
| 221 |
Params should be represented without the comma and whitespace they have |
| 222 |
around them. |
| 223 |
""" |
| 224 |
sig, = Script('def x(a, b=5, c=""): pass\n x(').get_signatures() |
| 225 |
assert [p.description for p in sig.params] == ['param a', 'param b=5', 'param c=""'] |
| 226 |
|
| 227 |
|
| 228 |
@pytest.mark.parametrize( |
| 229 |
'code, index, name, is_definition', [ |
| 230 |
('name', 0, 'name', False), |
| 231 |
('a = f(x)', 0, 'a', True), |
| 232 |
('a = f(x)', 1, 'f', False), |
| 233 |
('a = f(x)', 2, 'x', False), |
| 234 |
] |
| 235 |
) |
| 236 |
def test_is_definition(get_names, code, index, name, is_definition): |
| 237 |
d = get_names( |
| 238 |
dedent(code), |
| 239 |
references=True, |
| 240 |
all_scopes=True, |
| 241 |
)[index] |
| 242 |
assert d.name == name |
| 243 |
assert d.is_definition() == is_definition |
| 244 |
|
| 245 |
|
| 246 |
@pytest.mark.parametrize( |
| 247 |
'code, expected', ( |
| 248 |
('import x as a', [False, True]), |
| 249 |
('from x import y', [False, True]), |
| 250 |
('from x.z import y', [False, False, True]), |
| 251 |
) |
| 252 |
) |
| 253 |
def test_is_definition_import(get_names, code, expected): |
| 254 |
ns = get_names(dedent(code), references=True, all_scopes=True) |
| 255 |
|
| 256 |
ns = sorted(ns, key=lambda name: (name.line, name.column)) |
| 257 |
assert [name.is_definition() for name in ns] == expected |
| 258 |
|
| 259 |
|
| 260 |
def test_parent(Script): |
| 261 |
def _parent(source, line=None, column=None): |
| 262 |
def_, = Script(dedent(source)).goto(line, column) |
| 263 |
return def_.parent() |
| 264 |
|
| 265 |
parent = _parent('foo=1\nfoo') |
| 266 |
assert parent.type == 'module' |
| 267 |
|
| 268 |
parent = _parent(''' |
| 269 |
def spam(): |
| 270 |
if 1: |
| 271 |
y=1 |
| 272 |
y''') |
| 273 |
assert parent.name == 'spam' |
| 274 |
assert parent.parent().type == 'module' |
| 275 |
|
| 276 |
|
| 277 |
def test_parent_on_function(Script): |
| 278 |
code = 'def spam():\n pass' |
| 279 |
def_, = Script(code).goto(line=1, column=len('def spam')) |
| 280 |
parent = def_.parent() |
| 281 |
assert parent.name == '__main__' |
| 282 |
assert parent.type == 'module' |
| 283 |
|
| 284 |
|
| 285 |
def test_parent_on_completion_and_else(Script): |
| 286 |
script = Script(dedent('''\ |
| 287 |
class Foo(): |
| 288 |
def bar(name): name |
| 289 |
Foo().bar''')) |
| 290 |
|
| 291 |
bar, = script.complete() |
| 292 |
parent = bar.parent() |
| 293 |
assert parent.name == 'Foo' |
| 294 |
assert parent.type == 'class' |
| 295 |
|
| 296 |
param, name, = [d for d in script.get_names(all_scopes=True, references=True) |
| 297 |
if d.name == 'name'] |
| 298 |
parent = name.parent() |
| 299 |
assert parent.name == 'bar' |
| 300 |
assert parent.type == 'function' |
| 301 |
parent = name.parent().parent() |
| 302 |
assert parent.name == 'Foo' |
| 303 |
assert parent.type == 'class' |
| 304 |
|
| 305 |
parent = param.parent() |
| 306 |
assert parent.name == 'bar' |
| 307 |
assert parent.type == 'function' |
| 308 |
parent = param.parent().parent() |
| 309 |
assert parent.name == 'Foo' |
| 310 |
assert parent.type == 'class' |
| 311 |
|
| 312 |
parent = Script('str.join').complete()[0].parent() |
| 313 |
assert parent.name == 'str' |
| 314 |
assert parent.type == 'class' |
| 315 |
|
| 316 |
|
| 317 |
def test_parent_on_closure(Script): |
| 318 |
script = Script(dedent('''\ |
| 319 |
class Foo(): |
| 320 |
def bar(name): |
| 321 |
def inner(): foo |
| 322 |
return inner''')) |
| 323 |
|
| 324 |
names = script.get_names(all_scopes=True, references=True) |
| 325 |
inner_func, inner_reference = filter(lambda d: d.name == 'inner', names) |
| 326 |
foo, = filter(lambda d: d.name == 'foo', names) |
| 327 |
|
| 328 |
assert foo.parent().name == 'inner' |
| 329 |
assert foo.parent().parent().name == 'bar' |
| 330 |
assert foo.parent().parent().parent().name == 'Foo' |
| 331 |
assert foo.parent().parent().parent().parent().name == '__main__' |
| 332 |
|
| 333 |
assert inner_func.parent().name == 'bar' |
| 334 |
assert inner_func.parent().parent().name == 'Foo' |
| 335 |
assert inner_reference.parent().name == 'bar' |
| 336 |
assert inner_reference.parent().parent().name == 'Foo' |
| 337 |
|
| 338 |
|
| 339 |
def test_parent_on_comprehension(Script): |
| 340 |
ns = Script('''\ |
| 341 |
def spam(): |
| 342 |
return [i for i in range(5)] |
| 343 |
''').get_names(all_scopes=True) |
| 344 |
|
| 345 |
assert [name.name for name in ns] == ['spam', 'i'] |
| 346 |
|
| 347 |
assert ns[0].parent().name == '__main__' |
| 348 |
assert ns[0].parent().type == 'module' |
| 349 |
assert ns[1].parent().name == 'spam' |
| 350 |
assert ns[1].parent().type == 'function' |
| 351 |
|
| 352 |
|
| 353 |
def test_type(Script): |
| 354 |
for c in Script('a = [str()]; a[0].').complete(): |
| 355 |
if c.name == '__class__': |
| 356 |
assert c.type == 'property' |
| 357 |
else: |
| 358 |
assert c.type in ('function', 'statement') |
| 359 |
|
| 360 |
for c in Script('list.').complete(): |
| 361 |
assert c.type |
| 362 |
|
| 363 |
|
| 364 |
for c in Script('import os; os.path.').complete(): |
| 365 |
assert c.type |
| 366 |
|
| 367 |
|
| 368 |
def test_type_II(Script): |
| 369 |
""" |
| 370 |
GitHub Issue #833, `keyword`s are seen as `module`s |
| 371 |
""" |
| 372 |
for c in Script('f').complete(): |
| 373 |
if c.name == 'for': |
| 374 |
assert c.type == 'keyword' |
| 375 |
|
| 376 |
|
| 377 |
@pytest.mark.parametrize( |
| 378 |
'added_code, expected_type, expected_infer_type', [ |
| 379 |
('Foo().x', 'property', 'instance'), |
| 380 |
('Foo.x', 'property', 'property'), |
| 381 |
('Foo().y', 'function', 'function'), |
| 382 |
('Foo.y', 'function', 'function'), |
| 383 |
('Foo().z', 'function', 'function'), |
| 384 |
('Foo.z', 'function', 'function'), |
| 385 |
] |
| 386 |
) |
| 387 |
def test_class_types(goto_or_help_or_infer, added_code, expected_type, |
| 388 |
expected_infer_type): |
| 389 |
code = dedent('''\ |
| 390 |
class Foo: |
| 391 |
@property |
| 392 |
def x(self): return 1 |
| 393 |
@staticmethod |
| 394 |
def y(self): ... |
| 395 |
@classmethod |
| 396 |
def z(self): ... |
| 397 |
''') |
| 398 |
d, = goto_or_help_or_infer(code + added_code) |
| 399 |
if goto_or_help_or_infer.type == 'infer': |
| 400 |
assert d.type == expected_infer_type |
| 401 |
else: |
| 402 |
assert d.type == expected_type |
| 403 |
|
| 404 |
|
| 405 |
""" |
| 406 |
This tests the BaseName.goto function, not the jedi |
| 407 |
function. They are not really different in functionality, but really |
| 408 |
different as an implementation. |
| 409 |
""" |
| 410 |
|
| 411 |
|
| 412 |
def test_goto_repetition(get_names): |
| 413 |
defs = get_names('a = 1; a', references=True, definitions=False) |
| 414 |
|
| 415 |
|
| 416 |
for _ in range(3): |
| 417 |
assert len(defs) == 1 |
| 418 |
ass = defs[0].goto() |
| 419 |
assert ass[0].description == 'a = 1' |
| 420 |
|
| 421 |
|
| 422 |
def test_goto_named_params(get_names): |
| 423 |
src = """\ |
| 424 |
def foo(a=1, bar=2): |
| 425 |
pass |
| 426 |
foo(bar=1) |
| 427 |
""" |
| 428 |
bar = get_names(dedent(src), references=True)[-1] |
| 429 |
param = bar.goto()[0] |
| 430 |
assert (param.line, param.column) == (1, 13) |
| 431 |
assert param.type == 'param' |
| 432 |
|
| 433 |
|
| 434 |
def test_class_call(get_names): |
| 435 |
src = 'from threading import Thread; Thread(group=1)' |
| 436 |
n = get_names(src, references=True)[-1] |
| 437 |
assert n.name == 'group' |
| 438 |
param_def = n.goto()[0] |
| 439 |
assert param_def.name == 'group' |
| 440 |
assert param_def.type == 'param' |
| 441 |
|
| 442 |
|
| 443 |
def test_parentheses(get_names): |
| 444 |
n = get_names('("").upper', references=True)[-1] |
| 445 |
assert n.goto()[0].name == 'upper' |
| 446 |
|
| 447 |
|
| 448 |
def test_import(get_names): |
| 449 |
nms = get_names('from json import load', references=True) |
| 450 |
assert nms[0].name == 'json' |
| 451 |
assert nms[0].type == 'module' |
| 452 |
n = nms[0].goto()[0] |
| 453 |
assert n.name == 'json' |
| 454 |
assert n.type == 'module' |
| 455 |
|
| 456 |
assert nms[1].name == 'load' |
| 457 |
assert nms[1].type == 'function' |
| 458 |
n = nms[1].goto()[0] |
| 459 |
assert n.name == 'load' |
| 460 |
assert n.type == 'function' |
| 461 |
|
| 462 |
nms = get_names('import os; os.path', references=True) |
| 463 |
assert nms[0].name == 'os' |
| 464 |
assert nms[0].type == 'module' |
| 465 |
n = nms[0].goto()[0] |
| 466 |
assert n.name == 'os' |
| 467 |
assert n.type == 'module' |
| 468 |
|
| 469 |
nms = nms[2].goto() |
| 470 |
assert nms |
| 471 |
assert all(n.type == 'module' for n in nms) |
| 472 |
assert 'path' in {n.name for n in nms} |
| 473 |
|
| 474 |
nms = get_names('import os.path', references=True) |
| 475 |
n = nms[0].goto()[0] |
| 476 |
assert n.name == 'os' |
| 477 |
assert n.type == 'module' |
| 478 |
n = nms[1].goto()[0] |
| 479 |
|
| 480 |
|
| 481 |
assert n.name in ('macpath', 'ntpath', 'posixpath', 'os2emxpath') |
| 482 |
assert n.type == 'module' |
| 483 |
|
| 484 |
|
| 485 |
def test_import_alias(get_names): |
| 486 |
nms = get_names('import json as foo', references=True) |
| 487 |
assert nms[0].name == 'json' |
| 488 |
assert nms[0].type == 'module' |
| 489 |
assert nms[0]._name.tree_name.parent.type == 'dotted_as_name' |
| 490 |
n = nms[0].goto()[0] |
| 491 |
assert n.name == 'json' |
| 492 |
assert n.type == 'module' |
| 493 |
assert n._name._value.tree_node.type == 'file_input' |
| 494 |
|
| 495 |
assert nms[1].name == 'foo' |
| 496 |
assert nms[1].type == 'module' |
| 497 |
assert nms[1]._name.tree_name.parent.type == 'dotted_as_name' |
| 498 |
ass = nms[1].goto() |
| 499 |
assert len(ass) == 1 |
| 500 |
assert ass[0].name == 'json' |
| 501 |
assert ass[0].type == 'module' |
| 502 |
assert ass[0]._name._value.tree_node.type == 'file_input' |
| 503 |
|
| 504 |
|
| 505 |
def test_added_equals_to_params(Script): |
| 506 |
def run(rest_source): |
| 507 |
source = dedent(""" |
| 508 |
def foo(bar, baz): |
| 509 |
pass |
| 510 |
""") |
| 511 |
results = Script(source + rest_source).complete() |
| 512 |
assert len(results) == 1 |
| 513 |
return results[0] |
| 514 |
|
| 515 |
assert run('foo(bar').name_with_symbols == 'bar=' |
| 516 |
assert run('foo(bar').complete == '=' |
| 517 |
assert run('foo(bar').get_completion_prefix_length() == 3 |
| 518 |
assert run('foo(bar, baz').complete == '=' |
| 519 |
assert run('foo(bar, baz').get_completion_prefix_length() == 3 |
| 520 |
assert run(' bar').name_with_symbols == 'bar' |
| 521 |
assert run(' bar').complete == '' |
| 522 |
assert run(' bar').get_completion_prefix_length() == 3 |
| 523 |
x = run('foo(bar=isins').name_with_symbols |
| 524 |
assert run('foo(bar=isins').get_completion_prefix_length() == 5 |
| 525 |
assert x == 'isinstance' |
| 526 |
|
| 527 |
|
| 528 |
def test_builtin_module_with_path(Script): |
| 529 |
""" |
| 530 |
This test simply tests if a module from /usr/lib/python3.14/lib-dynload/ has |
| 531 |
a path or not. It shouldn't have a module_path, because that is just |
| 532 |
confusing. |
| 533 |
""" |
| 534 |
semlock, = Script('from _multiprocessing import SemLock').infer() |
| 535 |
assert isinstance(semlock._name, CompiledValueName) |
| 536 |
assert semlock.module_path is None |
| 537 |
assert semlock.in_builtin_module() is True |
| 538 |
assert semlock.name == 'SemLock' |
| 539 |
assert semlock.line is None |
| 540 |
assert semlock.column is None |
| 541 |
|
| 542 |
|
| 543 |
@pytest.mark.parametrize( |
| 544 |
'code, description', [ |
| 545 |
('int', 'instance int'), |
| 546 |
('str.index', 'instance int'), |
| 547 |
('1', None), |
| 548 |
] |
| 549 |
) |
| 550 |
def test_execute(Script, code, description): |
| 551 |
definition, = Script(code).goto() |
| 552 |
definitions = definition.execute() |
| 553 |
if description is None: |
| 554 |
assert not definitions |
| 555 |
else: |
| 556 |
d, = definitions |
| 557 |
assert d.description == description |
| 558 |
|
| 559 |
|
| 560 |
@pytest.mark.parametrize('goto', [False, True, None]) |
| 561 |
@pytest.mark.parametrize( |
| 562 |
'code, name, file_name', [ |
| 563 |
('from pkg import Foo; Foo.foo', 'foo', '__init__.py'), |
| 564 |
('from pkg import Foo; Foo().foo', 'foo', '__init__.py'), |
| 565 |
('from pkg import Foo; Foo.bar', 'bar', 'module.py'), |
| 566 |
('from pkg import Foo; Foo().bar', 'bar', 'module.py'), |
| 567 |
]) |
| 568 |
def test_inheritance_module_path(Script, goto, code, name, file_name): |
| 569 |
base_path = get_example_dir('inheritance', 'pkg') |
| 570 |
whatever_path = base_path.joinpath('NOT_EXISTING.py') |
| 571 |
|
| 572 |
script = Script(code, path=whatever_path) |
| 573 |
if goto is None: |
| 574 |
func, = script.infer() |
| 575 |
else: |
| 576 |
func, = script.goto(follow_imports=goto) |
| 577 |
assert func.type == 'function' |
| 578 |
assert func.name == name |
| 579 |
assert func.module_path == base_path.joinpath(file_name) |
| 580 |
|
| 581 |
|
| 582 |
def test_definition_goto_follow_imports(Script): |
| 583 |
dumps = Script('from json import dumps\ndumps').get_names(references=True)[-1] |
| 584 |
assert dumps.description == 'dumps' |
| 585 |
no_follow, = dumps.goto() |
| 586 |
assert no_follow.description == 'def dumps' |
| 587 |
assert no_follow.line == 1 |
| 588 |
assert no_follow.column == 17 |
| 589 |
assert no_follow.module_name == '__main__' |
| 590 |
follow, = dumps.goto(follow_imports=True) |
| 591 |
assert follow.description == 'def dumps' |
| 592 |
assert follow.line != 1 |
| 593 |
assert follow.module_name == 'json' |
| 594 |
|
| 595 |
|
| 596 |
@pytest.mark.parametrize( |
| 597 |
'code, expected', [ |
| 598 |
('1', 'int'), |
| 599 |
('x = None; x', 'None'), |
| 600 |
('n: Optional[str]; n', 'Optional[str]'), |
| 601 |
('n = None if xxxxx else ""; n', 'Optional[str]'), |
| 602 |
('n = None if xxxxx else str(); n', 'Optional[str]'), |
| 603 |
('n = None if xxxxx else str; n', 'Optional[Type[str]]'), |
| 604 |
('class Foo: pass\nFoo', 'Type[Foo]'), |
| 605 |
('class Foo: pass\nFoo()', 'Foo'), |
| 606 |
|
| 607 |
('n: Type[List[int]]; n', 'Type[List[int]]'), |
| 608 |
('n: Type[List]; n', 'Type[list]'), |
| 609 |
('n: List; n', 'list'), |
| 610 |
('n: List[int]; n', 'List[int]'), |
| 611 |
('n: Iterable[int]; n', 'Iterable[int]'), |
| 612 |
|
| 613 |
('n = [1]; n', 'List[int]'), |
| 614 |
('n = [1, ""]; n', 'List[Union[int, str]]'), |
| 615 |
('n = [1, str(), None]; n', 'List[Optional[Union[int, str]]]'), |
| 616 |
('n = {1, str()}; n', 'Set[Union[int, str]]'), |
| 617 |
('n = (1,); n', 'Tuple[int]'), |
| 618 |
('n = {1: ""}; n', 'Dict[int, str]'), |
| 619 |
('n = {1: "", 1.0: b""}; n', 'Dict[Union[float, int], Union[bytes, str]]'), |
| 620 |
|
| 621 |
('n = next; n', 'Union[next(i: SupportsNext[_T], /) -> _T, ' |
| 622 |
'next(i: SupportsNext[_T], default: _VT, /) -> _T | _VT]'), |
| 623 |
('abs', 'abs(x: SupportsAbs[_T], /) -> _T'), |
| 624 |
('def foo(x, y): return x if xxxx else y\nfoo(str(), 1)\nfoo', |
| 625 |
'foo(x: str, y: int) -> Union[int, str]'), |
| 626 |
('def foo(x, y = None): return x if xxxx else y\nfoo(str(), 1)\nfoo', |
| 627 |
'foo(x: str, y: int=None) -> Union[int, str]'), |
| 628 |
] |
| 629 |
) |
| 630 |
def test_get_type_hint(Script, code, expected): |
| 631 |
code = 'from typing import *\n' + code |
| 632 |
d, = Script(code).goto() |
| 633 |
assert d.get_type_hint() == expected |
| 634 |
|
| 635 |
|
| 636 |
def test_pseudotreenameclass_type(Script): |
| 637 |
assert Script('from typing import Any\n').get_names()[0].type == 'class' |
| 638 |
|
| 639 |
|
| 640 |
cls_code = '''\ |
| 641 |
class AClass: |
| 642 |
"""my class""" |
| 643 |
@staticmethod |
| 644 |
def hello(): |
| 645 |
func_var = 1 |
| 646 |
return func_var |
| 647 |
''' |
| 648 |
|
| 649 |
|
| 650 |
@pytest.mark.parametrize( |
| 651 |
'code, pos, start, end', [ |
| 652 |
('def a_func():\n return "bar"\n', (1, 4), (1, 0), (2, 16)), |
| 653 |
('var1 = 12', (1, 0), (1, 0), (1, 9)), |
| 654 |
('var1 + 1', (1, 0), (1, 0), (1, 4)), |
| 655 |
('class AClass: pass', (1, 6), (1, 0), (1, 18)), |
| 656 |
('class AClass: pass\n', (1, 6), (1, 0), (1, 18)), |
| 657 |
(cls_code, (1, 6), (1, 0), (6, 23)), |
| 658 |
(cls_code, (4, 8), (4, 4), (6, 23)), |
| 659 |
(cls_code, (5, 8), (5, 8), (5, 20)), |
| 660 |
] |
| 661 |
) |
| 662 |
def test_definition_start_end_position(Script, code, pos, start, end): |
| 663 |
'''Tests for definition_start_position and definition_end_position''' |
| 664 |
name = next( |
| 665 |
n for n in Script(code=code).get_names(all_scopes=True, references=True) |
| 666 |
if n._name.tree_name.start_pos <= pos <= n._name.tree_name.end_pos |
| 667 |
) |
| 668 |
assert name.get_definition_start_position() == start |
| 669 |
assert name.get_definition_end_position() == end |