-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nathaniel May
committed
Oct 26, 2021
1 parent
c019a94
commit 5c9fd07
Showing
3 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
from typing import NamedTuple, NoReturn, Union | ||
|
||
|
||
# common trick for getting mypy to do exhaustiveness checks | ||
# will come up with something like `"assert_never" has incompatible type` | ||
# if something is missing. | ||
def assert_never(x: NoReturn) -> NoReturn: | ||
raise AssertionError("Unhandled type: {}".format(type(x).__name__)) | ||
|
||
# The following classes represent the data necessary to describe a | ||
# particular event to both human readable logs, and machine reliable | ||
# event streams. The transformation to these forms will live in outside | ||
# functions. | ||
# | ||
# Until we drop support for Python 3.6 we must use NamedTuples over | ||
# frozen dataclasses. | ||
|
||
# TODO dummy class | ||
class OK(NamedTuple): | ||
result: int | ||
|
||
|
||
# TODO dummy class | ||
class Failure(NamedTuple): | ||
msg: str | ||
|
||
|
||
# using a union instead of inheritance means that this set cannot | ||
# be extended outside this file, and thus mypy can do exhaustiveness | ||
# checks for us. | ||
Event = Union[OK, Failure] | ||
|
||
|
||
# function that translates any instance of the above event types | ||
# into its human-readable message. | ||
# | ||
# This could instead be implemented as a method on an ABC for all | ||
# above classes, but this at least puts all that logic in one place. | ||
def humanMsg(r: Event) -> str: | ||
if isinstance(r, OK): | ||
return str(r.result) | ||
elif isinstance(r, Failure): | ||
return "Failure: " + r.msg | ||
else: | ||
assert_never(r) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters