示例#1
0
def fix_instance(t: Instance, fail: Callable[[str, Context], None]) -> None:
    """Fix a malformed instance by replacing all type arguments with Any.

    Also emit a suitable error if this is not due to implicit Any's.
    """
    if len(t.args) == 0:
        any_type = AnyType(TypeOfAny.from_omitted_generics,
                           line=t.line, column=t.column)
        t.args = [any_type] * len(t.type.type_vars)
        return
    # Invalid number of type parameters.
    n = len(t.type.type_vars)
    s = '{} type arguments'.format(n)
    if n == 0:
        s = 'no type arguments'
    elif n == 1:
        s = '1 type argument'
    act = str(len(t.args))
    if act == '0':
        act = 'none'
    fail('"{}" expects {}, but {} given'.format(
        t.type.name(), s, act), t)
    # Construct the correct number of type arguments, as
    # otherwise the type checker may crash as it expects
    # things to be right.
    t.args = [AnyType(TypeOfAny.from_error) for _ in t.type.type_vars]
    t.invalid = True
示例#2
0
 def visit_instance(self, t: Instance) -> None:
     info = t.type
     if info.replaced or info.tuple_type:
         self.indicator['synthetic'] = True
     # Check type argument count.
     if len(t.args) != len(info.type_vars):
         if len(t.args) == 0:
             from_builtins = t.type.fullname(
             ) in nongen_builtins and not t.from_generic_builtin
             if (self.options.disallow_any_generics
                     and not self.is_typeshed_stub and from_builtins):
                 alternative = nongen_builtins[t.type.fullname()]
                 self.fail(
                     messages.IMPLICIT_GENERIC_ANY_BUILTIN.format(
                         alternative), t)
             # Insert implicit 'Any' type arguments.
             if from_builtins:
                 # this 'Any' was already reported elsewhere
                 any_type = AnyType(TypeOfAny.special_form,
                                    line=t.line,
                                    column=t.column)
             else:
                 any_type = AnyType(TypeOfAny.from_omitted_generics,
                                    line=t.line,
                                    column=t.column)
             t.args = [any_type] * len(info.type_vars)
             return
         # Invalid number of type parameters.
         n = len(info.type_vars)
         s = '{} type arguments'.format(n)
         if n == 0:
             s = 'no type arguments'
         elif n == 1:
             s = '1 type argument'
         act = str(len(t.args))
         if act == '0':
             act = 'none'
         self.fail(
             '"{}" expects {}, but {} given'.format(info.name(), s, act), t)
         # Construct the correct number of type arguments, as
         # otherwise the type checker may crash as it expects
         # things to be right.
         t.args = [AnyType(TypeOfAny.from_error) for _ in info.type_vars]
         t.invalid = True
     elif info.defn.type_vars:
         # Check type argument values. This is postponed to the end of semantic analysis
         # since we need full MROs and resolved forward references.
         for tvar in info.defn.type_vars:
             if (tvar.values or not isinstance(tvar.upper_bound, Instance)
                     or
                     tvar.upper_bound.type.fullname() != 'builtins.object'):
                 # Some restrictions on type variable. These can only be checked later
                 # after we have final MROs and forward references have been resolved.
                 self.indicator['typevar'] = True
     for arg in t.args:
         arg.accept(self)
     if info.is_newtype:
         for base in info.bases:
             base.accept(self)
示例#3
0
 def visit_instance(self, t: Instance) -> None:
     info = t.type
     # Check type argument count.
     if len(t.args) != len(info.type_vars):
         if len(t.args) == 0:
             from_builtins = t.type.fullname() in nongen_builtins and not t.from_generic_builtin
             if ('generics' in self.options.disallow_any and
                     not self.is_typeshed_stub and
                     from_builtins):
                 alternative = nongen_builtins[t.type.fullname()]
                 self.fail(messages.IMPLICIT_GENERIC_ANY_BUILTIN.format(alternative), t)
             # Insert implicit 'Any' type arguments.
             any_type = AnyType(from_omitted_generics=not from_builtins, line=t.line,
                                column=t.line)
             t.args = [any_type] * len(info.type_vars)
             return
         # Invalid number of type parameters.
         n = len(info.type_vars)
         s = '{} type arguments'.format(n)
         if n == 0:
             s = 'no type arguments'
         elif n == 1:
             s = '1 type argument'
         act = str(len(t.args))
         if act == '0':
             act = 'none'
         self.fail('"{}" expects {}, but {} given'.format(
             info.name(), s, act), t)
         # Construct the correct number of type arguments, as
         # otherwise the type checker may crash as it expects
         # things to be right.
         t.args = [AnyType() for _ in info.type_vars]
         t.invalid = True
     elif info.defn.type_vars:
         # Check type argument values.
         for (i, arg), tvar in zip(enumerate(t.args), info.defn.type_vars):
             if tvar.values:
                 if isinstance(arg, TypeVarType):
                     arg_values = arg.values
                     if not arg_values:
                         self.fail('Type variable "{}" not valid as type '
                                   'argument value for "{}"'.format(
                                       arg.name, info.name()), t)
                         continue
                 else:
                     arg_values = [arg]
                 self.check_type_var_values(info, arg_values,
                                            tvar.values, i + 1, t)
             if not is_subtype(arg, tvar.upper_bound):
                 self.fail('Type argument "{}" of "{}" must be '
                           'a subtype of "{}"'.format(
                               arg, info.name(), tvar.upper_bound), t)
     for arg in t.args:
         arg.accept(self)
     if info.is_newtype:
         for base in info.bases:
             base.accept(self)
示例#4
0
文件: typeanal.py 项目: sixolet/mypy
 def visit_instance(self, t: Instance) -> None:
     info = t.type
     if info.replaced or info.tuple_type:
         self.indicator['synthetic'] = True
     # Check type argument count.
     if len(t.args) != len(info.type_vars):
         if len(t.args) == 0:
             from_builtins = t.type.fullname() in nongen_builtins and not t.from_generic_builtin
             if (self.options.disallow_any_generics and
                     not self.is_typeshed_stub and
                     from_builtins):
                 alternative = nongen_builtins[t.type.fullname()]
                 self.fail(messages.IMPLICIT_GENERIC_ANY_BUILTIN.format(alternative), t)
             # Insert implicit 'Any' type arguments.
             if from_builtins:
                 # this 'Any' was already reported elsewhere
                 any_type = AnyType(TypeOfAny.special_form,
                                    line=t.line, column=t.column)
             else:
                 any_type = AnyType(TypeOfAny.from_omitted_generics,
                                    line=t.line, column=t.column)
             t.args = [any_type] * len(info.type_vars)
             return
         # Invalid number of type parameters.
         n = len(info.type_vars)
         s = '{} type arguments'.format(n)
         if n == 0:
             s = 'no type arguments'
         elif n == 1:
             s = '1 type argument'
         act = str(len(t.args))
         if act == '0':
             act = 'none'
         self.fail('"{}" expects {}, but {} given'.format(
             info.name(), s, act), t)
         # Construct the correct number of type arguments, as
         # otherwise the type checker may crash as it expects
         # things to be right.
         t.args = [AnyType(TypeOfAny.from_error) for _ in info.type_vars]
         t.invalid = True
     elif info.defn.type_vars:
         # Check type argument values. This is postponed to the end of semantic analysis
         # since we need full MROs and resolved forward references.
         for tvar in info.defn.type_vars:
             if (tvar.values
                     or not isinstance(tvar.upper_bound, Instance)
                     or tvar.upper_bound.type.fullname() != 'builtins.object'):
                 # Some restrictions on type variable. These can only be checked later
                 # after we have final MROs and forward references have been resolved.
                 self.indicator['typevar'] = True
     for arg in t.args:
         arg.accept(self)
     if info.is_newtype:
         for base in info.bases:
             base.accept(self)
示例#5
0
 def visit_instance(self, t: Instance) -> None:
     info = t.type
     # Check type argument count.
     if len(t.args) != len(info.type_vars):
         if len(t.args) == 0:
             # Insert implicit 'Any' type arguments.
             t.args = [AnyType()] * len(info.type_vars)
             return
         # Invalid number of type parameters.
         n = len(info.type_vars)
         s = '{} type arguments'.format(n)
         if n == 0:
             s = 'no type arguments'
         elif n == 1:
             s = '1 type argument'
         act = str(len(t.args))
         if act == '0':
             act = 'none'
         self.fail(
             '"{}" expects {}, but {} given'.format(info.name(), s, act), t)
         # Construct the correct number of type arguments, as
         # otherwise the type checker may crash as it expects
         # things to be right.
         t.args = [AnyType() for _ in info.type_vars]
         t.invalid = True
     elif info.defn.type_vars:
         # Check type argument values.
         for (i, arg), tvar in zip(enumerate(t.args), info.defn.type_vars):
             if tvar.values:
                 if isinstance(arg, TypeVarType):
                     arg_values = arg.values
                     if not arg_values:
                         self.fail(
                             'Type variable "{}" not valid as type '
                             'argument value for "{}"'.format(
                                 arg.name, info.name()), t)
                         continue
                 else:
                     arg_values = [arg]
                 self.check_type_var_values(info, arg_values, tvar.values,
                                            i + 1, t)
             if not is_subtype(arg, tvar.upper_bound):
                 self.fail(
                     'Type argument "{}" of "{}" must be '
                     'a subtype of "{}"'.format(arg, info.name(),
                                                tvar.upper_bound), t)
     for arg in t.args:
         arg.accept(self)
     if info.is_newtype:
         for base in info.bases:
             base.accept(self)
示例#6
0
 def visit_instance(self, t: Instance) -> None:
     info = t.type
     # Check type argument count.
     if len(t.args) != len(info.type_vars):
         if len(t.args) == 0:
             # Insert implicit 'Any' type arguments.
             t.args = [AnyType()] * len(info.type_vars)
             return
         # Invalid number of type parameters.
         n = len(info.type_vars)
         s = '{} type arguments'.format(n)
         if n == 0:
             s = 'no type arguments'
         elif n == 1:
             s = '1 type argument'
         act = str(len(t.args))
         if act == '0':
             act = 'none'
         self.fail('"{}" expects {}, but {} given'.format(
             info.name(), s, act), t)
         # Construct the correct number of type arguments, as
         # otherwise the type checker may crash as it expects
         # things to be right.
         t.args = [AnyType() for _ in info.type_vars]
         t.invalid = True
     elif info.defn.type_vars:
         # Check type argument values.
         for (i, arg), TypeVar in zip(enumerate(t.args), info.defn.type_vars):
             if TypeVar.values:
                 if isinstance(arg, TypeVarType):
                     arg_values = arg.values
                     if not arg_values:
                         self.fail('Type variable "{}" not valid as type '
                                   'argument value for "{}"'.format(
                                       arg.name, info.name()), t)
                         continue
                 else:
                     arg_values = [arg]
                 self.check_type_var_values(info, arg_values,
                                            TypeVar.values, i + 1, t)
             if not satisfies_upper_bound(arg, TypeVar.upper_bound):
                 self.fail('Type argument "{}" of "{}" must be '
                           'a subtype of "{}"'.format(
                               arg, info.name(), TypeVar.upper_bound), t)
     for arg in t.args:
         arg.accept(self)
示例#7
0
 def visit_instance(self, t: Instance) -> None:
     info = t.type
     if info.replaced or info.tuple_type:
         self.indicator['synthetic'] = True
     # Check type argument count.
     if len(t.args) != len(info.type_vars):
         if len(t.args) == 0:
             from_builtins = t.type.fullname(
             ) in nongen_builtins and not t.from_generic_builtin
             if (self.options.disallow_any_generics
                     and not self.is_typeshed_stub and from_builtins):
                 alternative = nongen_builtins[t.type.fullname()]
                 self.fail(
                     messages.IMPLICIT_GENERIC_ANY_BUILTIN.format(
                         alternative), t)
             # Insert implicit 'Any' type arguments.
             if from_builtins:
                 # this 'Any' was already reported elsewhere
                 any_type = AnyType(TypeOfAny.special_form,
                                    line=t.line,
                                    column=t.column)
             else:
                 any_type = AnyType(TypeOfAny.from_omitted_generics,
                                    line=t.line,
                                    column=t.column)
             t.args = [any_type] * len(info.type_vars)
             return
         # Invalid number of type parameters.
         n = len(info.type_vars)
         s = '{} type arguments'.format(n)
         if n == 0:
             s = 'no type arguments'
         elif n == 1:
             s = '1 type argument'
         act = str(len(t.args))
         if act == '0':
             act = 'none'
         self.fail(
             '"{}" expects {}, but {} given'.format(info.name(), s, act), t)
         # Construct the correct number of type arguments, as
         # otherwise the type checker may crash as it expects
         # things to be right.
         t.args = [AnyType(TypeOfAny.from_error) for _ in info.type_vars]
         t.invalid = True
     elif info.defn.type_vars:
         # Check type argument values.
         # TODO: Calling is_subtype and is_same_types in semantic analysis is a bad idea
         for (i, arg), tvar in zip(enumerate(t.args), info.defn.type_vars):
             if tvar.values:
                 if isinstance(arg, TypeVarType):
                     arg_values = arg.values
                     if not arg_values:
                         self.fail(
                             'Type variable "{}" not valid as type '
                             'argument value for "{}"'.format(
                                 arg.name, info.name()), t)
                         continue
                 else:
                     arg_values = [arg]
                 self.check_type_var_values(info, arg_values, tvar.name,
                                            tvar.values, i + 1, t)
             # TODO: These hacks will be not necessary when this will be moved to later stage.
             arg = self.resolve_type(arg)
             bound = self.resolve_type(tvar.upper_bound)
             if not is_subtype(arg, bound):
                 self.fail(
                     'Type argument "{}" of "{}" must be '
                     'a subtype of "{}"'.format(arg, info.name(), bound), t)
     for arg in t.args:
         arg.accept(self)
     if info.is_newtype:
         for base in info.bases:
             base.accept(self)
示例#8
0
 def visit_instance(self, t: Instance) -> ProperType:
     dup = Instance(t.type, t.args, last_known_value=t.last_known_value)
     dup.invalid = t.invalid
     return self.copy_common(t, dup)
示例#9
0
 def visit_instance(self, t: Instance) -> None:
     info = t.type
     if info.replaced or info.tuple_type:
         self.indicator['synthetic'] = True
     # Check type argument count.
     if len(t.args) != len(info.type_vars):
         if len(t.args) == 0:
             from_builtins = t.type.fullname() in nongen_builtins and not t.from_generic_builtin
             if (self.options.disallow_any_generics and
                     not self.is_typeshed_stub and
                     from_builtins):
                 alternative = nongen_builtins[t.type.fullname()]
                 self.fail(messages.IMPLICIT_GENERIC_ANY_BUILTIN.format(alternative), t)
             # Insert implicit 'Any' type arguments.
             if from_builtins:
                 # this 'Any' was already reported elsewhere
                 any_type = AnyType(TypeOfAny.special_form,
                                    line=t.line, column=t.column)
             else:
                 any_type = AnyType(TypeOfAny.from_omitted_generics,
                                    line=t.line, column=t.column)
             t.args = [any_type] * len(info.type_vars)
             return
         # Invalid number of type parameters.
         n = len(info.type_vars)
         s = '{} type arguments'.format(n)
         if n == 0:
             s = 'no type arguments'
         elif n == 1:
             s = '1 type argument'
         act = str(len(t.args))
         if act == '0':
             act = 'none'
         self.fail('"{}" expects {}, but {} given'.format(
             info.name(), s, act), t)
         # Construct the correct number of type arguments, as
         # otherwise the type checker may crash as it expects
         # things to be right.
         t.args = [AnyType(TypeOfAny.from_error) for _ in info.type_vars]
         t.invalid = True
     elif info.defn.type_vars:
         # Check type argument values.
         # TODO: Calling is_subtype and is_same_types in semantic analysis is a bad idea
         for (i, arg), tvar in zip(enumerate(t.args), info.defn.type_vars):
             if tvar.values:
                 if isinstance(arg, TypeVarType):
                     arg_values = arg.values
                     if not arg_values:
                         self.fail('Type variable "{}" not valid as type '
                                   'argument value for "{}"'.format(
                                       arg.name, info.name()), t)
                         continue
                 else:
                     arg_values = [arg]
                 self.check_type_var_values(info, arg_values, tvar.name, tvar.values, i + 1, t)
             # TODO: These hacks will be not necessary when this will be moved to later stage.
             arg = self.resolve_type(arg)
             bound = self.resolve_type(tvar.upper_bound)
             if not is_subtype(arg, bound):
                 self.fail('Type argument "{}" of "{}" must be '
                           'a subtype of "{}"'.format(
                               arg, info.name(), bound), t)
     for arg in t.args:
         arg.accept(self)
     if info.is_newtype:
         for base in info.bases:
             base.accept(self)