mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 23:04:48 +08:00
jedi issues now warnings instead of errors for AttributeErrors that happen in instances with __getattr__/__getattribute__ methods
This commit is contained in:
@@ -82,10 +82,25 @@ 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):
|
def add_attribute_error(evaluator, scope, name_part):
|
||||||
|
message = ('AttributeError: %s has no attribute %s.' % (scope, name_part))
|
||||||
|
from jedi.evaluate.representation import Instance
|
||||||
|
# Check for __getattr__/__getattribute__ existance and issue a warning
|
||||||
|
# instead of an error, if that happens.
|
||||||
|
if isinstance(scope, Instance):
|
||||||
|
typ = Warning
|
||||||
|
try:
|
||||||
|
scope.get_subscope_by_name('__getattr__')
|
||||||
|
except KeyError:
|
||||||
|
try:
|
||||||
|
scope.get_subscope_by_name('__getattribute__')
|
||||||
|
except KeyError:
|
||||||
typ = Error
|
typ = Error
|
||||||
payload = scope, jedi_obj # jedi_obj is a name_part.
|
else:
|
||||||
add(evaluator, 'attribute-error', jedi_obj, message, typ, payload)
|
typ = Error
|
||||||
|
|
||||||
|
payload = scope, name_part
|
||||||
|
add(evaluator, 'attribute-error', name_part, 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):
|
||||||
|
|||||||
@@ -50,17 +50,14 @@ class NameFinder(object):
|
|||||||
and not (isinstance(self.name_str, pr.NamePart)
|
and not (isinstance(self.name_str, pr.NamePart)
|
||||||
and isinstance(self.name_str.parent.parent, pr.Param)):
|
and isinstance(self.name_str.parent.parent, pr.Param)):
|
||||||
if not isinstance(self.name_str, (str, unicode)): # TODO Remove?
|
if not isinstance(self.name_str, (str, unicode)): # TODO Remove?
|
||||||
err_type = 'name-error' if search_global else 'attribute-error'
|
if search_global:
|
||||||
if err_type == 'name-error':
|
|
||||||
message = ("NameError: name '%s' is not defined."
|
message = ("NameError: name '%s' is not defined."
|
||||||
% self.name_str)
|
% self.name_str)
|
||||||
|
analysis.add(self._evaluator, 'name-error', self.name_str,
|
||||||
|
message)
|
||||||
else:
|
else:
|
||||||
message = ('AttributeError: %s has no attribute %s.'
|
|
||||||
% (self._last_filter_name_scope, self.name_str))
|
|
||||||
payload = self.name_str
|
|
||||||
analysis.add_attribute_error(self._evaluator,
|
analysis.add_attribute_error(self._evaluator,
|
||||||
self.name_str, message,
|
self.scope, self.name_str)
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user