mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
gh-90633: Improve error and docs for typing.assert_never (#91720)
Closes #90633 Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
9ff2f12c87
commit
93d280141c
4 changed files with 37 additions and 2 deletions
|
|
@ -2382,6 +2382,9 @@ def is_typeddict(tp):
|
|||
return isinstance(tp, _TypedDictMeta)
|
||||
|
||||
|
||||
_ASSERT_NEVER_REPR_MAX_LENGTH = 100
|
||||
|
||||
|
||||
def assert_never(arg: Never, /) -> Never:
|
||||
"""Statically assert that a line of code is unreachable.
|
||||
|
||||
|
|
@ -2402,7 +2405,10 @@ def assert_never(arg: Never, /) -> Never:
|
|||
At runtime, this throws an exception when called.
|
||||
|
||||
"""
|
||||
raise AssertionError("Expected code to be unreachable")
|
||||
value = repr(arg)
|
||||
if len(value) > _ASSERT_NEVER_REPR_MAX_LENGTH:
|
||||
value = value[:_ASSERT_NEVER_REPR_MAX_LENGTH] + '...'
|
||||
raise AssertionError(f"Expected code to be unreachable, but got: {value}")
|
||||
|
||||
|
||||
def no_type_check(arg):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue