mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-09 23:34:45 +08:00
preparation for warnings in static analysis
This commit is contained in:
@@ -82,8 +82,20 @@ def add(evaluator, name, jedi_obj, message=None, typ=Error, payload=None):
|
|||||||
evaluator.analysis.append(instance)
|
evaluator.analysis.append(instance)
|
||||||
|
|
||||||
|
|
||||||
|
def add_attribute_error(evaluator, jedi_obj, message, scope):
|
||||||
|
typ = Error
|
||||||
|
payload = scope, jedi_obj # jedi_obj is a name_part.
|
||||||
|
add(evaluator, 'attribute-error', jedi_obj, message, typ, payload)
|
||||||
|
|
||||||
|
|
||||||
def _check_for_exception_catch(evaluator, jedi_obj, exception, payload=None):
|
def _check_for_exception_catch(evaluator, jedi_obj, exception, payload=None):
|
||||||
"""Returns True if the exception was catched."""
|
"""
|
||||||
|
Checks if a jedi object (e.g. `Statement`) sits inside a try/catch and
|
||||||
|
doesn't count as an error (if equal to `exception`).
|
||||||
|
Also checks `hasattr` for AttributeErrors and uses the `payload` to compare
|
||||||
|
it.
|
||||||
|
Returns True if the exception was catched.
|
||||||
|
"""
|
||||||
def check_match(cls):
|
def check_match(cls):
|
||||||
try:
|
try:
|
||||||
return isinstance(cls, CompiledObject) and issubclass(exception, cls.obj)
|
return isinstance(cls, CompiledObject) and issubclass(exception, cls.obj)
|
||||||
|
|||||||
@@ -57,8 +57,10 @@ class NameFinder(object):
|
|||||||
else:
|
else:
|
||||||
message = ('AttributeError: %s has no attribute %s.'
|
message = ('AttributeError: %s has no attribute %s.'
|
||||||
% (self._last_filter_name_scope, self.name_str))
|
% (self._last_filter_name_scope, self.name_str))
|
||||||
analysis.add(self._evaluator, err_type, self.name_str, message,
|
payload = self.name_str
|
||||||
payload=(self.scope, self.name_str))
|
analysis.add_attribute_error(self._evaluator,
|
||||||
|
self.name_str, message,
|
||||||
|
self.scope)
|
||||||
|
|
||||||
debug.dbg('finder._names_to_types: %s -> %s', names, types)
|
debug.dbg('finder._names_to_types: %s -> %s', names, types)
|
||||||
return self._resolve_descriptors(types)
|
return self._resolve_descriptors(types)
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from . import helpers
|
|||||||
from . import run
|
from . import run
|
||||||
from . import refactor
|
from . import refactor
|
||||||
import jedi
|
import jedi
|
||||||
|
from jedi.evaluate.analysis import Warning
|
||||||
|
|
||||||
|
|
||||||
def pytest_addoption(parser):
|
def pytest_addoption(parser):
|
||||||
@@ -105,7 +106,9 @@ class StaticAnalysisCase(object):
|
|||||||
|
|
||||||
def run(self, compare_cb):
|
def run(self, compare_cb):
|
||||||
analysis = jedi.Script(self._source, path=self._path)._analysis()
|
analysis = jedi.Script(self._source, path=self._path)._analysis()
|
||||||
analysis = [(r.line, r.column, r.name) for r in 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())
|
compare_cb(self, analysis, self.collect_comparison())
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user