| 1 |
""" |
| 2 |
Tests of various import related things that could not be tested with "Black Box |
| 3 |
Tests". |
| 4 |
""" |
| 5 |
|
| 6 |
import os |
| 7 |
from pathlib import Path |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
import jedi |
| 12 |
from jedi.file_io import FileIO |
| 13 |
from jedi.inference import compiled |
| 14 |
from jedi.inference import imports |
| 15 |
from jedi.api.project import Project |
| 16 |
from jedi.inference.gradual.conversion import _stub_to_python_value_set |
| 17 |
from jedi.inference.references import get_module_contexts_containing_name |
| 18 |
from ..helpers import get_example_dir, test_dir, test_dir_project, root_dir |
| 19 |
from jedi.inference.compiled.subprocess.functions import _find_module_py33, _find_module |
| 20 |
|
| 21 |
THIS_DIR = os.path.dirname(__file__) |
| 22 |
|
| 23 |
|
| 24 |
def test_find_module_basic(): |
| 25 |
"""Needs to work like the old find_module.""" |
| 26 |
assert _find_module_py33('_io') == (None, False) |
| 27 |
with pytest.raises(ImportError): |
| 28 |
assert _find_module_py33('_DOESNTEXIST_') == (None, None) |
| 29 |
|
| 30 |
|
| 31 |
def test_find_module_package(): |
| 32 |
file_io, is_package = _find_module('json') |
| 33 |
assert file_io.path.parts[-2:] == ('json', '__init__.py') |
| 34 |
assert is_package is True |
| 35 |
|
| 36 |
|
| 37 |
def test_find_module_not_package(): |
| 38 |
file_io, is_package = _find_module('io') |
| 39 |
assert file_io.path.name == 'io.py' |
| 40 |
assert is_package is False |
| 41 |
|
| 42 |
|
| 43 |
pkg_zip_path = Path(get_example_dir('zipped_imports', 'pkg.zip')) |
| 44 |
|
| 45 |
|
| 46 |
def test_find_module_package_zipped(Script, inference_state, environment): |
| 47 |
sys_path = environment.get_sys_path() + [str(pkg_zip_path)] |
| 48 |
|
| 49 |
project = Project('.', sys_path=sys_path) |
| 50 |
script = Script('import pkg; pkg.mod', project=project) |
| 51 |
assert len(script.complete()) == 1 |
| 52 |
|
| 53 |
file_io, is_package = inference_state.compiled_subprocess.get_module_info( |
| 54 |
sys_path=sys_path, |
| 55 |
string='pkg', |
| 56 |
full_name='pkg' |
| 57 |
) |
| 58 |
assert file_io is not None |
| 59 |
assert file_io.path.parts[-3:] == ('pkg.zip', 'pkg', '__init__.py') |
| 60 |
assert file_io._zip_path.name == 'pkg.zip' |
| 61 |
assert is_package is True |
| 62 |
|
| 63 |
|
| 64 |
@pytest.mark.parametrize( |
| 65 |
'code, file, package, path', [ |
| 66 |
('import pkg', '__init__.py', 'pkg', 'pkg'), |
| 67 |
('import pkg', '__init__.py', 'pkg', 'pkg'), |
| 68 |
|
| 69 |
('from pkg import module', 'module.py', 'pkg', None), |
| 70 |
('from pkg.module', 'module.py', 'pkg', None), |
| 71 |
|
| 72 |
('from pkg import nested', os.path.join('nested', '__init__.py'), |
| 73 |
'pkg.nested', os.path.join('pkg', 'nested')), |
| 74 |
('from pkg.nested', os.path.join('nested', '__init__.py'), |
| 75 |
'pkg.nested', os.path.join('pkg', 'nested')), |
| 76 |
|
| 77 |
('from pkg.nested import nested_module', |
| 78 |
os.path.join('nested', 'nested_module.py'), 'pkg.nested', None), |
| 79 |
('from pkg.nested.nested_module', |
| 80 |
os.path.join('nested', 'nested_module.py'), 'pkg.nested', None), |
| 81 |
|
| 82 |
('from pkg.namespace import namespace_module', |
| 83 |
os.path.join('namespace', 'namespace_module.py'), 'pkg.namespace', None), |
| 84 |
('from pkg.namespace.namespace_module', |
| 85 |
os.path.join('namespace', 'namespace_module.py'), 'pkg.namespace', None), |
| 86 |
] |
| 87 |
|
| 88 |
) |
| 89 |
def test_correct_zip_package_behavior(Script, inference_state, environment, code, |
| 90 |
file, package, path): |
| 91 |
sys_path = environment.get_sys_path() + [str(pkg_zip_path)] |
| 92 |
pkg, = Script(code, project=Project('.', sys_path=sys_path)).infer() |
| 93 |
value, = pkg._name.infer() |
| 94 |
assert value.py__file__() == pkg_zip_path.joinpath('pkg', file) |
| 95 |
assert '.'.join(value.py__package__()) == package |
| 96 |
assert value.is_package() is (path is not None) |
| 97 |
if path is not None: |
| 98 |
assert value.py__path__() == [str(pkg_zip_path.joinpath(path))] |
| 99 |
|
| 100 |
value.string_names = None |
| 101 |
assert value.py__package__() == [] |
| 102 |
|
| 103 |
|
| 104 |
@pytest.mark.parametrize("code,names", [ |
| 105 |
("from pkg.", {"module", "nested", "namespace"}), |
| 106 |
("from pkg.nested.", {"nested_module"}) |
| 107 |
]) |
| 108 |
def test_zip_package_import_complete(Script, environment, code, names): |
| 109 |
sys_path = environment.get_sys_path() + [str(pkg_zip_path)] |
| 110 |
completions = Script(code, project=Project('.', sys_path=sys_path)).complete() |
| 111 |
assert names == {c.name for c in completions} |
| 112 |
|
| 113 |
|
| 114 |
def test_find_module_not_package_zipped(Script, inference_state, environment): |
| 115 |
path = get_example_dir('zipped_imports', 'not_pkg.zip') |
| 116 |
sys_path = environment.get_sys_path() + [path] |
| 117 |
script = Script('import not_pkg; not_pkg.val', project=Project('.', sys_path=sys_path)) |
| 118 |
assert len(script.complete()) == 1 |
| 119 |
|
| 120 |
file_io, is_package = inference_state.compiled_subprocess.get_module_info( |
| 121 |
sys_path=map(str, sys_path), |
| 122 |
string='not_pkg', |
| 123 |
full_name='not_pkg' |
| 124 |
) |
| 125 |
assert file_io.path.parts[-2:] == ('not_pkg.zip', 'not_pkg.py') |
| 126 |
assert is_package is False |
| 127 |
|
| 128 |
|
| 129 |
def test_import_not_in_sys_path(Script, environment): |
| 130 |
""" |
| 131 |
non-direct imports (not in sys.path) |
| 132 |
|
| 133 |
This is in the end just a fallback. |
| 134 |
""" |
| 135 |
path = get_example_dir() |
| 136 |
module_path = os.path.join(path, 'not_in_sys_path', 'pkg', 'module.py') |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
project = Project(path, sys_path=environment.get_sys_path()) |
| 141 |
a = Script(path=module_path, project=project).infer(line=5) |
| 142 |
assert a[0].name == 'int' |
| 143 |
|
| 144 |
a = Script(path=module_path, project=project).infer(line=6) |
| 145 |
assert a[0].name == 'str' |
| 146 |
a = Script(path=module_path, project=project).infer(line=7) |
| 147 |
assert a[0].name == 'str' |
| 148 |
|
| 149 |
|
| 150 |
@pytest.mark.parametrize("code,name", [ |
| 151 |
("from flask.ext import foo; foo.", "Foo"), |
| 152 |
("from flask.ext import bar; bar.", "Bar"), |
| 153 |
("from flask.ext import baz; baz.", "Baz"), |
| 154 |
("from flask.ext import moo; moo.", "Moo"), |
| 155 |
("from flask.ext.", "foo"), |
| 156 |
("from flask.ext.", "bar"), |
| 157 |
("from flask.ext.", "baz"), |
| 158 |
("from flask.ext.", "moo"), |
| 159 |
pytest.param("import flask.ext.foo; flask.ext.foo.", "Foo", marks=pytest.mark.xfail), |
| 160 |
pytest.param("import flask.ext.bar; flask.ext.bar.", "Foo", marks=pytest.mark.xfail), |
| 161 |
pytest.param("import flask.ext.baz; flask.ext.baz.", "Foo", marks=pytest.mark.xfail), |
| 162 |
pytest.param("import flask.ext.moo; flask.ext.moo.", "Foo", marks=pytest.mark.xfail), |
| 163 |
]) |
| 164 |
def test_flask_ext(Script, code, name): |
| 165 |
"""flask.ext.foo is really imported from flaskext.foo or flask_foo. |
| 166 |
""" |
| 167 |
path = get_example_dir('flask-site-packages') |
| 168 |
completions = Script(code, project=Project('.', sys_path=[path])).complete() |
| 169 |
assert name in [c.name for c in completions] |
| 170 |
|
| 171 |
|
| 172 |
def test_not_importable_file(Script): |
| 173 |
src = 'import not_importable_file as x; x.' |
| 174 |
assert not Script(src, path='example.py', project=test_dir_project).complete() |
| 175 |
|
| 176 |
|
| 177 |
def test_import_unique(Script): |
| 178 |
src = "import os; os.path" |
| 179 |
defs = Script(src, path='example.py').infer() |
| 180 |
parent_contexts = [d._name._value for d in defs] |
| 181 |
assert len(parent_contexts) == len(set(parent_contexts)) |
| 182 |
|
| 183 |
|
| 184 |
def test_cache_works_with_sys_path_param(Script, tmpdir): |
| 185 |
foo_path = tmpdir.join('foo') |
| 186 |
bar_path = tmpdir.join('bar') |
| 187 |
foo_path.join('module.py').write('foo = 123', ensure=True) |
| 188 |
bar_path.join('module.py').write('bar = 123', ensure=True) |
| 189 |
foo_completions = Script( |
| 190 |
'import module; module.', |
| 191 |
project=Project('.', sys_path=[foo_path.strpath]), |
| 192 |
).complete() |
| 193 |
bar_completions = Script( |
| 194 |
'import module; module.', |
| 195 |
project=Project('.', sys_path=[bar_path.strpath]), |
| 196 |
).complete() |
| 197 |
assert 'foo' in [c.name for c in foo_completions] |
| 198 |
assert 'bar' not in [c.name for c in foo_completions] |
| 199 |
|
| 200 |
assert 'bar' in [c.name for c in bar_completions] |
| 201 |
assert 'foo' not in [c.name for c in bar_completions] |
| 202 |
|
| 203 |
|
| 204 |
def test_import_completion_docstring(Script): |
| 205 |
import abc |
| 206 |
s = Script('"""test"""\nimport ab') |
| 207 |
abc_completions = [c for c in s.complete() if c.name == 'abc'] |
| 208 |
assert len(abc_completions) == 1 |
| 209 |
assert abc_completions[0].docstring(fast=False) == abc.__doc__ |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
assert abc_completions[0].docstring() == '' |
| 214 |
|
| 215 |
|
| 216 |
def test_goto_definition_on_import(Script): |
| 217 |
assert Script("import sys_blabla").infer(1, 8) == [] |
| 218 |
assert len(Script("import sys").infer(1, 8)) == 1 |
| 219 |
|
| 220 |
|
| 221 |
def test_complete_on_empty_import(ScriptWithProject): |
| 222 |
path = os.path.join(test_dir, 'whatever.py') |
| 223 |
assert ScriptWithProject("from datetime import").complete()[0].name == 'import' |
| 224 |
|
| 225 |
assert 10 < len(ScriptWithProject("from .", path=path).complete()) < 30 |
| 226 |
|
| 227 |
|
| 228 |
assert len(ScriptWithProject("from . import", path=path).complete(1, 5)) > 30 |
| 229 |
|
| 230 |
assert 10 < len(ScriptWithProject("from . import", path=path).complete(1, 6)) < 30 |
| 231 |
|
| 232 |
|
| 233 |
assert len(ScriptWithProject("from . import classes", path=path).complete(1, 5)) > 30 |
| 234 |
|
| 235 |
assert 10 < len(ScriptWithProject("from . import classes", path=path).complete(1, 6)) < 30 |
| 236 |
|
| 237 |
wanted = {'ImportError', 'import', 'ImportWarning'} |
| 238 |
assert {c.name for c in ScriptWithProject("import").complete()} == wanted |
| 239 |
assert len(ScriptWithProject("import import", path=path).complete()) > 0 |
| 240 |
|
| 241 |
|
| 242 |
assert ScriptWithProject("from datetime import").complete()[0].name == 'import' |
| 243 |
assert ScriptWithProject("from datetime import ").complete() |
| 244 |
|
| 245 |
|
| 246 |
def test_imports_on_global_namespace_without_path(Script): |
| 247 |
"""If the path is None, there shouldn't be any import problem""" |
| 248 |
completions = Script("import operator").complete() |
| 249 |
assert [c.name for c in completions] == ['operator'] |
| 250 |
completions = Script("import operator", path='example.py').complete() |
| 251 |
assert [c.name for c in completions] == ['operator'] |
| 252 |
|
| 253 |
|
| 254 |
completions = Script("import keyword", path='example.py').complete() |
| 255 |
assert [c.name for c in completions] == ['keyword'] |
| 256 |
completions = Script("import keyword").complete() |
| 257 |
assert [c.name for c in completions] == ['keyword'] |
| 258 |
|
| 259 |
|
| 260 |
def test_named_import(Script): |
| 261 |
"""named import - jedi-vim issue #8""" |
| 262 |
s = "import time as dt" |
| 263 |
assert len(Script(s, path='/').infer(1, 15)) == 1 |
| 264 |
assert len(Script(s, path='/').infer(1, 10)) == 1 |
| 265 |
|
| 266 |
|
| 267 |
def test_nested_import(Script): |
| 268 |
s = "import multiprocessing.dummy; multiprocessing.dummy" |
| 269 |
g = Script(s).goto() |
| 270 |
assert len(g) == 1 |
| 271 |
assert (g[0].line, g[0].column) != (0, 0) |
| 272 |
|
| 273 |
|
| 274 |
def test_goto(Script): |
| 275 |
sys, = Script("import sys").goto(follow_imports=True) |
| 276 |
assert sys.type == 'module' |
| 277 |
|
| 278 |
|
| 279 |
def test_os_after_from(Script): |
| 280 |
def check(source, result, column=None): |
| 281 |
completions = Script(source).complete(column=column) |
| 282 |
assert [c.name for c in completions] == result |
| 283 |
|
| 284 |
check('\nfrom os. ', ['path']) |
| 285 |
check('\nfrom os ', ['import']) |
| 286 |
check('from os ', ['import']) |
| 287 |
check('\nfrom os import whatever', ['import'], len('from os im')) |
| 288 |
|
| 289 |
check('from os\\\n', ['import']) |
| 290 |
check('from os \\\n', ['import']) |
| 291 |
|
| 292 |
|
| 293 |
def test_os_issues(Script): |
| 294 |
def import_names(*args, **kwargs): |
| 295 |
return [d.name for d in Script(*args).complete(**kwargs)] |
| 296 |
|
| 297 |
|
| 298 |
s = 'import os, s' |
| 299 |
assert 'sys' in import_names(s) |
| 300 |
assert 'os' in import_names(s, column=len(s) - 3) |
| 301 |
|
| 302 |
|
| 303 |
s = 'from os import path, e' |
| 304 |
assert 'environ' in import_names(s) |
| 305 |
assert 'json' not in import_names(s, column=len(s) - 1) |
| 306 |
assert 'environ' in import_names(s, column=len(s) - 1) |
| 307 |
assert 'path' in import_names(s, column=len(s) - 3) |
| 308 |
|
| 309 |
|
| 310 |
def test_duplicated_import(Script): |
| 311 |
def import_names(*args, **kwargs): |
| 312 |
return [d.name for d in Script(*args).complete(**kwargs)] |
| 313 |
|
| 314 |
s = 'import os, o' |
| 315 |
assert 'os' not in import_names(s) |
| 316 |
assert 'os' in import_names(s, column=len(s) - 3) |
| 317 |
|
| 318 |
s = 'from os import path, p' |
| 319 |
assert 'path' not in import_names(s) |
| 320 |
assert 'path' in import_names(s, column=len(s) - 3) |
| 321 |
assert 'path' in import_names("from os import path") |
| 322 |
assert 'path' in import_names("from os import chdir, path") |
| 323 |
|
| 324 |
s = 'import math as mm, m' |
| 325 |
assert 'math' not in import_names(s) |
| 326 |
|
| 327 |
s = 'import math as os, o' |
| 328 |
assert 'os' in import_names(s) |
| 329 |
|
| 330 |
s = 'from os import path as pp, p' |
| 331 |
assert 'path' not in import_names(s) |
| 332 |
|
| 333 |
s = 'from os import chdir as path, p' |
| 334 |
assert 'path' in import_names(s) |
| 335 |
|
| 336 |
|
| 337 |
def test_path_issues(Script): |
| 338 |
""" |
| 339 |
See pull request #684 for details. |
| 340 |
""" |
| 341 |
source = '''from datetime import ''' |
| 342 |
assert Script(source).complete() |
| 343 |
|
| 344 |
|
| 345 |
def test_compiled_import_none(monkeypatch, Script): |
| 346 |
""" |
| 347 |
Related to #1079. An import might somehow fail and return None. |
| 348 |
""" |
| 349 |
script = Script('import sys') |
| 350 |
monkeypatch.setattr(compiled, 'load_module', lambda *args, **kwargs: None) |
| 351 |
def_, = script.infer() |
| 352 |
assert def_.type == 'module' |
| 353 |
value, = def_._name.infer() |
| 354 |
assert not _stub_to_python_value_set(value) |
| 355 |
|
| 356 |
|
| 357 |
@pytest.mark.parametrize( |
| 358 |
('path', 'is_package', 'goal'), [ |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
(os.path.join(THIS_DIR, 'test_docstring.py'), False, |
| 363 |
('test_inference', 'test_imports')), |
| 364 |
(os.path.join(THIS_DIR, '__init__.py'), True, |
| 365 |
('test_inference', 'test_imports')), |
| 366 |
] |
| 367 |
) |
| 368 |
def test_get_modules_containing_name(inference_state, path, goal, is_package): |
| 369 |
inference_state.project = Project(test_dir) |
| 370 |
module = imports._load_python_module( |
| 371 |
inference_state, |
| 372 |
FileIO(path), |
| 373 |
import_names=('ok', 'lala', 'x'), |
| 374 |
is_package=is_package, |
| 375 |
) |
| 376 |
assert module |
| 377 |
module_context = module.as_context() |
| 378 |
input_module, found_module = get_module_contexts_containing_name( |
| 379 |
inference_state, |
| 380 |
[module_context], |
| 381 |
'string_that_only_exists_here' |
| 382 |
) |
| 383 |
assert input_module is module_context |
| 384 |
assert found_module.string_names == goal |
| 385 |
|
| 386 |
|
| 387 |
@pytest.mark.parametrize( |
| 388 |
'path', ('api/whatever/test_this.py', 'api/whatever/file')) |
| 389 |
@pytest.mark.parametrize('empty_sys_path', (False, True)) |
| 390 |
def test_relative_imports_with_multiple_similar_directories(Script, path, empty_sys_path): |
| 391 |
dir = get_example_dir('issue1209') |
| 392 |
if empty_sys_path: |
| 393 |
project = Project(dir, sys_path=(), smart_sys_path=False) |
| 394 |
else: |
| 395 |
project = Project(dir) |
| 396 |
script = Script( |
| 397 |
"from . ", |
| 398 |
path=os.path.join(dir, path), |
| 399 |
project=project, |
| 400 |
) |
| 401 |
name, import_ = script.complete() |
| 402 |
assert import_.name == 'import' |
| 403 |
assert name.name == 'api_test1' |
| 404 |
|
| 405 |
|
| 406 |
def test_relative_imports_with_outside_paths(Script): |
| 407 |
dir = get_example_dir('issue1209') |
| 408 |
project = Project(dir, sys_path=[], smart_sys_path=False) |
| 409 |
script = Script( |
| 410 |
"from ...", |
| 411 |
path=os.path.join(dir, 'api/whatever/test_this.py'), |
| 412 |
project=project, |
| 413 |
) |
| 414 |
assert [c.name for c in script.complete()] == ['api', 'whatever'] |
| 415 |
|
| 416 |
script = Script( |
| 417 |
"from " + '.' * 100, |
| 418 |
path=os.path.join(dir, 'api/whatever/test_this.py'), |
| 419 |
project=project, |
| 420 |
) |
| 421 |
assert not script.complete() |
| 422 |
|
| 423 |
|
| 424 |
def test_relative_imports_without_path(Script): |
| 425 |
path = get_example_dir('issue1209', 'api', 'whatever') |
| 426 |
project = Project(path, sys_path=[], smart_sys_path=False) |
| 427 |
script = Script("from . ", project=project) |
| 428 |
assert [c.name for c in script.complete()] == ['api_test1', 'import'] |
| 429 |
|
| 430 |
script = Script("from .. ", project=project) |
| 431 |
assert [c.name for c in script.complete()] == ['import', 'whatever'] |
| 432 |
|
| 433 |
script = Script("from ... ", project=project) |
| 434 |
assert [c.name for c in script.complete()] == ['api', 'import', 'whatever'] |
| 435 |
|
| 436 |
|
| 437 |
def test_relative_import_out_of_file_system(Script): |
| 438 |
code = "from " + '.' * 100 |
| 439 |
assert not Script(code).complete() |
| 440 |
script = Script(code + ' ') |
| 441 |
import_, = script.complete() |
| 442 |
assert import_.name == 'import' |
| 443 |
|
| 444 |
script = Script("from " + '.' * 100 + 'abc import ABCMeta') |
| 445 |
assert not script.infer() |
| 446 |
assert not script.complete() |
| 447 |
|
| 448 |
|
| 449 |
@pytest.mark.parametrize( |
| 450 |
'level, directory, project_path, result', [ |
| 451 |
(1, '/a/b/c', '/a', (['b', 'c'], '/a')), |
| 452 |
(2, '/a/b/c', '/a', (['b'], '/a')), |
| 453 |
(3, '/a/b/c', '/a', ([], '/a')), |
| 454 |
(4, '/a/b/c', '/a', (None, '/')), |
| 455 |
(5, '/a/b/c', '/a', (None, None)), |
| 456 |
(1, '/', '/', ([], '/')), |
| 457 |
(2, '/', '/', (None, None)), |
| 458 |
(1, '/a/b', '/a/b/c', (None, '/a/b')), |
| 459 |
(2, '/a/b', '/a/b/c', (None, '/a')), |
| 460 |
(3, '/a/b', '/a/b/c', (None, '/')), |
| 461 |
] |
| 462 |
) |
| 463 |
def test_level_to_import_path(level, directory, project_path, result): |
| 464 |
assert imports._level_to_base_import_path(project_path, directory, level) == result |
| 465 |
|
| 466 |
|
| 467 |
def test_import_name_calculation(Script): |
| 468 |
s = Script(path=os.path.join(test_dir, 'completion', 'isinstance.py')) |
| 469 |
m = s._get_module_context() |
| 470 |
assert m.string_names == ('test', 'completion', 'isinstance') |
| 471 |
|
| 472 |
|
| 473 |
@pytest.mark.parametrize('name', ('builtins', 'typing')) |
| 474 |
def test_pre_defined_imports_module(Script, environment, name): |
| 475 |
path = os.path.join(root_dir, name + '.py') |
| 476 |
module = Script('', path=path)._get_module_context() |
| 477 |
assert module.string_names == (name,) |
| 478 |
|
| 479 |
assert str(module.inference_state.builtins_module.py__file__()) != path |
| 480 |
assert str(module.inference_state.typing_module.py__file__()) != path |
| 481 |
|
| 482 |
|
| 483 |
@pytest.mark.parametrize('name', ('builtins', 'typing')) |
| 484 |
def test_import_needed_modules_by_jedi(Script, environment, tmpdir, name): |
| 485 |
module_path = tmpdir.join(name + '.py') |
| 486 |
module_path.write('int = ...') |
| 487 |
script = Script( |
| 488 |
'import ' + name, |
| 489 |
path=tmpdir.join('something.py').strpath, |
| 490 |
project=Project('.', sys_path=[tmpdir.strpath] + environment.get_sys_path()), |
| 491 |
) |
| 492 |
module, = script.infer() |
| 493 |
assert str(module._inference_state.builtins_module.py__file__()) != module_path |
| 494 |
assert str(module._inference_state.typing_module.py__file__()) != module_path |
| 495 |
|
| 496 |
|
| 497 |
def test_import_with_semicolon(Script): |
| 498 |
names = [c.name for c in Script('xzy; from abc import ').complete()] |
| 499 |
assert 'ABCMeta' in names |
| 500 |
assert 'abc' not in names |
| 501 |
|
| 502 |
|
| 503 |
def test_relative_import_star(Script): |
| 504 |
|
| 505 |
source = """ |
| 506 |
from . import * |
| 507 |
furl.c |
| 508 |
""" |
| 509 |
script = Script(source, path='export.py') |
| 510 |
|
| 511 |
assert script.complete(3, len("furl.c")) |
| 512 |
|
| 513 |
|
| 514 |
@pytest.mark.parametrize('with_init', [False, True]) |
| 515 |
def test_relative_imports_without_path_and_setup_py( |
| 516 |
Script, inference_state, environment, tmpdir, with_init): |
| 517 |
|
| 518 |
|
| 519 |
tmpdir.join('file1.py').write('do_foo = 1') |
| 520 |
other_path = tmpdir.join('other_files') |
| 521 |
other_path.join('file2.py').write('def do_nothing():\n pass', ensure=True) |
| 522 |
if with_init: |
| 523 |
other_path.join('__init__.py').write('') |
| 524 |
|
| 525 |
for name, code in [('file2', 'from . import file2'), |
| 526 |
('file1', 'from .. import file1')]: |
| 527 |
for func in (jedi.Script.goto, jedi.Script.infer): |
| 528 |
n, = func(Script(code, path=other_path.join('test1.py').strpath)) |
| 529 |
assert n.name == name |
| 530 |
assert n.type == 'module' |
| 531 |
assert n.line == 1 |
| 532 |
|
| 533 |
|
| 534 |
def test_import_recursion(Script): |
| 535 |
path = get_example_dir('import-recursion', "cq_example.py") |
| 536 |
for c in Script(path=path).complete(3, 3): |
| 537 |
c.docstring() |