def visit_tuple_type(self, t: TupleType) -> ProperType: # When given two fixed-length tuples: # * If they have the same length, join their subtypes item-wise: # Tuple[int, bool] + Tuple[bool, bool] becomes Tuple[int, bool] # * If lengths do not match, return a variadic tuple: # Tuple[bool, int] + Tuple[bool] becomes Tuple[int, ...] # # Otherwise, `t` is a fixed-length tuple but `self.s` is NOT: # * Joining with a variadic tuple returns variadic tuple: # Tuple[int, bool] + Tuple[bool, ...] becomes Tuple[int, ...] # * Joining with any Sequence also returns a Sequence: # Tuple[int, bool] + List[bool] becomes Sequence[int] if isinstance(self.s, TupleType) and self.s.length() == t.length(): fallback = join_instances(mypy.typeops.tuple_fallback(self.s), mypy.typeops.tuple_fallback(t)) assert isinstance(fallback, Instance) if self.s.length() == t.length(): items = [] # type: List[Type] for i in range(t.length()): items.append(self.join(t.items[i], self.s.items[i])) return TupleType(items, fallback) else: return fallback else: return join_types(self.s, mypy.typeops.tuple_fallback(t))
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) else: return self.default(self.s)
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.join(t.items[i], self.s.items[i])) # TODO: What if the fallback types are different? return TupleType(items, t.fallback) else: return self.default(self.s)
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.join(t.items[i], self.s.items[i])) fallback = join_instances(self.s.fallback, t.fallback) assert isinstance(fallback, Instance) return TupleType(items, fallback) else: return self.default(self.s)
def visit_tuple_type(self, t: TupleType) -> Type: if (isinstance(self.s, TupleType) and cast(TupleType, self.s).length() == t.length()): items = [] # type: List[Type] for i in range(t.length()): items.append( self.join(t.items[i], (cast(TupleType, self.s)).items[i])) return TupleType(items) else: return self.default(self.s)
def visit_tuple_type(self, t: TupleType) -> Type: if (isinstance(self.s, TupleType) and cast(TupleType, self.s).length() == t.length()): items = [] # type: List[Type] for i in range(t.length()): items.append(self.join(t.items[i], (cast(TupleType, self.s)).items[i])) return TupleType(items) else: return self.default(self.s)
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.join(t.items[i], self.s.items[i])) # join fallback types if they are different from typing import cast return TupleType(items, cast(Instance, join_instances(self.s.fallback, t.fallback))) else: return self.default(self.s)
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)
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)
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)