Example #1
0
 def visit_tuple_type(self, t: TupleType) -> Type:
     if isinstance(self.s, TupleType) and self.s.length() == t.length():
         items = []  # type: List[Type]
         for i in range(t.length()):
             items.append(self.meet(t.items[i], self.s.items[i]))
         # TODO: What if the fallbacks are different?
         return TupleType(items, t.fallback)
     # meet(Tuple[t1, t2, <...>], Tuple[s, ...]) == Tuple[meet(t1, s), meet(t2, s), <...>].
     elif (isinstance(self.s, Instance) and
           self.s.type.fullname() == 'builtins.tuple' and self.s.args):
         return t.copy_modified(items=[meet_types(it, self.s.args[0]) for it in t.items])
     else:
         return self.default(self.s)
Example #2
0
 def visit_tuple_type(self, t: TupleType) -> Type:
     if isinstance(self.s, TupleType) and self.s.length() == t.length():
         items = []  # type: List[Type]
         for i in range(t.length()):
             items.append(self.meet(t.items[i], self.s.items[i]))
         # TODO: What if the fallbacks are different?
         return TupleType(items, t.fallback)
     # meet(Tuple[t1, t2, <...>], Tuple[s, ...]) == Tuple[meet(t1, s), meet(t2, s), <...>].
     elif (isinstance(self.s, Instance) and
           self.s.type.fullname() == 'builtins.tuple' and self.s.args):
         return t.copy_modified(items=[meet_types(it, self.s.args[0]) for it in t.items])
     else:
         return self.default(self.s)
Example #3
0
 def visit_tuple_type(self, t: TupleType) -> Type:
     if isinstance(self.s, TupleType) and self.s.length() == t.length():
         items = []  # type: List[Type]
         for i in range(t.length()):
             items.append(self.meet(t.items[i], self.s.items[i]))
         # TODO: What if the fallbacks are different?
         return TupleType(items, tuple_fallback(t))
     elif isinstance(self.s, Instance):
         # meet(Tuple[t1, t2, <...>], Tuple[s, ...]) == Tuple[meet(t1, s), meet(t2, s), <...>].
         if self.s.type.fullname() == 'builtins.tuple' and self.s.args:
             return t.copy_modified(items=[meet_types(it, self.s.args[0]) for it in t.items])
         elif is_proper_subtype(t, self.s):
             # A named tuple that inherits from a normal class
             return t
     return self.default(self.s)
Example #4
0
File: meet.py Project: python/mypy
 def visit_tuple_type(self, t: TupleType) -> Type:
     if isinstance(self.s, TupleType) and self.s.length() == t.length():
         items = []  # type: List[Type]
         for i in range(t.length()):
             items.append(self.meet(t.items[i], self.s.items[i]))
         # TODO: What if the fallbacks are different?
         return TupleType(items, tuple_fallback(t))
     elif isinstance(self.s, Instance):
         # meet(Tuple[t1, t2, <...>], Tuple[s, ...]) == Tuple[meet(t1, s), meet(t2, s), <...>].
         if self.s.type.fullname() == 'builtins.tuple' and self.s.args:
             return t.copy_modified(items=[meet_types(it, self.s.args[0]) for it in t.items])
         elif is_proper_subtype(t, self.s):
             # A named tuple that inherits from a normal class
             return t
     return self.default(self.s)
Example #5
0
 def visit_tuple_type(self, t: TupleType) -> Type:
     if isinstance(self.s, TupleType) and self.s.length() == t.length():
         items = []  # type: List[Type]
         for i in range(t.length()):
             items.append(self.meet(t.items[i], self.s.items[i]))
         # TODO: What if the fallbacks are different?
         return TupleType(items, t.fallback)
     # meet(Tuple[t1, t2, <...>], Tuple[s, ...]) == Tuple[meet(t1, s), meet(t2, s), <...>].
     elif (isinstance(self.s, Instance) and
           self.s.type.fullname() == 'builtins.tuple' and self.s.args):
         return t.copy_modified(items=[meet_types(it, self.s.args[0]) for it in t.items])
     elif (isinstance(self.s, Instance) and t.fallback.type == self.s.type):
         # Uh oh, a broken named tuple type (https://github.com/python/mypy/issues/3016).
         # Do something reasonable until that bug is fixed.
         return t
     else:
         return self.default(self.s)
Example #6
0
 def visit_tuple_type(self, t: TupleType) -> Type:
     if isinstance(self.s, TupleType) and self.s.length() == t.length():
         items = []  # type: List[Type]
         for i in range(t.length()):
             items.append(self.meet(t.items[i], self.s.items[i]))
         # TODO: What if the fallbacks are different?
         return TupleType(items, t.fallback)
     # meet(Tuple[t1, t2, <...>], Tuple[s, ...]) == Tuple[meet(t1, s), meet(t2, s), <...>].
     elif (isinstance(self.s, Instance) and
           self.s.type.fullname() == 'builtins.tuple' and self.s.args):
         return t.copy_modified(items=[meet_types(it, self.s.args[0]) for it in t.items])
     elif (isinstance(self.s, Instance) and t.fallback.type == self.s.type):
         # Uh oh, a broken named tuple type (https://github.com/python/mypy/issues/3016).
         # Do something reasonable until that bug is fixed.
         return t
     else:
         return self.default(self.s)
Example #7
0
    def visit_tuple_type(self, t: TupleType) -> Type:
        items = []
        for item in t.items:
            proper_item = get_proper_type(item)
            if isinstance(proper_item, UnpackType):
                unpacked_items = self.expand_unpack(proper_item)
                if unpacked_items is None:
                    # TODO: better error, something like tuple of unknown?
                    return UninhabitedType()
                elif isinstance(unpacked_items, Instance):
                    if len(t.items) == 1:
                        return unpacked_items
                    else:
                        assert False, "Invalid unpack of variable length tuple"
                elif isinstance(unpacked_items, AnyType):
                    return unpacked_items
                else:
                    items.extend(unpacked_items)
            else:
                items.append(proper_item.accept(self))

        return t.copy_modified(items=items)
Example #8
0
 def visit_tuple_type(self, t: TupleType) -> Type:
     return t.copy_modified(items=self.expand_types(t.items))
Example #9
0
 def visit_tuple_type(self, t: TupleType) -> Type:
     return t.copy_modified(items=self.expand_types(t.items))