def _iterate_argument_clinic(evaluator, arguments, parameters): """Uses a list with argument clinic information (see PEP 436).""" iterator = PushBackIterator(arguments.unpack()) for i, (name, optional, allow_kwargs, stars) in enumerate(parameters): if stars == 1: lazy_contexts = [] for key, argument in iterator: if key is not None: iterator.push_back((key, argument)) break lazy_contexts.append(argument) yield ContextSet([iterable.FakeSequence(evaluator, u'tuple', lazy_contexts)]) lazy_contexts continue elif stars == 2: raise NotImplementedError() key, argument = next(iterator, (None, None)) if key is not None: debug.warning('Keyword arguments in argument clinic are currently not supported.') raise ParamIssue if argument is None and not optional: debug.warning('TypeError: %s expected at least %s arguments, got %s', name, len(parameters), i) raise ParamIssue context_set = NO_CONTEXTS if argument is None else argument.infer() if not context_set and not optional: # For the stdlib we always want values. If we don't get them, # that's ok, maybe something is too hard to resolve, however, # we will not proceed with the evaluation of that function. debug.warning('argument_clinic "%s" not resolvable.', name) raise ParamIssue yield context_set
def _create_default_param(execution_context, param): if param.star_count == 1: result_arg = LazyKnownContext( iterable.FakeSequence(execution_context.evaluator, u'tuple', [])) elif param.star_count == 2: result_arg = LazyKnownContext( iterable.FakeDict(execution_context.evaluator, {})) elif param.default is None: result_arg = LazyUnknownContext() else: result_arg = LazyTreeContext(execution_context.parent_context, param.default) return ExecutedParam(execution_context, param, result_arg)
def py__call__(self, item_context_set): context_set = NO_CONTEXTS for args_context in self._args_context_set: lazy_contexts = list(args_context.py__iter__()) if len(lazy_contexts) == 1: # TODO we need to add the contextualized context. context_set |= item_context_set.get_item(lazy_contexts[0].infer(), None) else: context_set |= ContextSet([iterable.FakeSequence( self._wrapped_context.evaluator, 'list', [ LazyKnownContexts(item_context_set.get_item(lazy_context.infer(), None)) for lazy_context in lazy_contexts ], )]) return context_set
def builtins_reversed(evaluator, sequences, obj, arguments): # While we could do without this variable (just by using sequences), we # want static analysis to work well. Therefore we need to generated the # values again. key, lazy_context = next(arguments.unpack()) cn = None if isinstance(lazy_context, LazyTreeContext): # TODO access private cn = ContextualizedNode(lazy_context._context, lazy_context.data) ordered = list(sequences.iterate(cn)) rev = list(reversed(ordered)) # Repack iterator values and then run it the normal way. This is # necessary, because `reversed` is a function and autocompletion # would fail in certain cases like `reversed(x).__iter__` if we # just returned the result directly. seq = iterable.FakeSequence(evaluator, u'list', rev) arguments = ValuesArguments([ContextSet(seq)]) return ContextSet(CompiledInstance(evaluator, evaluator.builtins_module, obj, arguments))
def get_params(execution_context, var_args): result_params = [] param_dict = {} funcdef = execution_context.tree_node parent_context = execution_context.parent_context for param in funcdef.get_params(): param_dict[param.name.value] = param unpacked_va = list(var_args.unpack(funcdef)) var_arg_iterator = PushBackIterator(iter(unpacked_va)) non_matching_keys = defaultdict(lambda: []) keys_used = {} keys_only = False had_multiple_value_error = False for param in funcdef.get_params(): # The value and key can both be null. There, the defaults apply. # args / kwargs will just be empty arrays / dicts, respectively. # Wrong value count is just ignored. If you try to test cases that are # not allowed in Python, Jedi will maybe not show any completions. key, argument = next(var_arg_iterator, (None, None)) while key is not None: keys_only = True try: key_param = param_dict[key] except KeyError: non_matching_keys[key] = argument else: if key in keys_used: had_multiple_value_error = True m = ( "TypeError: %s() got multiple values for keyword argument '%s'." % (funcdef.name, key)) for node in var_args.get_calling_nodes(): analysis.add(parent_context, 'type-error-multiple-values', node, message=m) else: keys_used[key] = ExecutedParam(execution_context, key_param, argument) key, argument = next(var_arg_iterator, (None, None)) try: result_params.append(keys_used[param.name.value]) continue except KeyError: pass if param.star_count == 1: # *args param lazy_context_list = [] if argument is not None: lazy_context_list.append(argument) for key, argument in var_arg_iterator: # Iterate until a key argument is found. if key: var_arg_iterator.push_back((key, argument)) break lazy_context_list.append(argument) seq = iterable.FakeSequence(execution_context.evaluator, u'tuple', lazy_context_list) result_arg = LazyKnownContext(seq) elif param.star_count == 2: # **kwargs param dct = iterable.FakeDict(execution_context.evaluator, dict(non_matching_keys)) result_arg = LazyKnownContext(dct) non_matching_keys = {} else: # normal param if argument is None: # No value: Return an empty container if param.default is None: result_arg = LazyUnknownContext() if not keys_only: for node in var_args.get_calling_nodes(): m = _error_argument_count(funcdef, len(unpacked_va)) analysis.add(parent_context, 'type-error-too-few-arguments', node, message=m) else: result_arg = LazyTreeContext(parent_context, param.default) else: result_arg = argument result_params.append( ExecutedParam(execution_context, param, result_arg)) if not isinstance(result_arg, LazyUnknownContext): keys_used[param.name.value] = result_params[-1] if keys_only: # All arguments should be handed over to the next function. It's not # about the values inside, it's about the names. Jedi needs to now that # there's nothing to find for certain names. for k in set(param_dict) - set(keys_used): param = param_dict[k] if not (non_matching_keys or had_multiple_value_error or param.star_count or param.default): # add a warning only if there's not another one. for node in var_args.get_calling_nodes(): m = _error_argument_count(funcdef, len(unpacked_va)) analysis.add(parent_context, 'type-error-too-few-arguments', node, message=m) for key, lazy_context in non_matching_keys.items(): m = "TypeError: %s() got an unexpected keyword argument '%s'." \ % (funcdef.name, key) _add_argument_issue(parent_context, 'type-error-keyword-argument', lazy_context, message=m) remaining_arguments = list(var_arg_iterator) if remaining_arguments: m = _error_argument_count(funcdef, len(unpacked_va)) # Just report an error for the first param that is not needed (like # cPython). first_key, lazy_context = remaining_arguments[0] if var_args.get_calling_nodes(): # There might not be a valid calling node so check for that first. _add_argument_issue(parent_context, 'type-error-too-many-arguments', lazy_context, message=m) return result_params