This commit is contained in:
ANtlord
2019-10-21 22:27:06 +03:00
5 changed files with 68 additions and 58 deletions

View File

@@ -11,8 +11,8 @@ Changelog
0.15.0 (2019-08-11)
+++++++++++++++++++
- Added file path completions, there's a **new ``Completion.type``** ``path``,
now. Example: ``'/ho`` -> ``'/home/``
- Added file path completions, there's a **new** ``Completion.type`` now:
``path``. Example: ``'/ho`` -> ``'/home/``
- ``*args``/``**kwargs`` resolving. If possible Jedi replaces the parameters
with the actual alternatives.
- Better support for enums/dataclasses
@@ -28,7 +28,7 @@ New APIs:
following additional attributes ``infer_default()``, ``infer_annotation()``,
``to_string()``, and ``kind``.
- ``Definition.execute() -> List[Definition]``, makes it possible to infer
return values of functions.
return values of functions.
0.14.1 (2019-07-13)

View File

@@ -10,7 +10,6 @@ from . import refactor
import jedi
from jedi.api.environment import InterpreterEnvironment
from jedi.inference.analysis import Warning
def pytest_addoption(parser):
@@ -85,46 +84,7 @@ def collect_static_analysis_tests(base_dir, test_files):
files_to_execute = [a for a in test_files.items() if a[0] in f_name]
if f_name.endswith(".py") and (not test_files or files_to_execute):
path = os.path.join(base_dir, f_name)
yield StaticAnalysisCase(path)
class StaticAnalysisCase(object):
"""
Static Analysis cases lie in the static_analysis folder.
The tests also start with `#!`, like the goto_definition tests.
"""
def __init__(self, path):
self._path = path
self.name = os.path.basename(path)
with open(path) as f:
self._source = f.read()
self.skip = False
for line in self._source.splitlines():
self.skip = self.skip or run.skip_python_version(line)
def collect_comparison(self):
cases = []
for line_nr, line in enumerate(self._source.splitlines(), 1):
match = re.match(r'(\s*)#! (\d+ )?(.*)$', line)
if match is not None:
column = int(match.group(2) or 0) + len(match.group(1))
cases.append((line_nr + 1, column, match.group(3)))
return cases
def run(self, compare_cb, environment):
analysis = jedi.Script(
self._source,
path=self._path,
environment=environment,
)._analysis()
typ_str = lambda inst: 'warning ' if isinstance(inst, Warning) else ''
analysis = [(r.line, r.column, typ_str(r) + r.name)
for r in analysis]
compare_cb(self, analysis, self.collect_comparison())
def __repr__(self):
return "<%s: %s>" % (self.__class__.__name__, os.path.basename(self._path))
yield run.StaticAnalysisCase(path)
@pytest.fixture(scope='session')

View File

@@ -127,6 +127,7 @@ from jedi.api.completion import get_user_context
from jedi import parser_utils
from jedi.api.environment import get_default_environment, get_system_environment
from jedi.inference.gradual.conversion import convert_values
from jedi.inference.analysis import Warning
TEST_COMPLETIONS = 0
@@ -138,16 +139,8 @@ TEST_USAGES = 3
grammar36 = parso.load_grammar(version='3.6')
class IntegrationTestCase(object):
def __init__(self, test_type, correct, line_nr, column, start, line,
path=None, skip_version_info=None):
self.test_type = test_type
self.correct = correct
self.line_nr = line_nr
self.column = column
self.start = start
self.line = line
self.path = path
class BaseTestCase(object):
def __init__(self, skip_version_info=None):
self._skip_version_info = skip_version_info
self._skip = None
@@ -175,6 +168,19 @@ class IntegrationTestCase(object):
operator_, min_version[0], min_version[1]
)
class IntegrationTestCase(BaseTestCase):
def __init__(self, test_type, correct, line_nr, column, start, line,
path=None, skip_version_info=None):
super(IntegrationTestCase, self).__init__(skip_version_info)
self.test_type = test_type
self.correct = correct
self.line_nr = line_nr
self.column = column
self.start = start
self.line = line
self.path = path
@property
def module_name(self):
return os.path.splitext(os.path.basename(self.path))[0]
@@ -278,6 +284,47 @@ class IntegrationTestCase(object):
return compare_cb(self, compare, sorted(wanted))
class StaticAnalysisCase(BaseTestCase):
"""
Static Analysis cases lie in the static_analysis folder.
The tests also start with `#!`, like the goto_definition tests.
"""
def __init__(self, path):
self._path = path
self.name = os.path.basename(path)
with open(path) as f:
self._source = f.read()
skip_version_info = None
for line in self._source.splitlines():
skip_version_info = skip_python_version(line) or skip_version_info
super(StaticAnalysisCase, self).__init__(skip_version_info)
def collect_comparison(self):
cases = []
for line_nr, line in enumerate(self._source.splitlines(), 1):
match = re.match(r'(\s*)#! (\d+ )?(.*)$', line)
if match is not None:
column = int(match.group(2) or 0) + len(match.group(1))
cases.append((line_nr + 1, column, match.group(3)))
return cases
def run(self, compare_cb, environment):
analysis = jedi.Script(
self._source,
path=self._path,
environment=environment,
)._analysis()
typ_str = lambda inst: 'warning ' if isinstance(inst, Warning) else ''
analysis = [(r.line, r.column, typ_str(r) + r.name)
for r in analysis]
compare_cb(self, analysis, self.collect_comparison())
def __repr__(self):
return "<%s: %s>" % (self.__class__.__name__, os.path.basename(self._path))
def skip_python_version(line):
# check for python minimal version number
match = re.match(r" *# *python *([<>]=?|==) *(\d+(?:\.\d+)?)$", line)

View File

@@ -1,4 +1,5 @@
from os.path import join, sep as s
from os.path import join, sep as s, dirname
import os
import sys
from textwrap import dedent
@@ -164,6 +165,7 @@ f2 = join(root_dir, 'test', 'example.py')
os_path = 'from os.path import *\n'
# os.path.sep escaped
se = s * 2 if s == '\\' else s
current_dirname = os.path.basename(dirname(dirname(dirname(__file__))))
@pytest.mark.parametrize(
@@ -181,7 +183,7 @@ se = s * 2 if s == '\\' else s
('test%sexample.py' % se, 'r"test%scomp"' % s, 5, ['t' + s]),
('test%sexample.py' % se, 'r"test%scomp"' % s, 11, ['letion' + s]),
('test%sexample.py' % se, '"%s"' % join('test', 'completion', 'basi'), 21, ['c.py']),
('example.py', 'rb"' + join('..', 'jedi', 'tes'), None, ['t' + s]),
('example.py', 'rb"'+ join('..', current_dirname, 'tes'), None, ['t' + s]),
# Absolute paths
(None, '"' + join(root_dir, 'test', 'test_ca'), None, ['che.py"']),

View File

@@ -45,8 +45,9 @@ def test_completion(case, monkeypatch, environment, has_typing):
def test_static_analysis(static_analysis_case, environment):
if static_analysis_case.skip is not None:
pytest.skip(static_analysis_case.skip)
skip_reason = static_analysis_case.get_skip_reason(environment)
if skip_reason is not None:
pytest.skip(skip_reason)
else:
static_analysis_case.run(assert_static_analysis, environment)