You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Prevent crashing when match arms use name of existing callable (#18449)
Fixes#16793. Fixes crash in #13666.
Previously mypy considered that variables in match/case patterns must be
Var's, causing a hard crash when a name of captured pattern clashes with
a name of some existing function. This PR removes such assumption about
Var and allows other nodes.
case (x,) if x == "test": # E: Incompatible types in capture pattern (pattern captures type "Literal['test']", variable has type "Callable[[], Tuple[Literal['test']]]")
2483
+
reveal_type(x) # N: Revealed type is "def () -> Tuple[Literal['test']]"
2484
+
case foo:
2485
+
foo
2486
+
2487
+
[builtins fixtures/dict.pyi]
2488
+
2489
+
[case testMatchRebindsInnerFunctionName]
2490
+
# flags: --warn-unreachable
2491
+
class Some:
2492
+
value: int | str
2493
+
__match_args__ = ("value",)
2494
+
2495
+
def fn1(x: Some | int | str) -> None:
2496
+
match x:
2497
+
case int():
2498
+
def value():
2499
+
return 1
2500
+
reveal_type(value) # N: Revealed type is "def () -> Any"
2501
+
case str():
2502
+
def value():
2503
+
return 1
2504
+
reveal_type(value) # N: Revealed type is "def () -> Any"
2505
+
case Some(value): # E: Incompatible types in capture pattern (pattern captures type "Union[int, str]", variable has type "Callable[[], Any]")
2506
+
pass
2507
+
2508
+
def fn2(x: Some | int | str) -> None:
2509
+
match x:
2510
+
case int():
2511
+
def value() -> str:
2512
+
return ""
2513
+
reveal_type(value) # N: Revealed type is "def () -> builtins.str"
2514
+
case str():
2515
+
def value() -> int: # E: All conditional function variants must have identical signatures \
2516
+
# N: Original: \
2517
+
# N: def value() -> str \
2518
+
# N: Redefinition: \
2519
+
# N: def value() -> int
2520
+
return 1
2521
+
reveal_type(value) # N: Revealed type is "def () -> builtins.str"
2522
+
case Some(value): # E: Incompatible types in capture pattern (pattern captures type "Union[int, str]", variable has type "Callable[[], str]")
0 commit comments