Skip to content

Commit 6cb4d7f

Browse files
committed
Use assert_never for enum fallbacks
1 parent e8b5ac7 commit 6cb4d7f

4 files changed

Lines changed: 22 additions & 16 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ run.patch = [ "subprocess" ]
385385
run.relative_files = true
386386
run.source = [ "ci/", "src/", "tests/" ]
387387
report.exclude_also = [
388+
"case _ as unreachable:\n\\s*assert_never\\(",
388389
"class .*\\bProtocol\\):",
389390
"if TYPE_CHECKING:",
390391
]

src/mock_vws/_flask_server/target_manager.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
from enum import StrEnum, auto
88
from http import HTTPMethod, HTTPStatus
9+
from typing import assert_never
910
from zoneinfo import ZoneInfo
1011

1112
from beartype import beartype
@@ -37,17 +38,19 @@ class _TargetRaterChoice(StrEnum):
3738
PERFECT = auto()
3839
RANDOM = auto()
3940

40-
def to_target_rater(self) -> TargetTrackingRater:
41+
def to_target_rater(
42+
self: _TargetRaterChoice,
43+
) -> TargetTrackingRater:
4144
"""Get the target rater."""
4245
match self:
43-
case self.BRISQUE:
46+
case _TargetRaterChoice.BRISQUE:
4447
return BrisqueTargetTrackingRater()
45-
case self.PERFECT:
48+
case _TargetRaterChoice.PERFECT:
4649
return HardcodedTargetTrackingRater(rating=5)
47-
case self.RANDOM:
50+
case _TargetRaterChoice.RANDOM:
4851
return RandomTargetTrackingRater()
49-
case _: # pragma: no cover
50-
raise ValueError
52+
case _ as unreachable:
53+
assert_never(unreachable)
5154

5255

5356
@beartype

src/mock_vws/_flask_server/vwq.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import time
99
from enum import StrEnum, auto
1010
from http import HTTPMethod, HTTPStatus
11+
from typing import assert_never
1112

1213
import requests
1314
from beartype import beartype
@@ -39,15 +40,15 @@ class _ImageMatcherChoice(StrEnum):
3940
EXACT = auto()
4041
STRUCTURAL_SIMILARITY = auto()
4142

42-
def to_image_matcher(self) -> ImageMatcher:
43+
def to_image_matcher(self: _ImageMatcherChoice) -> ImageMatcher:
4344
"""Get the image matcher."""
4445
match self:
45-
case self.EXACT:
46+
case _ImageMatcherChoice.EXACT:
4647
return ExactMatcher()
47-
case self.STRUCTURAL_SIMILARITY:
48+
case _ImageMatcherChoice.STRUCTURAL_SIMILARITY:
4849
return StructuralSimilarityMatcher()
49-
case _: # pragma: no cover
50-
raise ValueError
50+
case _ as unreachable:
51+
assert_never(unreachable)
5152

5253

5354
@beartype

src/mock_vws/_flask_server/vws.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import uuid
1313
from enum import StrEnum, auto
1414
from http import HTTPMethod, HTTPStatus
15+
from typing import assert_never
1516

1617
import requests
1718
from beartype import beartype
@@ -62,15 +63,15 @@ class _ImageMatcherChoice(StrEnum):
6263
EXACT = auto()
6364
STRUCTURAL_SIMILARITY = auto()
6465

65-
def to_image_matcher(self) -> ImageMatcher:
66+
def to_image_matcher(self: _ImageMatcherChoice) -> ImageMatcher:
6667
"""Get the image matcher."""
6768
match self:
68-
case self.EXACT:
69+
case _ImageMatcherChoice.EXACT:
6970
return ExactMatcher()
70-
case self.STRUCTURAL_SIMILARITY:
71+
case _ImageMatcherChoice.STRUCTURAL_SIMILARITY:
7172
return StructuralSimilarityMatcher()
72-
case _: # pragma: no cover
73-
raise ValueError
73+
case _ as unreachable:
74+
assert_never(unreachable)
7475

7576

7677
@beartype

0 commit comments

Comments
 (0)