diff options
author | Jelle Zijlstra <jelle.zijlstra@gmail.com> | 2022-04-25 13:40:18 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-25 13:40:18 (GMT) |
commit | 93d280141c369fd1906569ff605148b6e22f6a43 (patch) | |
tree | 01714d7314fffffdac59f85006d0b23759a22044 /Lib/typing.py | |
parent | 9ff2f12c876289a7192fd1672ed08a1876a51e17 (diff) | |
download | cpython-93d280141c369fd1906569ff605148b6e22f6a43.zip cpython-93d280141c369fd1906569ff605148b6e22f6a43.tar.gz cpython-93d280141c369fd1906569ff605148b6e22f6a43.tar.bz2 |
gh-90633: Improve error and docs for typing.assert_never (#91720)
Closes #90633
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Diffstat (limited to 'Lib/typing.py')
-rw-r--r-- | Lib/typing.py | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/Lib/typing.py b/Lib/typing.py index a6f4fa9..9d8149c 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -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): |