Use pathlib for file ios, because the new parso is out

This commit is contained in:
Dave Halter
2020-08-05 00:52:50 +02:00
parent 94bf83c826
commit f12ed2088a
6 changed files with 17 additions and 14 deletions

View File

@@ -4,6 +4,7 @@ Used only for REPL Completion.
import inspect import inspect
import os import os
from pathlib import Path
from jedi.parser_utils import get_cached_code_lines from jedi.parser_utils import get_cached_code_lines
@@ -190,7 +191,8 @@ def _find_syntax_node_name(inference_state, python_object):
except TypeError: except TypeError:
# The type might not be known (e.g. class_with_dict.__weakref__) # The type might not be known (e.g. class_with_dict.__weakref__)
return None return None
if path is None or not os.path.exists(path): path = None if path is None else Path(path)
if path is None or not path.exists():
# The path might not exist or be e.g. <stdin>. # The path might not exist or be e.g. <stdin>.
return None return None

View File

@@ -3,6 +3,7 @@ import os
import inspect import inspect
import importlib import importlib
import warnings import warnings
from pathlib import Path
from zipimport import zipimporter from zipimport import zipimporter
from importlib.machinery import all_suffixes from importlib.machinery import all_suffixes
@@ -211,7 +212,7 @@ def _from_loader(loader, string):
if code is None: if code is None:
return None, is_package return None, is_package
if isinstance(loader, zipimporter): if isinstance(loader, zipimporter):
return ZipFileIO(module_path, code, cast_path(loader.archive)), is_package return ZipFileIO(module_path, code, Path(cast_path(loader.archive))), is_package
return KnownContentFileIO(module_path, code), is_package return KnownContentFileIO(module_path, code), is_package

View File

@@ -203,11 +203,11 @@ def recurse_find_python_folders_and_files(folder_io, except_paths=()):
# Delete folders that we don't want to iterate over. # Delete folders that we don't want to iterate over.
for file_io in file_ios: for file_io in file_ios:
path = file_io.path path = file_io.path
if path.endswith('.py') or path.endswith('.pyi'): if path.suffix in ('.py', '.pyi'):
if path not in except_paths: if path not in except_paths:
yield None, file_io yield None, file_io
if path.endswith('.gitignore'): if path.name == '.gitignore':
ignored_paths, ignored_names = \ ignored_paths, ignored_names = \
gitignored_lines(root_folder_io, file_io) gitignored_lines(root_folder_io, file_io)
except_paths |= ignored_paths except_paths |= ignored_paths

View File

@@ -164,7 +164,7 @@ def _get_paths_from_buildout_script(inference_state, buildout_script_path):
inference_state, module_node, inference_state, module_node,
file_io=file_io, file_io=file_io,
string_names=None, string_names=None,
code_lines=get_cached_code_lines(inference_state.grammar, str(buildout_script_path)), code_lines=get_cached_code_lines(inference_state.grammar, buildout_script_path),
).as_context() ).as_context()
yield from check_sys_path_modifications(module_context) yield from check_sys_path_modifications(module_context)

View File

@@ -15,7 +15,7 @@ from test.helpers import test_dir, get_example_dir
def test_preload_modules(): def test_preload_modules():
def check_loaded(*modules): def check_loaded(*module_names):
for grammar_cache in cache.parser_cache.values(): for grammar_cache in cache.parser_cache.values():
if None in grammar_cache: if None in grammar_cache:
break break
@@ -25,9 +25,9 @@ def test_preload_modules():
if path is not None and str(path).startswith(str(typeshed.TYPESHED_PATH)) if path is not None and str(path).startswith(str(typeshed.TYPESHED_PATH))
) )
# +1 for None module (currently used) # +1 for None module (currently used)
assert len(grammar_cache) - typeshed_cache_count == len(modules) + 1 assert len(grammar_cache) - typeshed_cache_count == len(module_names) + 1
for i in modules: for i in module_names:
assert [i in k for k in grammar_cache.keys() if k is not None] assert [i in str(k) for k in grammar_cache.keys() if k is not None]
old_cache = cache.parser_cache.copy() old_cache = cache.parser_cache.copy()
cache.parser_cache.clear() cache.parser_cache.clear()

View File

@@ -29,13 +29,13 @@ def test_find_module_basic():
def test_find_module_package(): def test_find_module_package():
file_io, is_package = _find_module('json') file_io, is_package = _find_module('json')
assert file_io.path.endswith(os.path.join('json', '__init__.py')) assert file_io.path.parts[-2:] == ('json', '__init__.py')
assert is_package is True assert is_package is True
def test_find_module_not_package(): def test_find_module_not_package():
file_io, is_package = _find_module('io') file_io, is_package = _find_module('io')
assert file_io.path.endswith('io.py') assert file_io.path.name == 'io.py'
assert is_package is False assert is_package is False
@@ -55,8 +55,8 @@ def test_find_module_package_zipped(Script, inference_state, environment):
full_name='pkg' full_name='pkg'
) )
assert file_io is not None assert file_io is not None
assert file_io.path.endswith(os.path.join('pkg.zip', 'pkg', '__init__.py')) assert file_io.path.parts[-3:] == ('pkg.zip', 'pkg', '__init__.py')
assert file_io._zip_path.endswith('pkg.zip') assert file_io._zip_path.name == 'pkg.zip'
assert is_package is True assert is_package is True
@@ -108,7 +108,7 @@ def test_find_module_not_package_zipped(Script, inference_state, environment):
string='not_pkg', string='not_pkg',
full_name='not_pkg' full_name='not_pkg'
) )
assert file_io.path.endswith(os.path.join('not_pkg.zip', 'not_pkg.py')) assert file_io.path.parts[-2:] == ('not_pkg.zip', 'not_pkg.py')
assert is_package is False assert is_package is False