Skip to content

Commit

Permalink
Fix tuple subtyping
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Sep 9, 2023
1 parent 8b73cc2 commit 4031bdd
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ def set_strict_flags() -> None:
parser.error("Can only find occurrences of class members.")
if len(_find_occurrences) != 2:
parser.error("Can only find occurrences of non-nested class members.")
state.find_occurrences = _find_occurrences # type: ignore[assignment]
state.find_occurrences = _find_occurrences

# Set reports.
for flag, val in vars(special_opts).items():
Expand Down
6 changes: 4 additions & 2 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,10 @@ def visit_instance(self, left: Instance) -> bool:
# dynamic base classes correctly, see #5456.
return not isinstance(self.right, NoneType)
right = self.right
if isinstance(right, TupleType) and right.partial_fallback.type.is_enum:
return self._is_subtype(left, mypy.typeops.tuple_fallback(right))
if isinstance(right, TupleType):
return self._is_subtype(left, right.partial_fallback) and self._is_subtype(
left, mypy.typeops.tuple_fallback(right)
)
if isinstance(right, Instance):
if type_state.is_cached_subtype_check(self._subtype_kind, left, right):
return True
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ t1: Tuple[A, A]
t2: tuple

if int():
t1 = t2 # E: Incompatible types in assignment (expression has type "Tuple[Any, ...]", variable has type "Tuple[A, A]")
t1 = t2
if int():
t2 = t1

Expand Down
3 changes: 1 addition & 2 deletions test-data/unit/check-typevar-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ f(empty) # E: Argument 1 to "f" has incompatible type "Tuple[()]"; expected "Tu
f(bad_args) # E: Argument 1 to "f" has incompatible type "Tuple[str, str]"; expected "Tuple[int, str]"

# The reason for error in subtle: actual can be empty, formal cannot.
reveal_type(f(var_len_tuple)) # N: Revealed type is "Tuple[builtins.str, Unpack[builtins.tuple[builtins.int, ...]]]" \
# E: Argument 1 to "f" has incompatible type "Tuple[int, ...]"; expected "Tuple[int, Unpack[Tuple[int, ...]]]"
reveal_type(f(var_len_tuple)) # N: Revealed type is "Tuple[builtins.str, Unpack[builtins.tuple[builtins.int, ...]]]"

g_args: Tuple[str, int]
reveal_type(g(g_args)) # N: Revealed type is "Tuple[builtins.str, builtins.str]"
Expand Down

0 comments on commit 4031bdd

Please sign in to comment.