mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 22:14:27 +08:00
Fix an issue with transform_path_to_dotted
This commit is contained in:
@@ -228,18 +228,28 @@ def transform_path_to_dotted(sys_path, module_path):
|
|||||||
# -1 to remove the separator
|
# -1 to remove the separator
|
||||||
module_path = module_path[:-len('__init__') - 1]
|
module_path = module_path[:-len('__init__') - 1]
|
||||||
|
|
||||||
for p in sys_path:
|
def iter_potential_solutions():
|
||||||
if module_path.startswith(p):
|
for p in sys_path:
|
||||||
rest = module_path[len(p):]
|
if module_path.startswith(p):
|
||||||
# On Windows a path can also use a slash.
|
# Strip the trailing slash/backslash
|
||||||
if rest.startswith(os.path.sep) or rest.startswith('/'):
|
rest = module_path[len(p) + 1:]
|
||||||
# Remove a slash in cases it's still there.
|
|
||||||
rest = rest[1:]
|
|
||||||
|
|
||||||
if rest:
|
if rest:
|
||||||
split = rest.split(os.path.sep)
|
split = rest.split(os.path.sep)
|
||||||
for string in split:
|
if not all(split):
|
||||||
if not string:
|
# This means that part of the file path was empty, this
|
||||||
return None, False
|
# is very strange and is probably a file that is called
|
||||||
return tuple(split), is_package
|
# `.py`.
|
||||||
return None, False
|
return
|
||||||
|
yield tuple(split)
|
||||||
|
|
||||||
|
potential_solutions = tuple(iter_potential_solutions())
|
||||||
|
if not potential_solutions:
|
||||||
|
return None, False
|
||||||
|
# Try to find the shortest path, this makes more sense usually, because the
|
||||||
|
# user usually has venvs somewhere. This means that a path like
|
||||||
|
# .tox/py37/lib/python3.7/os.py can be normal for a file. However in that
|
||||||
|
# case we definitely want to return ['os'] as a path and not a crazy
|
||||||
|
# ['.tox', 'py37', 'lib', 'python3.7', 'os']. Keep in mind that this is a
|
||||||
|
# heuristic and there's now ay to "always" do it right.
|
||||||
|
return sorted(potential_solutions, key=lambda p: len(p))[0], is_package
|
||||||
|
|||||||
@@ -91,6 +91,9 @@ _s = ['/a', '/b', '/c/d/']
|
|||||||
(['/foo'], '/foo/bar/__init__.so', ('bar',), True),
|
(['/foo'], '/foo/bar/__init__.so', ('bar',), True),
|
||||||
(['/foo'], '/x/bar.py', None, False),
|
(['/foo'], '/x/bar.py', None, False),
|
||||||
(['/foo'], '/foo/bar.xyz', ('bar.xyz',), False),
|
(['/foo'], '/foo/bar.xyz', ('bar.xyz',), False),
|
||||||
|
|
||||||
|
(['/foo', '/foo/bar'], '/foo/bar/baz', ('baz',), False),
|
||||||
|
(['/foo/bar', '/foo'], '/foo/bar/baz', ('baz',), False),
|
||||||
])
|
])
|
||||||
def test_calculate_dotted_from_path(sys_path_, module_path, expected, is_package):
|
def test_calculate_dotted_from_path(sys_path_, module_path, expected, is_package):
|
||||||
# transform_path_to_dotted expects normalized absolute paths.
|
# transform_path_to_dotted expects normalized absolute paths.
|
||||||
|
|||||||
Reference in New Issue
Block a user