add multiple values for keyword type error detection

This commit is contained in:
Dave Halter
2014-05-26 18:40:02 +02:00
parent 720907531b
commit f3e986a285
4 changed files with 19 additions and 11 deletions

View File

@@ -15,6 +15,7 @@ CODES = {
'type-error-too-many-arguments': (5, TypeError, None), 'type-error-too-many-arguments': (5, TypeError, None),
'type-error-too-few-arguments': (6, TypeError, None), 'type-error-too-few-arguments': (6, TypeError, None),
'type-error-keyword-argument': (7, TypeError, None), 'type-error-keyword-argument': (7, TypeError, None),
'type-error-multiple-values': (8, TypeError, None),
} }

View File

@@ -1,5 +1,6 @@
import copy import copy
from jedi._compatibility import unicode
from jedi.parser import representation as pr from jedi.parser import representation as pr
from jedi.evaluate import iterable from jedi.evaluate import iterable
from jedi import common from jedi import common
@@ -37,11 +38,19 @@ def get_params(evaluator, func, var_args):
while key: while key:
keys_only = True keys_only = True
try: try:
key_param = param_dict[str(key)] key_param = param_dict[unicode(key)]
except KeyError: except KeyError:
non_matching_keys.append((key, value)) non_matching_keys.append((key, value))
else: else:
keys_used.add(str(key)) k = unicode(key)
if k in keys_used:
print(keys_used, unicode(key), value)
m = ("TypeError: %s() got multiple values for keyword argument '%s'."
% (func.name, k))
analysis.add(evaluator, 'type-error-multiple-values',
var_args, message=m)
else:
keys_used.add(k)
result.append(_gen_param_name_copy(func, var_args, key_param, result.append(_gen_param_name_copy(func, var_args, key_param,
values=[value])) values=[value]))
key, value = next(var_arg_iterator, (None, None)) key, value = next(var_arg_iterator, (None, None))
@@ -93,7 +102,7 @@ def get_params(evaluator, func, var_args):
# Now add to result if it's not one of the previously covered cases. # Now add to result if it's not one of the previously covered cases.
if not has_default_value and (not keys_only or param.stars == 2): if not has_default_value and (not keys_only or param.stars == 2):
keys_used.add(str(key)) keys_used.add(unicode(param.get_name()))
result.append(_gen_param_name_copy(func, var_args, param, result.append(_gen_param_name_copy(func, var_args, param,
keys=keys, values=values, keys=keys, values=values,
array_type=array_type)) array_type=array_type))

View File

@@ -14,7 +14,7 @@ simple(1, 2)
def nested(*args): def nested(*args):
# TODO: shoult not be her but in line 17 # TODO: should not be here, but in line 17
#! 13 type-error-too-few-arguments #! 13 type-error-too-few-arguments
return simple(*args) return simple(*args)
@@ -41,9 +41,7 @@ def two_params(x, y):
two_params(y=2, x=1) two_params(y=2, x=1)
two_params(1, y=2) two_params(1, y=2)
# The next two statements should generate errors. Basically it should generate #! 10 type-error-multiple-values
# something like `TypeError: x() got multiple values for keyword argument 'a'`.
# For now however, any error should be fine.
two_params(1, x=2) two_params(1, x=2)
#! 17 type-error-too-many-arguments #! 17 type-error-too-many-arguments
two_params(1, 2, y=3) two_params(1, 2, y=3)

View File

@@ -26,7 +26,7 @@ def assert_static_analysis(case, actual, desired):
d = set(desired) d = set(desired)
assert actual == desired, """ assert actual == desired, """
Test %r failed. Test %r failed.
too much = %s specified, not raised = %s
missing = %s missing = %s
""" % (case, sorted(d - a), sorted(a - d)) """ % (case, sorted(d - a), sorted(a - d))