diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index e2074d347428..a6f3ee30449d 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -1,6 +1,7 @@ -- Capture Pattern -- [case testMatchCapturePatternType] +# flags: --strict-equality --warn-unreachable class A: ... m: A @@ -11,7 +12,7 @@ match m: -- Literal Pattern -- [case testMatchLiteralPatternNarrows] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable m: object match m: @@ -23,7 +24,7 @@ match m: reveal_type(other) # N: Revealed type is "builtins.object" [case testMatchLiteralPatternNarrows2] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable from typing import Any m: Any @@ -111,6 +112,7 @@ b: str -- Sequence Pattern -- [case testMatchSequencePatternCaptures] +# flags: --strict-equality --warn-unreachable from typing import List m: List[int] @@ -120,6 +122,7 @@ match m: [builtins fixtures/list.pyi] [case testMatchSequencePatternCapturesStarred] +# flags: --strict-equality --warn-unreachable from typing import Sequence m: Sequence[int] @@ -130,6 +133,7 @@ match m: [builtins fixtures/list.pyi] [case testMatchSequencePatternNarrowsInner] +# flags: --strict-equality --warn-unreachable from typing import Sequence m: Sequence[object] @@ -138,6 +142,7 @@ match m: reveal_type(m) # N: Revealed type is "typing.Sequence[builtins.int]" [case testMatchSequencePatternNarrowsOuter] +# flags: --strict-equality --warn-unreachable from typing import Sequence m: object @@ -146,6 +151,7 @@ match m: reveal_type(m) # N: Revealed type is "typing.Sequence[builtins.int]" [case testMatchSequencePatternAlreadyNarrowerInner] +# flags: --strict-equality --warn-unreachable from typing import Sequence m: Sequence[bool] @@ -154,6 +160,7 @@ match m: reveal_type(m) # N: Revealed type is "typing.Sequence[builtins.bool]" [case testMatchSequencePatternAlreadyNarrowerOuter] +# flags: --strict-equality --warn-unreachable from typing import Sequence m: Sequence[object] @@ -162,6 +169,7 @@ match m: reveal_type(m) # N: Revealed type is "typing.Sequence[builtins.int]" [case testMatchSequencePatternAlreadyNarrowerBoth] +# flags: --strict-equality --warn-unreachable from typing import Sequence m: Sequence[bool] @@ -170,6 +178,7 @@ match m: reveal_type(m) # N: Revealed type is "typing.Sequence[builtins.bool]" [case testMatchNestedSequencePatternNarrowsInner] +# flags: --strict-equality --warn-unreachable from typing import Sequence m: Sequence[Sequence[object]] @@ -178,6 +187,7 @@ match m: reveal_type(m) # N: Revealed type is "typing.Sequence[typing.Sequence[builtins.int]]" [case testMatchNestedSequencePatternNarrowsOuter] +# flags: --strict-equality --warn-unreachable from typing import Sequence m: object @@ -186,6 +196,7 @@ match m: reveal_type(m) # N: Revealed type is "typing.Sequence[typing.Sequence[builtins.int]]" [case testMatchSequencePatternDoesntNarrowInvariant] +# flags: --strict-equality --warn-unreachable from typing import List m: List[object] @@ -195,6 +206,7 @@ match m: [builtins fixtures/list.pyi] [case testMatchSequencePatternMatches] +# flags: --strict-equality --warn-unreachable import array, collections from typing import Sequence, Iterable @@ -245,19 +257,20 @@ match m8: match m9: case [i]: - reveal_type(i) + reveal_type(i) # E: Statement is unreachable match m10: case [j]: - reveal_type(j) + reveal_type(j) # E: Statement is unreachable match m11: case [k]: - reveal_type(k) + reveal_type(k) # E: Statement is unreachable [builtins fixtures/primitives.pyi] [typing fixtures/typing-full.pyi] [case testMatchSequencePatternCapturesTuple] +# flags: --strict-equality --warn-unreachable from typing import Tuple m: Tuple[int, str, bool] @@ -270,27 +283,30 @@ match m: [builtins fixtures/list.pyi] [case testMatchSequencePatternTupleTooLong] +# flags: --strict-equality --warn-unreachable from typing import Tuple m: Tuple[int, str] match m: case [a, b, c]: - reveal_type(a) + reveal_type(a) # E: Statement is unreachable reveal_type(b) reveal_type(c) [builtins fixtures/list.pyi] [case testMatchSequencePatternTupleTooShort] +# flags: --strict-equality --warn-unreachable from typing import Tuple m: Tuple[int, str, bool] match m: case [a, b]: - reveal_type(a) + reveal_type(a) # E: Statement is unreachable reveal_type(b) [builtins fixtures/list.pyi] [case testMatchSequencePatternTupleNarrows] +# flags: --strict-equality --warn-unreachable from typing import Tuple m: Tuple[object, object] @@ -300,6 +316,7 @@ match m: [builtins fixtures/list.pyi] [case testMatchSequencePatternTupleStarred] +# flags: --strict-equality --warn-unreachable from typing import Tuple m: Tuple[int, str, bool] @@ -312,6 +329,7 @@ match m: [builtins fixtures/list.pyi] [case testMatchSequencePatternTupleStarredUnion] +# flags: --strict-equality --warn-unreachable from typing import Tuple m: Tuple[int, str, float, bool] @@ -324,18 +342,20 @@ match m: [builtins fixtures/list.pyi] [case testMatchSequencePatternTupleStarredTooShort] +# flags: --strict-equality --warn-unreachable from typing import Tuple m: Tuple[int] reveal_type(m) # N: Revealed type is "tuple[builtins.int]" match m: case [a, *b, c]: - reveal_type(a) + reveal_type(a) # E: Statement is unreachable reveal_type(b) reveal_type(c) [builtins fixtures/list.pyi] [case testMatchNonMatchingSequencePattern] +# flags: --strict-equality --warn-unreachable from typing import List x: List[int] @@ -367,6 +387,7 @@ match [SubClass("a"), SubClass("b")]: # now propagate the type information up to the parent. [case testMatchNarrowingUnionTypedDictViaIndex] +# flags: --strict-equality --warn-unreachable from typing import Literal, TypedDict class A(TypedDict): @@ -388,6 +409,7 @@ match d["tag"]: [typing fixtures/typing-typeddict.pyi] [case testMatchNarrowingUnionClassViaAttribute] +# flags: --strict-equality --warn-unreachable from typing import Literal class A: @@ -408,6 +430,7 @@ match d.tag: reveal_type(d.num) # N: Revealed type is "builtins.int" [case testMatchSequenceUnion] +# flags: --strict-equality --warn-unreachable from typing import List, Union m: Union[List[List[str]], str] @@ -417,6 +440,7 @@ match m: [builtins fixtures/list.pyi] [case testMatchSequencePatternNarrowSubjectItems] +# flags: --strict-equality --warn-unreachable m: int n: str o: bool @@ -437,6 +461,7 @@ reveal_type(o) # N: Revealed type is "builtins.bool" [builtins fixtures/tuple.pyi] [case testMatchSequencePatternNarrowSubjectItemsRecursive] +# flags: --strict-equality --warn-unreachable m: int n: int o: int @@ -455,36 +480,39 @@ match m, (n, o), (p, (q, r)): [builtins fixtures/tuple.pyi] [case testMatchSequencePatternSequencesLengthMismatchNoNarrowing] +# flags: --strict-equality --warn-unreachable m: int n: str o: bool match m, n, o: case [3, "foo"]: - pass + pass # E: Statement is unreachable case [3, "foo", True, True]: - pass + pass # E: Statement is unreachable [builtins fixtures/tuple.pyi] [case testMatchSequencePatternSequencesLengthMismatchNoNarrowingRecursive] +# flags: --strict-equality --warn-unreachable m: int n: int o: int match m, (n, o): case [0]: - pass + pass # E: Statement is unreachable case [0, 1, [2]]: - pass + pass # E: Statement is unreachable case [0, [1]]: - pass + pass # E: Statement is unreachable case [0, [1, 2, 3]]: - pass + pass # E: Statement is unreachable [builtins fixtures/tuple.pyi] -- Mapping Pattern -- [case testMatchMappingPatternCaptures] +# flags: --strict-equality --warn-unreachable from typing import Dict import b m: Dict[str, int] @@ -515,6 +543,7 @@ b: int [builtins fixtures/dict.pyi] [case testMatchMappingPatternCapturesTypedDict] +# flags: --strict-equality --warn-unreachable from typing import TypedDict class A(TypedDict): @@ -536,6 +565,7 @@ match m: [typing fixtures/typing-typeddict.pyi] [case testMatchMappingPatternCapturesTypedDictWithLiteral] +# flags: --strict-equality --warn-unreachable from typing import TypedDict import b @@ -563,6 +593,7 @@ o: Final[str] = "o" [typing fixtures/typing-typeddict.pyi] [case testMatchMappingPatternCapturesTypedDictWithNonLiteral] +# flags: --strict-equality --warn-unreachable from typing import TypedDict import b @@ -602,6 +633,7 @@ b: int [typing fixtures/typing-typeddict.pyi] [case testMatchMappingPatternCaptureRest] +# flags: --strict-equality --warn-unreachable m: object match m: @@ -610,6 +642,7 @@ match m: [builtins fixtures/dict.pyi] [case testMatchMappingPatternCaptureRestFromMapping] +# flags: --strict-equality --warn-unreachable from typing import Mapping m: Mapping[str, int] @@ -620,6 +653,7 @@ match m: [builtins fixtures/dict.pyi] [case testMatchMappingPatternNarrowing] +# flags: --strict-equality --warn-unreachable from typing import Mapping, Sequence def f1(x: dict[str, str] | list[str] | str) -> None: @@ -660,6 +694,7 @@ def f2_rest(x: Mapping[str, str] | Sequence[str] | str) -> None: [builtins fixtures/dict.pyi] [case testMatchMappingPatternNarrowingAny] +# flags: --strict-equality --warn-unreachable from typing import Any def f1(x: Any) -> None: @@ -676,6 +711,7 @@ def f2(x: Any) -> None: -- Class Pattern -- [case testMatchClassPatternCapturePositional] +# flags: --strict-equality --warn-unreachable from typing import Final class A: @@ -692,6 +728,7 @@ match m: [builtins fixtures/tuple.pyi] [case testMatchClassPatternMemberClassCapturePositional] +# flags: --strict-equality --warn-unreachable import b m: b.A @@ -710,6 +747,7 @@ class A: [builtins fixtures/tuple.pyi] [case testMatchClassPatternCaptureKeyword] +# flags: --strict-equality --warn-unreachable class A: a: str b: int @@ -722,6 +760,7 @@ match m: reveal_type(j) # N: Revealed type is "builtins.int" [case testMatchClassPatternCaptureSelf] +# flags: --strict-equality --warn-unreachable m: object match m: @@ -750,6 +789,7 @@ match m: [builtins fixtures/primitives.pyi] [case testMatchClassPatternNarrowSelfCapture] +# flags: --strict-equality --warn-unreachable m: object match m: @@ -778,6 +818,7 @@ match m: [builtins fixtures/primitives.pyi] [case testMatchClassPatternCaptureSelfSubtype] +# flags: --strict-equality --warn-unreachable class A(str): pass @@ -797,6 +838,7 @@ def f2(x: B): [builtins fixtures/tuple.pyi] [case testMatchInvalidClassPattern] +# flags: --strict-equality --warn-unreachable m: object match m: @@ -812,6 +854,7 @@ match m: reveal_type(n) # N: Revealed type is "Any" [case testMatchClassPatternCaptureDataclass] +# flags: --strict-equality --warn-unreachable from dataclasses import dataclass @dataclass @@ -828,6 +871,7 @@ match m: [builtins fixtures/dataclasses.pyi] [case testMatchClassPatternCaptureDataclassNoMatchArgs] +# flags: --strict-equality --warn-unreachable from dataclasses import dataclass @dataclass(match_args=False) @@ -839,10 +883,11 @@ m: A match m: case A(i, j): # E: Class "__main__.A" doesn't define "__match_args__" - pass + pass # E: Statement is unreachable [builtins fixtures/dataclasses.pyi] [case testMatchClassPatternCaptureDataclassPartialMatchArgs] +# flags: --strict-equality --warn-unreachable from dataclasses import dataclass, field @dataclass @@ -854,12 +899,13 @@ m: A match m: case A(i, j): # E: Too many positional patterns for class pattern - pass + pass # E: Statement is unreachable case A(k): reveal_type(k) # N: Revealed type is "builtins.str" [builtins fixtures/dataclasses.pyi] [case testMatchClassPatternCaptureNamedTupleInline] +# flags: --strict-equality --warn-unreachable from collections import namedtuple A = namedtuple("A", ["a", "b"]) @@ -873,6 +919,7 @@ match m: [builtins fixtures/list.pyi] [case testMatchClassPatternCaptureNamedTupleInlineTyped] +# flags: --strict-equality --warn-unreachable from typing import NamedTuple A = NamedTuple("A", [("a", str), ("b", int)]) @@ -886,6 +933,7 @@ match m: [builtins fixtures/list.pyi] [case testMatchClassPatternCaptureNamedTupleClass] +# flags: --strict-equality --warn-unreachable from typing import NamedTuple class A(NamedTuple): @@ -901,6 +949,7 @@ match m: [builtins fixtures/tuple.pyi] [case testMatchSequencePatternCaptureNamedTuple] +# flags: --strict-equality --warn-unreachable from typing import NamedTuple class N(NamedTuple): @@ -916,6 +965,7 @@ match a: [builtins fixtures/tuple.pyi] [case testMatchClassPatternCaptureGeneric] +# flags: --strict-equality --warn-unreachable from typing import Generic, TypeVar T = TypeVar('T') @@ -931,6 +981,7 @@ match m: reveal_type(i) # N: Revealed type is "Any" [case testMatchClassPatternCaptureVariadicGeneric] +# flags: --strict-equality --warn-unreachable from typing import Generic, Tuple from typing_extensions import TypeVarTuple, Unpack @@ -946,6 +997,7 @@ match m: [builtins fixtures/tuple.pyi] [case testMatchClassPatternCaptureGenericAlreadyKnown] +# flags: --strict-equality --warn-unreachable from typing import Generic, TypeVar T = TypeVar('T') @@ -961,6 +1013,7 @@ match m: reveal_type(i) # N: Revealed type is "builtins.int" [case testMatchClassPatternCaptureFilledGenericTypeAlias] +# flags: --strict-equality --warn-unreachable from typing import Generic, TypeVar T = TypeVar('T') @@ -974,9 +1027,10 @@ m: object match m: case B(a=i): # E: Class pattern class must not be a type alias with type parameters - reveal_type(i) + reveal_type(i) # E: Statement is unreachable [case testMatchClassPatternCaptureGenericTypeAlias] +# flags: --strict-equality --warn-unreachable from typing import Generic, TypeVar T = TypeVar('T') @@ -993,6 +1047,7 @@ match m: pass [case testMatchClassPatternNarrows] +# flags: --strict-equality --warn-unreachable from typing import Final class A: @@ -1010,6 +1065,7 @@ match m: [builtins fixtures/tuple.pyi] [case testMatchClassPatternNarrowsUnion] +# flags: --strict-equality --warn-unreachable from typing import Final, Union class A: @@ -1046,6 +1102,7 @@ match m: [builtins fixtures/tuple.pyi] [case testMatchClassPatternAlreadyNarrower] +# flags: --strict-equality --warn-unreachable from typing import Final class A: @@ -1066,6 +1123,7 @@ match m: [builtins fixtures/tuple.pyi] [case testMatchClassPatternIntersection] +# flags: --strict-equality --warn-unreachable from typing import Final class A: @@ -1084,6 +1142,7 @@ match m: [builtins fixtures/tuple.pyi] [case testMatchClassPatternNonexistentKeyword] +# flags: --strict-equality --warn-unreachable from typing import Any class A: ... @@ -1103,6 +1162,7 @@ match n: reveal_type(k) # N: Revealed type is "Any" [case testMatchClassPatternDuplicateKeyword] +# flags: --strict-equality --warn-unreachable class A: a: str @@ -1110,9 +1170,10 @@ m: object match m: case A(a=i, a=j): # E: Duplicate keyword pattern "a" - pass + pass # E: Statement is unreachable [case testMatchClassPatternDuplicateImplicitKeyword] +# flags: --strict-equality --warn-unreachable from typing import Final class A: @@ -1123,10 +1184,11 @@ m: object match m: case A(i, a=j): # E: Keyword "a" already matches a positional pattern - pass + pass # E: Statement is unreachable [builtins fixtures/tuple.pyi] [case testMatchClassPatternTooManyPositionals] +# flags: --strict-equality --warn-unreachable from typing import Final class A: @@ -1138,7 +1200,7 @@ m: object match m: case A(i, j, k): # E: Too many positional patterns for class pattern - pass + pass # E: Statement is unreachable [builtins fixtures/tuple.pyi] [case testMatchClassPatternIsNotType] @@ -1165,6 +1227,7 @@ def match_int_any(m: object, a: int | Any): reveal_type(j) # N: Revealed type is "Any" [case testMatchClassPatternAny] +# flags: --strict-equality --warn-unreachable from typing import Any Foo: Any @@ -1210,7 +1273,7 @@ def test_callable(x: Callable[[object], int]) -> None: reveal_type(other) # E: Statement is unreachable [case testMatchClassPatternCallbackProtocol] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable from typing import Any, Callable from typing_extensions import Protocol, runtime_checkable @@ -1252,7 +1315,7 @@ def test_callable(x: Callable[[object], int]) -> None: [builtins fixtures/dict.pyi] [case testMatchClassPatternAnyCallableProtocol] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable from typing import Any, Callable from typing_extensions import Protocol, runtime_checkable @@ -1288,6 +1351,7 @@ def test_callable(x: Callable[[object], int]) -> None: [case testMatchClassPatternProtocol] +# flags: --strict-equality --warn-unreachable from typing import Any from typing_extensions import Protocol, runtime_checkable @@ -1312,6 +1376,7 @@ def test_impl(x: Impl) -> None: [case testMatchClassPatternNestedGenerics] +# flags: --strict-equality --warn-unreachable # From cpython test_patma.py x = [[{0: 0}]] match x: @@ -1438,6 +1503,7 @@ def main(m: object): [builtins fixtures/tuple.pyi] [case testMatchNonFinalMatchArgs] +# flags: --strict-equality --warn-unreachable class A: __match_args__ = ("a", "b") a: str @@ -1452,6 +1518,7 @@ match m: [builtins fixtures/tuple.pyi] [case testMatchAnyTupleMatchArgs] +# flags: --strict-equality --warn-unreachable from typing import Tuple, Any class A: @@ -1469,6 +1536,7 @@ match m: [builtins fixtures/tuple.pyi] [case testMatchNonLiteralMatchArgs] +# flags: --strict-equality --warn-unreachable from typing import Final b: str = "b" @@ -1481,13 +1549,14 @@ m: object match m: case A(i, j, k): # E: Too many positional patterns for class pattern - pass + pass # E: Statement is unreachable case A(i, j): reveal_type(i) # N: Revealed type is "builtins.str" reveal_type(j) # N: Revealed type is "Any" [builtins fixtures/tuple.pyi] [case testMatchExternalMatchArgs] +# flags: --strict-equality --warn-unreachable from typing import Final, Literal args: Final = ("a", "b") @@ -1509,6 +1578,7 @@ class B: -- As Pattern -- [case testMatchAsPattern] +# flags: --strict-equality --warn-unreachable m: int match m: @@ -1517,6 +1587,7 @@ match m: reveal_type(l) # N: Revealed type is "builtins.int" [case testMatchAsPatternNarrows] +# flags: --strict-equality --warn-unreachable m: object match m: @@ -1524,6 +1595,7 @@ match m: reveal_type(l) # N: Revealed type is "builtins.int" [case testMatchAsPatternCapturesOr] +# flags: --strict-equality --warn-unreachable m: object match m: @@ -1531,6 +1603,7 @@ match m: reveal_type(n) # N: Revealed type is "Literal[1] | Literal[2]" [case testMatchAsPatternAlreadyNarrower] +# flags: --strict-equality --warn-unreachable m: bool match m: @@ -1540,6 +1613,7 @@ match m: -- Or Pattern -- [case testMatchOrPatternNarrows] +# flags: --strict-equality --warn-unreachable m: object match m: @@ -1547,6 +1621,7 @@ match m: reveal_type(m) # N: Revealed type is "Literal[1] | Literal[2]" [case testMatchOrPatternNarrowsStr] +# flags: --strict-equality --warn-unreachable m: object match m: @@ -1554,6 +1629,7 @@ match m: reveal_type(m) # N: Revealed type is "Literal['foo'] | Literal['bar']" [case testMatchOrPatternNarrowsUnion] +# flags: --strict-equality --warn-unreachable m: object match m: @@ -1561,6 +1637,7 @@ match m: reveal_type(m) # N: Revealed type is "Literal[1] | Literal['foo']" [case testMatchOrPatternCapturesMissing] +# flags: --strict-equality --warn-unreachable from typing import List m: List[int] @@ -1571,6 +1648,7 @@ match m: [builtins fixtures/list.pyi] [case testMatchOrPatternCapturesJoin] +# flags: --strict-equality --warn-unreachable m: object match m: @@ -1581,6 +1659,7 @@ match m: -- Interactions -- [case testMatchCapturePatternMultipleCases] +# flags: --strict-equality --warn-unreachable m: object match m: @@ -1592,6 +1671,7 @@ match m: reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testMatchCapturePatternMultipleCaptures] +# flags: --strict-equality --warn-unreachable from typing import Iterable m: Iterable[int] @@ -1602,6 +1682,7 @@ match m: [builtins fixtures/list.pyi] [case testMatchCapturePatternPreexistingSame] +# flags: --strict-equality --warn-unreachable a: int m: int @@ -1610,6 +1691,7 @@ match m: reveal_type(a) # N: Revealed type is "builtins.int" [case testMatchCapturePatternPreexistingNarrows] +# flags: --strict-equality --warn-unreachable a: int m: bool @@ -1622,6 +1704,7 @@ a = 3 reveal_type(a) # N: Revealed type is "builtins.int" [case testMatchCapturePatternPreexistingIncompatible] +# flags: --strict-equality --warn-unreachable a: str m: int @@ -1632,6 +1715,7 @@ match m: reveal_type(a) # N: Revealed type is "builtins.str" [case testMatchCapturePatternPreexistingIncompatibleLater] +# flags: --strict-equality --warn-unreachable a: str m: object @@ -1644,6 +1728,7 @@ match m: reveal_type(a) # N: Revealed type is "builtins.str" [case testMatchCapturePatternFromFunctionReturningUnion] +# flags: --strict-equality --warn-unreachable def func1(arg: bool) -> str | int: ... def func2(arg: bool) -> bytes | int: ... @@ -1659,6 +1744,7 @@ def main() -> None: reveal_type(a) # N: Revealed type is "builtins.int" [case testMatchCapturePatternFromAsyncFunctionReturningUnion] +# flags: --strict-equality --warn-unreachable async def func1(arg: bool) -> str | int: ... async def func2(arg: bool) -> bytes | int: ... @@ -1676,6 +1762,7 @@ async def main() -> None: -- Guards -- [case testMatchSimplePatternGuard] +# flags: --strict-equality --warn-unreachable m: str def guard() -> bool: ... @@ -1685,6 +1772,7 @@ match m: reveal_type(a) # N: Revealed type is "builtins.str" [case testMatchAlwaysTruePatternGuard] +# flags: --strict-equality --warn-unreachable m: str match m: @@ -1692,13 +1780,15 @@ match m: reveal_type(a) # N: Revealed type is "builtins.str" [case testMatchAlwaysFalsePatternGuard] +# flags: --strict-equality --warn-unreachable m: str match m: case a if False: - reveal_type(a) + reveal_type(a) # E: Statement is unreachable [case testMatchRedefiningPatternGuard] +# flags: --strict-equality --warn-unreachable m: str match m: @@ -1706,6 +1796,7 @@ match m: reveal_type(a) # N: Revealed type is "Literal[1]?" [case testMatchAssigningPatternGuard] +# flags: --strict-equality --warn-unreachable m: str match m: @@ -1713,6 +1804,7 @@ match m: reveal_type(a) # N: Revealed type is "builtins.str" [case testMatchNarrowingPatternGuard] +# flags: --strict-equality --warn-unreachable m: object match m: @@ -1721,6 +1813,7 @@ match m: [builtins fixtures/isinstancelist.pyi] [case testMatchIncompatiblePatternGuard] +# flags: --strict-equality --warn-unreachable class A: ... class B: ... @@ -1741,6 +1834,7 @@ match m: [builtins fixtures/isinstancelist.pyi] [case testMatchSubjectAssignExprWithGuard] +# flags: --strict-equality --warn-unreachable from typing import Optional def func() -> Optional[str]: ... @@ -1753,6 +1847,7 @@ match m := func(): -- Exhaustiveness -- [case testMatchUnionNegativeNarrowing] +# flags: --strict-equality --warn-unreachable from typing import Union m: Union[str, int] @@ -1766,6 +1861,7 @@ match m: reveal_type(m) # N: Revealed type is "builtins.int" [case testMatchOrPatternNegativeNarrowing] +# flags: --strict-equality --warn-unreachable from typing import Union m: Union[str, bytes, int] @@ -1778,6 +1874,7 @@ match m: reveal_type(b) # N: Revealed type is "builtins.int" [case testMatchExhaustiveReturn] +# flags: --strict-equality --warn-unreachable def foo(value) -> int: match value: case "bar": @@ -1786,6 +1883,7 @@ def foo(value) -> int: return 2 [case testMatchNonExhaustiveReturn] +# flags: --strict-equality --warn-unreachable def foo(value) -> int: # E: Missing return statement match value: case "bar": @@ -1794,6 +1892,7 @@ def foo(value) -> int: # E: Missing return statement return 2 [case testMatchMoreExhaustiveReturnCases] +# flags: --strict-equality --warn-unreachable def g(value: int | None) -> int: match value: case int(): @@ -1809,6 +1908,7 @@ def b(value: bool) -> int: return 3 [case testMatchMiscNonExhaustiveReturn] +# flags: --strict-equality --warn-unreachable class C: a: int | str @@ -1845,6 +1945,7 @@ def f5(x: bool) -> int: # E: Missing return statement [builtins fixtures/dict.pyi] [case testMatchNonExhaustiveError] +# flags: --strict-equality --warn-unreachable from typing import NoReturn def assert_never(x: NoReturn) -> None: ... @@ -1858,6 +1959,7 @@ def f(value: int) -> int: # E: Missing return statement assert_never(o) # E: Argument 1 to "assert_never" has incompatible type "int"; expected "Never" [case testMatchExhaustiveNoError] +# flags: --strict-equality --warn-unreachable from typing import NoReturn, Union, Literal def assert_never(x: NoReturn) -> None: ... @@ -1868,10 +1970,11 @@ def f(value: Literal[1] | Literal[2]) -> int: case 2: return 1 case o: - assert_never(o) + assert_never(o) # E: Statement is unreachable [typing fixtures/typing-medium.pyi] [case testMatchSequencePatternNegativeNarrowing] +# flags: --strict-equality --warn-unreachable from typing import Literal, Union, Sequence, Tuple m1: Sequence[int | str] @@ -2030,6 +2133,7 @@ def f6(d1: list[str], d2: list[str]): [case testMatchTupleUnions] +# flags: --strict-equality --warn-unreachable from typing_extensions import Unpack m1: tuple[int, str] | None @@ -2093,6 +2197,7 @@ match m9: [case testMatchEnumSingleChoice] +# flags: --strict-equality --warn-unreachable from enum import Enum from typing import NoReturn @@ -2112,12 +2217,13 @@ def f(m: Medal) -> None: # net change we don't end up inserting the LiteralType into the type map reveal_type(m) # N: Revealed type is "__main__.Medal" case _: - assert_never(m) + assert_never(m) # E: Statement is unreachable reveal_type(always_assigned) # N: Revealed type is "builtins.int" [builtins fixtures/bool.pyi] [case testMatchLiteralPatternEnumNegativeNarrowing] +# flags: --strict-equality --warn-unreachable from enum import Enum class Medal(Enum): gold = 1 @@ -2147,7 +2253,7 @@ def g(m: Medal) -> int: [builtins fixtures/enum.pyi] [case testMatchLiteralOrValuePattern] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable from typing import Literal def test1(x: Literal[1,2,3]) -> None: @@ -2176,6 +2282,7 @@ def test3(x: Literal[1,2,3]) -> None: reveal_type(x) # N: Revealed type is "Literal[2]" [case testMatchLiteralPatternEnumWithTypedAttribute] +# flags: --strict-equality --warn-unreachable from enum import Enum from typing import NoReturn def assert_never(x: NoReturn) -> None: ... @@ -2206,11 +2313,12 @@ match m: case Medal.bronze: reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.bronze]" case _ as unreachable: - assert_never(unreachable) + assert_never(unreachable) # E: Statement is unreachable [builtins fixtures/tuple.pyi] [case testMatchLiteralPatternFunctionalEnum] +# flags: --strict-equality --warn-unreachable from enum import Enum from typing import NoReturn def assert_never(x: NoReturn) -> None: ... @@ -2226,7 +2334,7 @@ match m: case Medal.bronze: reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.bronze]" case _ as unreachable: - assert_never(unreachable) + assert_never(unreachable) # E: Statement is unreachable [builtins fixtures/enum.pyi] [case testMatchLiteralPatternEnumCustomEquals] @@ -2249,6 +2357,7 @@ match m: [builtins fixtures/enum.pyi] [case testMatchNarrowUsingPatternGuardSpecialCase] +# flags: --strict-equality --warn-unreachable def f(x: int | str) -> int: match x: case x if isinstance(x, str): @@ -2258,6 +2367,7 @@ def f(x: int | str) -> int: [builtins fixtures/isinstance.pyi] [case testMatchNarrowDownUnionPartially] +# flags: --strict-equality --warn-unreachable def f(x: int | str) -> None: match x: @@ -2278,6 +2388,7 @@ def h(x: int | str | None) -> None: reveal_type(x) # N: Revealed type is "None" [case testMatchNarrowDownUsingLiteralMatch] +# flags: --strict-equality --warn-unreachable from enum import Enum class Medal(Enum): gold = 1 @@ -2327,6 +2438,7 @@ def union(x: str | bool) -> None: [builtins fixtures/tuple.pyi] [case testMatchNarrowDownUnionUsingClassPattern] +# flags: --strict-equality --warn-unreachable class Foo: ... class Bar(Foo): ... @@ -2343,6 +2455,7 @@ def test_2(bar: Bar | str) -> None: [case testMatchAssertFalseToSilenceFalsePositives] +# flags: --strict-equality --warn-unreachable class C: a: int | str @@ -2364,6 +2477,7 @@ def g(c: C) -> int: assert False [case testMatchAsPatternExhaustiveness] +# flags: --strict-equality --warn-unreachable def f(x: int | str) -> int: match x: case int() as n: @@ -2372,6 +2486,7 @@ def f(x: int | str) -> int: return 1 [case testMatchOrPatternExhaustiveness] +# flags: --strict-equality --warn-unreachable from typing import NoReturn, Literal def assert_never(x: NoReturn) -> None: ... @@ -2402,6 +2517,7 @@ def f(x: A) -> None: reveal_type(y) # N: Revealed type is "__main__. | __main__." [case testMatchWithBreakAndContinue] +# flags: --strict-equality --warn-unreachable def f(x: int | str | None) -> None: i = int() while i: @@ -2414,6 +2530,7 @@ def f(x: int | str | None) -> None: reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None" [case testMatchNarrowDownWithStarred-skip] +# flags: --strict-equality --warn-unreachable from typing import List def f(x: List[int] | int) -> None: match x: @@ -2426,6 +2543,7 @@ def f(x: List[int] | int) -> None: -- Misc [case testMatchAndWithStatementScope] +# flags: --strict-equality --warn-unreachable from m import A, B with A() as x: @@ -2467,6 +2585,7 @@ class B: def __exit__(self, x, y, z) -> None: ... [case testOverrideMatchArgs] +# flags: --strict-equality --warn-unreachable class AST: __match_args__ = () @@ -2501,7 +2620,7 @@ def f(x: AST) -> None: [builtins fixtures/tuple.pyi] [case testMatchReachableDottedNames] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable class Consts: BLANK = "" SPECIAL = "asdf" @@ -2517,6 +2636,7 @@ def test_func(test_str: str) -> str: [case testNoneTypeWarning] +# flags: --strict-equality --warn-unreachable from types import NoneType def foo(x: NoneType): # E: NoneType should not be used as a type, please use None instead @@ -2525,6 +2645,7 @@ def foo(x: NoneType): # E: NoneType should not be used as a type, please use Non [builtins fixtures/tuple.pyi] [case testMatchTupleInstanceUnionNoCrash] +# flags: --strict-equality --warn-unreachable from typing import Union def func(e: Union[str, tuple[str]]) -> None: @@ -2534,6 +2655,7 @@ def func(e: Union[str, tuple[str]]) -> None: [builtins fixtures/tuple.pyi] [case testMatchTupleOptionalNoCrash] +# flags: --strict-equality --warn-unreachable foo: tuple[int] | None match foo: case x,: @@ -2560,6 +2682,7 @@ def main2(var: tuple[int, int] | tuple[str, str] | tuple[str, int]): [builtins fixtures/tuple.pyi] [case testMatchNamedAndKeywordsAreTheSame] +# flags: --strict-equality --warn-unreachable from typing import Generic, Final, TypeVar, Union from dataclasses import dataclass @@ -2626,6 +2749,7 @@ match input_arg: [builtins fixtures/dataclasses.pyi] [case testMatchValueConstrainedTypeVar] +# flags: --strict-equality --warn-unreachable from typing import TypeVar, Iterable S = TypeVar("S", int, str) @@ -2641,7 +2765,7 @@ def my_func(pairs: Iterable[tuple[S, S]]) -> None: [builtins fixtures/tuple.pyi] [case testPossiblyUndefinedMatch] -# flags: --enable-error-code possibly-undefined +# flags: --enable-error-code possibly-undefined --strict-equality --warn-unreachable def f0(x: int | str) -> int: match x: case int(): @@ -2704,7 +2828,7 @@ def f6(a: object) -> None: [builtins fixtures/tuple.pyi] [case testPossiblyUndefinedMatchUnreachable] -# flags: --enable-error-code possibly-undefined +# flags: --enable-error-code possibly-undefined --strict-equality --warn-unreachable import typing def f0(x: int) -> int: @@ -2728,7 +2852,7 @@ def f1(x: int) -> int: [typing fixtures/typing-medium.pyi] [case testUsedBeforeDefMatchWalrus] -# flags: --enable-error-code used-before-def +# flags: --enable-error-code used-before-def --strict-equality --warn-unreachable import typing def f0(x: int) -> None: @@ -2741,6 +2865,7 @@ def f0(x: int) -> None: d = y [case testTypeAliasWithNewUnionSyntaxAndNoneLeftOperand] +# flags: --strict-equality --warn-unreachable from typing import overload class C: @overload @@ -2757,6 +2882,7 @@ Y = None | D [builtins fixtures/type.pyi] [case testMatchStatementWalrus] +# flags: --strict-equality --warn-unreachable class A: a = 1 @@ -2781,6 +2907,7 @@ def f() -> None: reveal_type(y.a) # N: Revealed type is "builtins.int" [case testNarrowedVariableInNestedModifiedInMatch] +# flags: --strict-equality --warn-unreachable from typing import Optional def match_stmt_error1(x: Optional[str]) -> None: @@ -2850,7 +2977,7 @@ def match_stmt_error5(x: Optional[str]) -> None: [builtins fixtures/tuple.pyi] [case testMatchSubjectRedefinition] -# flags: --allow-redefinition +# flags: --allow-redefinition --strict-equality --warn-unreachable def transform1(a: str) -> int: ... @@ -2875,6 +3002,7 @@ def redefinition_bad(a: int): [builtins fixtures/primitives.pyi] [case testPatternMatchingClassPatternLocation] +# flags: --strict-equality --warn-unreachable # See https://github.com/python/mypy/issues/15496 from some_missing_lib import DataFrame, Series # type: ignore[import] from typing import TypeVar @@ -2895,7 +3023,7 @@ def f2(x: T) -> None: [builtins fixtures/primitives.pyi] [case testMatchGuardReachability] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable def f1(e: int) -> int: match e: case x if True: @@ -2937,7 +3065,7 @@ def f4(e: int | str | bytes) -> int: [builtins fixtures/primitives.pyi] [case testMatchGuardAlwaysTrueAlwaysFalse] -# flags: --warn-unreachable --always-true TRUEFLAG --always-false FALSEFLAG +# flags: --warn-unreachable --always-true TRUEFLAG --always-false FALSEFLAG --strict-equality TRUEFLAG = False FALSEFLAG = True @@ -2956,6 +3084,7 @@ def false_flag(e: int) -> None: 1 + "asdf" [case testMatchSequencePatternVariadicTupleNotTooShort] +# flags: --strict-equality --warn-unreachable from typing import Tuple from typing_extensions import Unpack @@ -2969,7 +3098,7 @@ match fm1: fm2: Tuple[int, int, Unpack[Tuple[str, ...]], int] match fm2: case [fa2, fb2]: - reveal_type(fa2) + reveal_type(fa2) # E: Statement is unreachable reveal_type(fb2) fm3: Tuple[int, int, Unpack[Tuple[str, ...]], int] @@ -3006,6 +3135,7 @@ match m3: [builtins fixtures/tuple.pyi] [case testMatchSequencePatternTypeVarTupleNotTooShort] +# flags: --strict-equality --warn-unreachable from typing import Tuple from typing_extensions import Unpack, TypeVarTuple @@ -3021,7 +3151,7 @@ def test(xs: Tuple[Unpack[Ts]]) -> None: fm2: Tuple[int, int, Unpack[Ts], int] match fm2: case [fa2, fb2]: - reveal_type(fa2) + reveal_type(fa2) # E: Statement is unreachable reveal_type(fb2) fm3: Tuple[int, int, Unpack[Ts], int] @@ -3058,6 +3188,7 @@ def test(xs: Tuple[Unpack[Ts]]) -> None: [builtins fixtures/tuple.pyi] [case testMatchSequencePatternTypeVarBoundNoCrash] +# flags: --strict-equality --warn-unreachable # This was crashing: https://github.com/python/mypy/issues/18089 from typing import TypeVar, Sequence, Any @@ -3070,6 +3201,7 @@ def f(x: T) -> None: [builtins fixtures/tuple.pyi] [case testMatchSequencePatternTypeVarBoundNarrows] +# flags: --strict-equality --warn-unreachable from typing import TypeVar, Sequence T = TypeVar("T", bound=Sequence[int | str]) @@ -3085,7 +3217,7 @@ def f(x: T) -> None: [builtins fixtures/tuple.pyi] [case testNarrowingTypeVarMatch] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable # https://github.com/python/mypy/issues/18126 from typing import TypeVar @@ -3100,6 +3232,7 @@ def fn_case(arg: T) -> None: [builtins fixtures/primitives.pyi] [case testNoneCheckDoesNotMakeTypeVarOptionalMatch] +# flags: --strict-equality --warn-unreachable from typing import TypeVar T = TypeVar('T') @@ -3115,7 +3248,7 @@ def foo(x: T) -> T: [builtins fixtures/isinstance.pyi] [case testMatchSequenceReachableFromAny] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable from typing import Any def maybe_list(d: Any) -> int: @@ -3147,14 +3280,15 @@ def nested_in_dict(d: dict[str, Any]) -> int: [builtins fixtures/dict.pyi] [case testMatchRebindsOuterFunctionName] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable from typing import Literal def f() -> None: def x() -> tuple[Literal["test"]]: ... match x(): - case (x,) if x == "test": # E: Incompatible types in capture pattern (pattern captures type "Literal['test']", variable has type "Callable[[], tuple[Literal['test']]]") + case (x,) if x == "test": # E: Incompatible types in capture pattern (pattern captures type "Literal['test']", variable has type "Callable[[], tuple[Literal['test']]]") \ + # E: Non-overlapping equality check (left operand type: "Callable[[], tuple[Literal['test']]]", right operand type: "Literal['test']") reveal_type(x) # E: Statement is unreachable case foo: foo @@ -3162,7 +3296,7 @@ def f() -> None: [builtins fixtures/dict.pyi] [case testMatchRebindsInnerFunctionName] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable class Some: value: int | str __match_args__ = ("value",) @@ -3199,7 +3333,7 @@ def fn2(x: Some | int | str) -> None: [builtins fixtures/dict.pyi] [case testMatchFunctionCall] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable def fn() -> int | str: ... @@ -3212,7 +3346,7 @@ match fn(): other # E: Statement is unreachable [case testMatchAttribute] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable class A: foo: int | str @@ -3226,7 +3360,7 @@ match A().foo: other # E: Statement is unreachable [case testMatchLiteral] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable def int_literal() -> None: match 12: @@ -3247,7 +3381,7 @@ def str_literal() -> None: other # E: Statement is unreachable [case testMatchOperations] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable x: int match -x: @@ -3276,7 +3410,7 @@ match 1 > 2: [builtins fixtures/ops.pyi] [case testMatchDictItem] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable m: dict[str, int | str] k: str @@ -3292,7 +3426,7 @@ match m[k]: [builtins fixtures/dict.pyi] [case testMatchLiteralValuePathological] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable match 0: case 0 as i: @@ -3303,6 +3437,7 @@ match 0: other # E: Statement is unreachable [case testMatchNamedTupleSequence] +# flags: --strict-equality --warn-unreachable from typing import Any, NamedTuple class T(NamedTuple): @@ -3318,7 +3453,7 @@ def f(t: T) -> None: [builtins fixtures/tuple.pyi] [case testMatchTypeObjectTypeVar] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable from typing import TypeVar import b @@ -3343,7 +3478,7 @@ class Two: ... [builtins fixtures/tuple.pyi] [case testNewRedefineMatchBasics] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition-new --local-partial-types --strict-equality --warn-unreachable def f1(x: int | str | list[bytes]) -> None: match x: @@ -3356,7 +3491,7 @@ def f1(x: int | str | list[bytes]) -> None: reveal_type(y) # N: Revealed type is "builtins.str | builtins.bytes" [case testNewRedefineLoopWithMatch] -# flags: --allow-redefinition-new --local-partial-types +# flags: --allow-redefinition-new --local-partial-types --strict-equality --warn-unreachable def f1() -> None: while True: @@ -3381,6 +3516,7 @@ def f2() -> None: [builtins fixtures/list.pyi] [case testExhaustiveMatchNoFlag] +# flags: --strict-equality --warn-unreachable a: int = 5 match a: @@ -3397,6 +3533,7 @@ match b: pass [case testNonExhaustiveMatchNoFlag] +# flags: --strict-equality --warn-unreachable a: int = 5 match a: @@ -3410,7 +3547,7 @@ match b: [case testExhaustiveMatchWithFlag] -# flags: --enable-error-code exhaustive-match +# flags: --enable-error-code exhaustive-match --strict-equality --warn-unreachable a: int = 5 match a: @@ -3427,7 +3564,7 @@ match b: pass [case testNonExhaustiveMatchWithFlag] -# flags: --enable-error-code exhaustive-match +# flags: --enable-error-code exhaustive-match --strict-equality --warn-unreachable a: int = 5 match a: # E: Match statement has unhandled case for values of type "int" \ @@ -3441,7 +3578,7 @@ match b: # E: Match statement has unhandled case for values of type "str" \ case "bye": pass [case testNonExhaustiveMatchEnumWithFlag] -# flags: --enable-error-code exhaustive-match +# flags: --enable-error-code exhaustive-match --strict-equality --warn-unreachable import enum @@ -3461,7 +3598,7 @@ match val: # E: Match statement has unhandled case for values of type "Literal[C [builtins fixtures/enum.pyi] [case testExhaustiveMatchEnumWithFlag] -# flags: --enable-error-code exhaustive-match +# flags: --enable-error-code exhaustive-match --strict-equality --warn-unreachable import enum @@ -3479,7 +3616,7 @@ match val: [builtins fixtures/enum.pyi] [case testNonExhaustiveMatchEnumMultipleMissingMatchesWithFlag] -# flags: --enable-error-code exhaustive-match +# flags: --enable-error-code exhaustive-match --strict-equality --warn-unreachable import enum @@ -3497,7 +3634,7 @@ match val: # E: Match statement has unhandled case for values of type "Literal[C [builtins fixtures/enum.pyi] [case testExhaustiveMatchEnumFallbackWithFlag] -# flags: --enable-error-code exhaustive-match +# flags: --enable-error-code exhaustive-match --strict-equality --warn-unreachable import enum @@ -3517,7 +3654,7 @@ match val: # Fork of testMatchNarrowingUnionTypedDictViaIndex to check behaviour with exhaustive match flag [case testExhaustiveMatchNarrowingUnionTypedDictViaIndex] -# flags: --enable-error-code exhaustive-match +# flags: --enable-error-code exhaustive-match --strict-equality --warn-unreachable from typing import Literal, TypedDict @@ -3539,6 +3676,7 @@ match d["tag"]: # E: Match statement has unhandled case for values of type "Lite [typing fixtures/typing-typeddict.pyi] [case testEnumTypeObjectMember] +# flags: --strict-equality --warn-unreachable import enum from typing import NoReturn @@ -3556,10 +3694,11 @@ match value_type: case ValueType.STR: pass case _: - assert_never(value_type) + assert_never(value_type) # E: Statement is unreachable [builtins fixtures/tuple.pyi] [case testAssignmentToFinalInMatchCaseNotAllowed] +# flags: --strict-equality --warn-unreachable from typing import Final FOO: Final[int] = 10 @@ -3570,7 +3709,7 @@ match val: pass [case testMatchExhaustivenessWithDeferral] -# flags: --enable-error-code exhaustive-match +# flags: --enable-error-code exhaustive-match --strict-equality --warn-unreachable from typing import Literal import unknown_module # E: Cannot find implementation or library stub for module named "unknown_module" \ # N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports @@ -3585,6 +3724,7 @@ def foo(e: Literal[0, 1]) -> None: defer = unknown_module.foo [case testMatchErrorsIncorrectName] +# flags: --strict-equality --warn-unreachable class A: pass @@ -3593,6 +3733,7 @@ match 5: pass [case testMatchAllowsAnyClassArgsForAny] +# flags: --strict-equality --warn-unreachable match 5: case BlahBlah(a, b): # E: Name "BlahBlah" is not defined reveal_type(a) # N: Revealed type is "Any" @@ -3601,6 +3742,7 @@ match 5: reveal_type(c) # N: Revealed type is "Any" [case testMatchAllowsNoneTypeAsClass] +# flags: --strict-equality --warn-unreachable import types class V: @@ -3617,6 +3759,7 @@ def fun(val: str | None): [builtins fixtures/tuple.pyi] [case testMatchMappingRestErrorLocation] +# flags: --strict-equality --warn-unreachable rest: list[int] data: list[int] match data: @@ -3629,7 +3772,7 @@ match config: [builtins fixtures/dict.pyi] [case testMatchTypeNarrowing] -# flags: --warn-unreachable +# flags: --strict-equality --warn-unreachable import builtins import typing import types @@ -3703,6 +3846,7 @@ def type_object(field_type: type[object]) -> str: [case testMatchFinalClass] +# flags: --strict-equality --warn-unreachable from typing import final, NoReturn def assert_never(x: NoReturn) -> NoReturn: ...