Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
msg.no_formal_self(name, item, context)
# This is pretty bad, so just return the original signature if
# there is at least one such error.
return functype
else:
selfarg = item.arg_types[0]
if subtypes.is_subtype(dispatched_arg_type, erase_typevars(erase_to_bound(selfarg))):
new_items.append(item)
if not new_items:
# Choose first item for the message (it may be not very helpful for overloads).
msg.incompatible_self_argument(name, dispatched_arg_type, items[0],
is_classmethod, context)
return functype
if len(new_items) == 1:
return new_items[0]
return Overloaded(new_items)
def strip_type(typ: Type) -> ProperType:
"""Make a copy of type without 'debugging info' (function name)."""
typ = get_proper_type(typ)
if isinstance(typ, CallableType):
return typ.copy_modified(name=None)
elif isinstance(typ, Overloaded):
return Overloaded([cast(CallableType, strip_type(item))
for item in typ.items()])
else:
return typ
def _proper_type(
self,
case_functions: List[CallableType],
) -> FunctionLike:
if len(case_functions) == 1:
return case_functions[0]
return Overloaded(case_functions)
def with_name(self, name: str) -> 'Overloaded':
ni = [] # type: List[CallableType]
for it in self._items:
ni.append(it.with_name(name))
return Overloaded(ni)
node: Context) -> None:
"""Check a method override with given signatures.
Arguments:
override: The signature of the overriding method.
original: The signature of the original supertype method.
name: The name of the subtype. This and the next argument are
only used for generating error messages.
supertype: The name of the supertype.
"""
# Use boolean variable to clarify code.
fail = False
if not is_subtype(override, original, ignore_pos_arg_names=True):
fail = True
elif (not isinstance(original, Overloaded) and
isinstance(override, Overloaded) and
name in nodes.reverse_op_methods.keys()):
# Operator method overrides cannot introduce overloading, as
# this could be unsafe with reverse operator methods.
fail = True
if fail:
emitted_msg = False
if (isinstance(override, CallableType) and
isinstance(original, CallableType) and
len(override.arg_types) == len(original.arg_types) and
override.min_args == original.min_args):
# Give more detailed messages for the common case of both
# signatures having the same number of arguments and no
# overloads.
# override might have its own generic function type
builtin_type: Callable[[str], Instance]) -> Type:
if isinstance(t, CallableType):
# TODO: Should we propagate type variable values?
vars = [TypeVarDef(n, i + 1, None, builtin_type('builtins.object'), tv.variance)
for (i, n), tv in zip(enumerate(info.type_vars), info.defn.type_vars)]
arg_types = t.arg_types
arg_kinds = t.arg_kinds
arg_names = t.arg_names
if is_classmethod:
arg_types = arg_types[1:]
arg_kinds = arg_kinds[1:]
arg_names = arg_names[1:]
return t.copy_modified(arg_types=arg_types, arg_kinds=arg_kinds, arg_names=arg_names,
variables=vars + t.variables)
elif isinstance(t, Overloaded):
return Overloaded([cast(CallableType, add_class_tvars(i, info, is_classmethod,
builtin_type))
for i in t.items()])
return t
callee.arg_kinds, callee.arg_names,
lambda i: self.accept(args[i]))
formal_arg_exprs = [[] for _ in range(num_formals)] # type: List[List[Expression]]
for formal, actuals in enumerate(formal_to_actual):
for actual in actuals:
formal_arg_exprs[formal].append(args[actual])
return signature_hook(
MethodSigContext(object_type, formal_arg_exprs, callee, e, self.chk))
else:
assert isinstance(callee, Overloaded)
items = []
for item in callee.items():
adjusted = self.apply_method_signature_hook(e, item, object_type, signature_hook)
assert isinstance(adjusted, CallableType)
items.append(adjusted)
return Overloaded(items)
callee.arg_kinds, callee.arg_names,
lambda i: self.accept(args[i]))
formal_arg_exprs = [[] for _ in range(num_formals)] # type: List[List[Expression]]
for formal, actuals in enumerate(formal_to_actual):
for actual in actuals:
formal_arg_exprs[formal].append(args[actual])
return signature_hook(
MethodSigContext(object_type, formal_arg_exprs, callee, e, self.chk))
else:
assert isinstance(callee, Overloaded)
items = []
for item in callee.items():
adjusted = self.apply_method_signature_hook(e, item, object_type, signature_hook)
assert isinstance(adjusted, CallableType)
items.append(adjusted)
return Overloaded(items)
def visit_callable_type(self, t: CallableType) -> ProperType:
if isinstance(self.s, CallableType) and is_similar_callables(t, self.s):
if is_equivalent(t, self.s):
return combine_similar_callables(t, self.s)
result = join_similar_callables(t, self.s)
# We set the from_type_type flag to suppress error when a collection of
# concrete class objects gets inferred as their common abstract superclass.
if not ((t.is_type_obj() and t.type_object().is_abstract) or
(self.s.is_type_obj() and self.s.type_object().is_abstract)):
result.from_type_type = True
if any(isinstance(tp, (NoneType, UninhabitedType))
for tp in get_proper_types(result.arg_types)):
# We don't want to return unusable Callable, attempt fallback instead.
return join_types(t.fallback, self.s)
return result
elif isinstance(self.s, Overloaded):
# Switch the order of arguments to that we'll get to visit_overloaded.
return join_types(t, self.s)
elif isinstance(self.s, Instance) and self.s.type.is_protocol:
call = unpack_callback_protocol(self.s)
if call:
return join_types(t, call)
return join_types(t.fallback, self.s)
def replace_implicit_first_type(sig: FunctionLike, new: Type) -> FunctionLike:
if isinstance(sig, CallableType):
return replace_leading_arg_type(sig, new)
else:
sig = cast(Overloaded, sig)
return Overloaded([cast(CallableType, replace_implicit_first_type(i, new))
for i in sig.items()])