1
0
forked from VimPlug/jedi

A stab at davidhalter/jedi#361 (Flask extension imports)

Both new-style and old-style extensions work, but only when imported
with a 'from'.  There are two skipped tests of the full dotted name
imports.

Also, our fixture has a normal flaskext package, whereas in practice
the flaskext module is injected from a pth file and does not have
__init__.py, we need to figure out to handle that.
This commit is contained in:
Albertas Agejevas
2014-07-27 12:40:35 +02:00
parent e2cdbf61de
commit 13c1f79d5c
7 changed files with 61 additions and 0 deletions

View File

@@ -343,6 +343,25 @@ class _Importer(object):
@memoize_default(NO_DEFAULT)
def follow_file_system(self):
# Handle "magic" Flask extension imports:
# ``flask.ext.foo`` is really ``flask_foo`` or ``flaskext.foo``.
if [part._string for part in self.import_path[:2]] == ['flask', 'ext']:
orig_path = tuple(self.import_path)
part = orig_path[2]
pos = (part._line, part._column)
try:
self.import_path = (
pr.NamePart('flask_' + part._string, part.parent, pos),
) + orig_path[3:]
return self._real_follow_file_system()
except ModuleNotFound as e:
self.import_path = (
pr.NamePart('flaskext', part.parent, pos),
) + orig_path[2:]
return self._real_follow_file_system()
return self._real_follow_file_system()
def _real_follow_file_system(self):
if self.file_path:
sys_path_mod = list(self.sys_path_with_modifications())
if not self.module.has_explicit_absolute_import:

View File

@@ -0,0 +1 @@
Baz = 1

View File

@@ -0,0 +1,2 @@
class Foo(object):
pass

View File

@@ -0,0 +1,2 @@
class Bar(object):
pass

View File

@@ -0,0 +1 @@
Moo = 1

View File

@@ -1,3 +1,6 @@
import os
import sys
import pytest
import jedi
@@ -23,3 +26,36 @@ def test_import_not_in_sys_path():
assert a[0].name == 'str'
a = jedi.Script(path='module.py', line=7).goto_definitions()
assert a[0].name == 'str'
def setup_function(function):
sys.path.append(os.path.join(
os.path.dirname(__file__), 'flask-site-packages'))
def teardown_function(function):
path = os.path.join(os.path.dirname(__file__), 'flask-site-packages')
sys.path.remove(path)
@pytest.mark.parametrize("script,name", [
("from flask.ext import foo; foo.", "Foo"), # flask_foo.py
("from flask.ext import bar; bar.", "Bar"), # flaskext/bar.py
("from flask.ext import baz; baz.", "Baz"), # flask_baz/__init__.py
("from flask.ext import moo; moo.", "Moo"), # flaskext/moo/__init__.py
])
def test_flask_ext(script, name):
"""flask.ext.foo is really imported from flaskext.foo or flask_foo.
"""
assert name in [c.name for c in jedi.Script(script).completions()]
@pytest.mark.xfail
@pytest.mark.parametrize("script,name", [
("import flask.ext.foo; flask.ext.foo.", "Foo"),
("import flask.ext.bar; flask.ext.bar.", "Foo"),
("import flask.ext.baz; flask.ext.baz.", "Foo"),
("import flask.ext.moo; flask.ext.moo.", "Foo"),
])
def test_flask_ext_more(script, name):
assert name in [c.name for c in jedi.Script(script).completions()]