Skip to content

Commit fecd37d

Browse files
committed
Change to error code only for exhaustive match
1 parent 1a3e359 commit fecd37d

File tree

8 files changed

+17
-69
lines changed

8 files changed

+17
-69
lines changed

docs/source/command_line.rst

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -799,48 +799,6 @@ of the above sections.
799799
x = 'a string'
800800
x.trim() # error: "str" has no attribute "trim" [attr-defined]
801801
802-
.. option:: --disallow-inexhaustive-match-statements
803-
804-
This flag will cause mypy to report an error whenever it encounters a match statement
805-
that does not cover all possible cases.
806-
807-
.. code-block:: python
808-
809-
import enum
810-
811-
812-
class Color(enum.Enum):
813-
RED = 1
814-
BLUE = 2
815-
816-
val: Color = Color.RED
817-
818-
# without --disallow-inexhaustive-match-statements
819-
match val:
820-
case Color.RED:
821-
print("red")
822-
823-
# Also no issues without --disallow-inexhaustive-match-statements, but this is exhaustive
824-
match val:
825-
case Color.RED:
826-
print("red")
827-
case _:
828-
print("other")
829-
830-
# with --disallow-inexhaustive-match-statements
831-
# error: Cases within match statement do not exhaustively handle all values: "Literal[Color.BLUE]". If not intended to handle all cases, use `case _: pass`
832-
match val:
833-
case Color.RED:
834-
print("red")
835-
836-
# no error with --disallow-inexhaustive-match-statements since all cases are handled
837-
match val:
838-
case Color.RED:
839-
print("red")
840-
case _:
841-
print("other")
842-
843-
844802
845803
.. _configuring-error-messages:
846804

docs/source/error_code_list2.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ Example:
619619
Check that match statements match exhaustively [match-exhaustive]
620620
-----------------------------------------------------------------------
621621

622-
If you use :option:`--disallow-inexhaustive-match-statements <mypy --disallow-inexhaustive-match-statements>`,
622+
If enabled with :option:`--enable-error-code exhaustive-match <mypy --enable-error-code>`,
623623
mypy generates an error if a match statement does not match all possible cases/types.
624624

625625

@@ -636,25 +636,25 @@ Example:
636636
637637
val: Color = Color.RED
638638
639-
# without --disallow-inexhaustive-match-statements
639+
# without --enable-error-code exhaustive-match
640640
match val:
641641
case Color.RED:
642642
print("red")
643643
644-
# Also no issues without --disallow-inexhaustive-match-statements, but this is exhaustive
644+
# Also no issues without --enable-error-code exhaustive-match, but this is exhaustive
645645
match val:
646646
case Color.RED:
647647
print("red")
648648
case _:
649649
print("other")
650650
651-
# with --disallow-inexhaustive-match-statements
651+
# with --enable-error-code exhaustive-match
652652
# error: Cases within match statement do not exhaustively handle all values: "Literal[Color.BLUE]". If not intended to handle all cases, use `case _: pass`
653653
match val:
654654
case Color.RED:
655655
print("red")
656656
657-
# no error with --disallow-inexhaustive-match-statements since all cases are handled
657+
# no error with --enable-error-code exhaustive-match since all cases are handled
658658
match val:
659659
case Color.RED:
660660
print("red")

docs/source/literal_types.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ Exhaustiveness checking is also supported for match statements (Python 3.10 and
471471

472472
For match statements specifically, inexhaustive matches can be caught
473473
without needing to use ``assert_never`` by using
474-
:option:`--disallow-inexhaustive-match-statements <mypy --disallow-inexhaustive-match-statements>`.
474+
:option:`--enable-error-code exhaustive-match <mypy --enable-error-code>`.
475475

476476

477477
Extra Enum checks

mypy/checker.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5511,10 +5511,7 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
55115511
self.push_type_map(else_map, from_assignment=False)
55125512
unmatched_types = else_map
55135513

5514-
if (
5515-
self.options.disallow_inexhaustive_match_statements is True
5516-
and unmatched_types is not None
5517-
):
5514+
if unmatched_types is not None:
55185515
for typ in set(unmatched_types.values()):
55195516
self.msg.match_statement_inexhaustive_match(typ, s)
55205517

mypy/errorcodes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,10 @@ def __hash__(self) -> int:
265265
default_enabled=False,
266266
)
267267
EXHAUSTIVE_MATCH: Final = ErrorCode(
268-
"exhaustive-match", "Reject match statements that are not exhaustive", "General"
268+
"exhaustive-match",
269+
"Reject match statements that are not exhaustive",
270+
"General",
271+
default_enabled=False,
269272
)
270273

271274
# Syntax errors are often blocking.

mypy/main.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -931,13 +931,6 @@ def add_invertible_flag(
931931
"and makes arguments prepended via Concatenate positional-only",
932932
group=strictness_group,
933933
)
934-
add_invertible_flag(
935-
"--disallow-inexhaustive-match-statements",
936-
default=False,
937-
strict_flag=False,
938-
help="Raise type error for match statements that do not match exhaustively",
939-
group=strictness_group,
940-
)
941934

942935
strict_help = "Strict mode; enables the following flags: {}".format(
943936
", ".join(strict_flag_names)

mypy/options.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,6 @@ def __init__(self) -> None:
349349
# Use this sparingly to avoid tests diverging from non-test behavior.
350350
self.test_env = False
351351

352-
# Only allow exhaustive match statements
353-
self.disallow_inexhaustive_match_statements = False
354-
355352
# -- experimental options --
356353
self.shadow_file: list[list[str]] | None = None
357354
self.show_column_numbers: bool = False

test-data/unit/check-python310.test

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,7 +2669,7 @@ match b:
26692669

26702670

26712671
[case testExhaustiveMatchWithFlag]
2672-
# flags: --disallow-inexhaustive-match-statements
2672+
# flags: --enable-error-code exhaustive-match
26732673

26742674
a: int = 5
26752675
match a:
@@ -2686,7 +2686,7 @@ match b:
26862686
pass
26872687

26882688
[case testNonExhaustiveMatchWithFlag]
2689-
# flags: --disallow-inexhaustive-match-statements
2689+
# flags: --enable-error-code exhaustive-match
26902690

26912691
a: int = 5
26922692
match a: # E: Cases within match statement do not exhaustively handle all values: "int". If not intended to handle all cases, use `case _: pass`
@@ -2699,7 +2699,7 @@ match b: # E: Cases within match statement do not exhaustively handle all values
26992699
pass
27002700

27012701
[case testNonExhaustiveMatchEnumWithFlag]
2702-
# flags: --disallow-inexhaustive-match-statements
2702+
# flags: --enable-error-code exhaustive-match
27032703

27042704
import enum
27052705

@@ -2720,7 +2720,7 @@ match val: # E: Cases within match statement do not exhaustively handle all valu
27202720

27212721

27222722
[case testExhaustiveMatchEnumWithFlag]
2723-
# flags: --disallow-inexhaustive-match-statements
2723+
# flags: --enable-error-code exhaustive-match
27242724

27252725
import enum
27262726

@@ -2739,7 +2739,7 @@ match val:
27392739
[builtins fixtures/enum.pyi]
27402740

27412741
[case testNonExhaustiveMatchEnumMultipleMissingMatchesWithFlag]
2742-
# flags: --disallow-inexhaustive-match-statements
2742+
# flags: --enable-error-code exhaustive-match
27432743

27442744
import enum
27452745

@@ -2758,7 +2758,7 @@ match val: # E: Cases within match statement do not exhaustively handle all valu
27582758
[builtins fixtures/enum.pyi]
27592759

27602760
[case testExhaustiveMatchEnumFallbackWithFlag]
2761-
# flags: --disallow-inexhaustive-match-statements
2761+
# flags: --enable-error-code exhaustive-match
27622762

27632763
import enum
27642764

0 commit comments

Comments
 (0)