Skip to content

Commit 57e38cc

Browse files
committed
compat: assert_never is now in the stdlib
Use the upstream implementation, so we can easily drop our own impl when we drop Python 3.10.
1 parent d70cf2c commit 57e38cc

File tree

1 file changed

+6
-33
lines changed

1 file changed

+6
-33
lines changed

src/_pytest/compat.py

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -278,39 +278,12 @@ def get_user_id() -> int | None:
278278
return uid if uid != ERROR else None
279279

280280

281-
# Perform exhaustiveness checking.
282-
#
283-
# Consider this example:
284-
#
285-
# MyUnion = Union[int, str]
286-
#
287-
# def handle(x: MyUnion) -> int {
288-
# if isinstance(x, int):
289-
# return 1
290-
# elif isinstance(x, str):
291-
# return 2
292-
# else:
293-
# raise Exception('unreachable')
294-
#
295-
# Now suppose we add a new variant:
296-
#
297-
# MyUnion = Union[int, str, bytes]
298-
#
299-
# After doing this, we must remember ourselves to go and update the handle
300-
# function to handle the new variant.
301-
#
302-
# With `assert_never` we can do better:
303-
#
304-
# // raise Exception('unreachable')
305-
# return assert_never(x)
306-
#
307-
# Now, if we forget to handle the new variant, the type-checker will emit a
308-
# compile-time error, instead of the runtime error we would have gotten
309-
# previously.
310-
#
311-
# This also work for Enums (if you use `is` to compare) and Literals.
312-
def assert_never(value: NoReturn) -> NoReturn:
313-
assert False, f"Unhandled value: {value} ({type(value).__name__})"
281+
if sys.version_info >= (3, 11):
282+
from typing import assert_never
283+
else:
284+
285+
def assert_never(value: NoReturn) -> NoReturn:
286+
assert False, f"Unhandled value: {value} ({type(value).__name__})"
314287

315288

316289
class CallableBool:

0 commit comments

Comments
 (0)