mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
a (temporary) solution for the __file__ access issues in imported modules, see #408
This commit is contained in:
@@ -142,7 +142,7 @@ class BaseDefinition(object):
|
|||||||
stripped = stripped.parent
|
stripped = stripped.parent
|
||||||
if isinstance(stripped, pr.Name):
|
if isinstance(stripped, pr.Name):
|
||||||
stripped = stripped.parent
|
stripped = stripped.parent
|
||||||
return type(stripped).__name__.lower()
|
return type(stripped).__name__.lower().replace('wrapper', '')
|
||||||
|
|
||||||
def _path(self):
|
def _path(self):
|
||||||
"""The module path."""
|
"""The module path."""
|
||||||
|
|||||||
@@ -206,6 +206,8 @@ class NameFinder(object):
|
|||||||
typ = er.Class(evaluator, typ)
|
typ = er.Class(evaluator, typ)
|
||||||
elif isinstance(typ, pr.Function):
|
elif isinstance(typ, pr.Function):
|
||||||
typ = er.Function(evaluator, typ)
|
typ = er.Function(evaluator, typ)
|
||||||
|
elif isinstance(typ, pr.Module):
|
||||||
|
typ = er.ModuleWrapper(evaluator, typ)
|
||||||
if typ.isinstance(er.Function) and resolve_decorator:
|
if typ.isinstance(er.Function) and resolve_decorator:
|
||||||
typ = typ.get_decorated_func()
|
typ = typ.get_decorated_func()
|
||||||
types.append(typ)
|
types.append(typ)
|
||||||
|
|||||||
@@ -473,7 +473,13 @@ def follow_imports(evaluator, scopes):
|
|||||||
result = []
|
result = []
|
||||||
for s in scopes:
|
for s in scopes:
|
||||||
if isinstance(s, pr.Import):
|
if isinstance(s, pr.Import):
|
||||||
result += ImportWrapper(evaluator, s).follow()
|
for r in ImportWrapper(evaluator, s).follow():
|
||||||
|
if isinstance(r, pr.Module) and not isinstance(r,
|
||||||
|
StarImportModule):
|
||||||
|
# TODO This check is strange and feels wrong somehow. Change it.
|
||||||
|
from jedi.evaluate import representation as er
|
||||||
|
r = er.ModuleWrapper(evaluator, r)
|
||||||
|
result.append(r)
|
||||||
else:
|
else:
|
||||||
result.append(s)
|
result.append(s)
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -521,3 +521,23 @@ class FunctionExecution(Executable):
|
|||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<%s of %s>" % (type(self).__name__, self.base)
|
return "<%s of %s>" % (type(self).__name__, self.base)
|
||||||
|
|
||||||
|
|
||||||
|
class ModuleWrapper(pr.Module):
|
||||||
|
def __init__(self, evaluator, module):
|
||||||
|
self._evaluator = evaluator
|
||||||
|
self._module = module
|
||||||
|
|
||||||
|
@memoize_default()
|
||||||
|
def get_defined_names(self):
|
||||||
|
names = ['__file__', '__package__', '__doc__', '__name__', '__version__']
|
||||||
|
# All the additional module attributes are strings.
|
||||||
|
parent = Instance(self._evaluator, compiled.create(self._evaluator, str))
|
||||||
|
module_attributes = [helpers.FakeName(n, parent) for n in names]
|
||||||
|
return self._module.get_defined_names() + module_attributes
|
||||||
|
|
||||||
|
def __getattr__(self, name):
|
||||||
|
return getattr(self._module, name)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<%s: %s>" % (type(self).__name__, self._module)
|
||||||
|
|||||||
@@ -46,8 +46,8 @@ def scope_nested():
|
|||||||
#? ['sqrt']
|
#? ['sqrt']
|
||||||
import_tree.pkg.sqrt
|
import_tree.pkg.sqrt
|
||||||
|
|
||||||
#? ['a', 'pkg']
|
#? ['pkg']
|
||||||
import_tree.
|
import_tree.p
|
||||||
|
|
||||||
#? float()
|
#? float()
|
||||||
import_tree.pkg.mod1.a
|
import_tree.pkg.mod1.a
|
||||||
@@ -317,3 +317,12 @@ else:
|
|||||||
a = not_existing_import
|
a = not_existing_import
|
||||||
#?
|
#?
|
||||||
a
|
a
|
||||||
|
|
||||||
|
# -----------------
|
||||||
|
# magic methods
|
||||||
|
# -----------------
|
||||||
|
|
||||||
|
def magic_method():
|
||||||
|
import keyword
|
||||||
|
#? ['__file__']
|
||||||
|
keyword.__file__
|
||||||
|
|||||||
Reference in New Issue
Block a user