Skip to content

Commit f83b056

Browse files
committed
Revise assert_never
This revises the docstring of git.types.assert_never to more clearly express its semantics and usage, to use the type names used in the typing module, typing_extensions package, and mypy. This expands some parts when doing so seemed to benefit clarity. The docstring had previously said AssertionError was raised, but the code raised ValueError. For now I've adjusted the docstring to be accurate to the code's behavior, but maybe this should change. I also removed the part about the mypy error being on variable creation, since I am not clear on what that was referring to, and if it means the first name binding operation for a variable in its scope, then I am not sure why that would be where an error message would be expected when using assert_never. Finally, this slightly adjusts the message: - "Literal" is decapitalized, to decrease confusion if the default message is used when matching on something not a typing.Literal. - The exception message prints the unexpected value's repr rather than its str, since the repr, when different from the str, is usually the representation more useful for debugging.
1 parent b2c3d8b commit f83b056

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

git/types.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,27 @@
7272

7373

7474
def assert_never(inp: NoReturn, raise_error: bool = True, exc: Union[Exception, None] = None) -> None:
75-
"""For use in exhaustive checking of literal or Enum in if/else chain.
75+
"""For use in exhaustive checking of a literal or enum in if/else chains.
7676
77-
Should only be reached if all members not handled OR attempt to pass non-members through chain.
77+
A call to this function should only be reached if not all members are handled, or if
78+
an attempt is made to pass non-members through the chain.
7879
79-
If all members handled, type is Empty. Otherwise, will cause mypy error.
80+
:param inp:
81+
If all members are handled, the argument for `inp` will have the
82+
:class:`~typing.Never`/:class:`~typing.NoReturn` type. Otherwise, the type will
83+
mismatch and cause a mypy error.
8084
81-
If non-members given, should cause mypy error at variable creation.
85+
:param raise_error:
86+
If ``True``, will also raise :class:`ValueError` with a general "unhandled
87+
literal" message, or the exception object passed as `exc`.
8288
83-
If raise_error is True, will also raise AssertionError or the Exception passed to exc.
89+
:param exc:
90+
It not ``None``, this should be an already-constructed exception object, to be
91+
raised if `raise_error` is ``True``.
8492
"""
8593
if raise_error:
8694
if exc is None:
87-
raise ValueError(f"An unhandled Literal ({inp}) in an if/else chain was found")
95+
raise ValueError(f"An unhandled literal ({inp!r}) in an if/else chain was found")
8896
else:
8997
raise exc
9098

0 commit comments

Comments
 (0)