Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# positional arguments without defaults
for a, annotation in args[:num_no_defaults]:
new_args.append(Argument(a, annotation, None, ARG_POS))
# positional arguments with defaults
for (a, annotation), d in zip(args[num_no_defaults:], defaults):
new_args.append(Argument(a, annotation, d, ARG_OPT))
# *arg
if n.vararg is not None:
new_args.append(Argument(Var(n.vararg), get_type(len(args)), None, ARG_STAR))
# **kwarg
if n.kwarg is not None:
typ = get_type(len(args) + (0 if n.vararg is None else 1))
new_args.append(Argument(Var(n.kwarg), typ, None, ARG_STAR2))
return new_args
func.info = info
func.is_class = is_classmethod
func.type = set_callable_name(signature, func)
func._fullname = info.fullname() + '.' + funcname
if is_classmethod:
v = Var(funcname, func.type)
v.is_classmethod = True
v.info = info
v._fullname = func._fullname
dec = Decorator(func, [NameExpr('classmethod')], v)
info.names[funcname] = SymbolTableNode(MDEF, dec)
else:
info.names[funcname] = SymbolTableNode(MDEF, func)
add_method('_replace', ret=selftype,
args=[Argument(var, var.type, EllipsisExpr(), ARG_NAMED_OPT) for var in vars])
def make_init_arg(var: Var) -> Argument:
default = default_items.get(var.name(), None)
kind = ARG_POS if default is None else ARG_OPT
return Argument(var, var.type, default, kind)
add_method('__new__', ret=selftype, name=info.name(),
args=[make_init_arg(var) for var in vars],
is_new=True)
add_method('_asdict', args=[], ret=ordereddictype)
special_form_any = AnyType(TypeOfAny.special_form)
add_method('_make', ret=selftype, is_classmethod=True,
args=[Argument(Var('iterable', iterable_type), iterable_type, None, ARG_POS),
Argument(Var('new'), special_form_any, EllipsisExpr(), ARG_NAMED_OPT),
Argument(Var('len'), special_form_any, EllipsisExpr(), ARG_NAMED_OPT)])
return info
def add_get_set_attr_fallback_to_any(ctx: ClassDefContext):
any = AnyType(TypeOfAny.special_form)
name_arg = Argument(variable=Var('name', any),
type_annotation=any, initializer=None, kind=ARG_POS)
add_method(ctx, '__getattr__', [name_arg], any)
value_arg = Argument(variable=Var('value', any),
type_annotation=any, initializer=None, kind=ARG_POS)
add_method(ctx, '__setattr__', [name_arg, value_arg], any)
initializer = None # type: Node
if self.current_str() == '=':
self.expect('=')
initializer = self.parse_expression(precedence[','])
if require_named:
kind = nodes.ARG_NAMED
else:
kind = nodes.ARG_OPT
else:
if require_named:
kind = nodes.ARG_NAMED
else:
kind = nodes.ARG_POS
return Argument(variable, type, initializer, kind), require_named
return
for arg_name, arg_type, original_argument in zip(method_type.arg_names[1:],
method_type.arg_types[1:],
method_node.arguments[1:]):
bound_arg_type = semanal_api.anal_type(arg_type, allow_placeholder=True)
assert bound_arg_type is not None
if isinstance(bound_arg_type, PlaceholderNode):
return
var = Var(name=original_argument.variable.name,
type=arg_type)
var.line = original_argument.variable.line
var.column = original_argument.variable.column
argument = Argument(variable=var,
type_annotation=bound_arg_type,
initializer=original_argument.initializer,
kind=original_argument.kind)
argument.set_line(original_argument)
arguments.append(argument)
add_method(ctx,
new_method_name,
args=arguments,
return_type=bound_return_type,
self_type=self_type)
def add_translate_method(self):
ctx = self._ctx
self_tvar_def = self._get_tvar_def(SELF_TVAR_NAME, ctx)
r_type = AnyType(TypeOfAny.explicit)
arg_type = TypeType(r_type)
arg = Argument(Var("target", arg_type), arg_type, None, ARG_POS)
add_method(
ctx,
"translate",
args=[arg],
return_type=r_type,
self_type=TypeVarType(self_tvar_def),
tvar_def=self_tvar_def,
)
return_type=return_type)
# get_next_by, get_previous_by for Date, DateTime
for field in self.django_context.get_model_fields(model_cls):
if isinstance(field, (DateField, DateTimeField)) and not field.null:
return_type = Instance(self.model_classdef.info, [])
common.add_method(self.ctx,
name='get_next_by_{}'.format(field.attname),
args=[Argument(Var('kwargs', AnyType(TypeOfAny.explicit)),
AnyType(TypeOfAny.explicit),
initializer=None,
kind=ARG_STAR2)],
return_type=return_type)
common.add_method(self.ctx,
name='get_previous_by_{}'.format(field.attname),
args=[Argument(Var('kwargs', AnyType(TypeOfAny.explicit)),
AnyType(TypeOfAny.explicit),
initializer=None,
kind=ARG_STAR2)],
return_type=return_type)
if i < len(type_comments) and type_comments[i] is not None:
return converter.visit_raw_str(type_comments[i])
return None
args = [(convert_arg(arg), get_type(i)) for i, arg in enumerate(n.args)]
defaults = self.visit_list(n.defaults)
new_args = [] # type: List[Argument]
num_no_defaults = len(args) - len(defaults)
# positional arguments without defaults
for a, annotation in args[:num_no_defaults]:
new_args.append(Argument(a, annotation, None, ARG_POS))
# positional arguments with defaults
for (a, annotation), d in zip(args[num_no_defaults:], defaults):
new_args.append(Argument(a, annotation, d, ARG_OPT))
# *arg
if n.vararg is not None:
new_args.append(Argument(Var(n.vararg), get_type(len(args)), None, ARG_STAR))
# **kwarg
if n.kwarg is not None:
typ = get_type(len(args) + (0 if n.vararg is None else 1))
new_args.append(Argument(Var(n.kwarg), typ, None, ARG_STAR2))
return new_args
converter = TypeConverter(self.errors, line=line,
assume_str_is_unicode=self.unicode_literals)
decompose_stmts = [] # type: List[Statement]
n_args = n.args
args = [(self.convert_arg(i, arg, line, decompose_stmts),
self.get_type(i, type_comments, converter))
for i, arg in enumerate(n_args)]
defaults = self.translate_expr_list(n.defaults)
names = [name for arg in n_args for name in self.extract_names(arg)] # type: List[str]
new_args = [] # type: List[Argument]
num_no_defaults = len(args) - len(defaults)
# positional arguments without defaults
for a, annotation in args[:num_no_defaults]:
new_args.append(Argument(a, annotation, None, ARG_POS))
# positional arguments with defaults
for (a, annotation), d in zip(args[num_no_defaults:], defaults):
new_args.append(Argument(a, annotation, d, ARG_OPT))
# *arg
if n.vararg is not None:
new_args.append(Argument(Var(n.vararg),
self.get_type(len(args), type_comments, converter),
None,
ARG_STAR))
names.append(n.vararg)
# **kwarg
if n.kwarg is not None:
typ = self.get_type(len(args) + (0 if n.vararg is None else 1),