Skip to content

Commit

Permalink
ref(bot): use exhaustiveness checking via NoReturn type (#614)
Browse files Browse the repository at this point in the history
`NoReturn` is Python's `never` which we can use to help ensure we handle
all cases.

I tried looking for other places where we could use this, but didn't
really find any easy places to add it.

Also we need to use `is` when comparing enum variants due to mypy's
backwards compatible refinement outlined in: python/mypy#6366
  • Loading branch information
sbdchd authored Feb 13, 2021
1 parent 232f141 commit 4d4c2a3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 3 additions & 1 deletion bot/.pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ disable=print-statement,
# doesn't handle .pyi files
useless-import-alias,
# conflicts with type stubs for structlog
keyword-arg-before-vararg
keyword-arg-before-vararg,
# not smart enough about refinement
inconsistent-return-statements,


# Enable the message, report, category or checker with the given id(s). You can
Expand Down
9 changes: 9 additions & 0 deletions bot/kodiak/assertions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import NoReturn


def assert_never(value: NoReturn) -> NoReturn:
"""
Enable exhaustiveness checking when comparing against enums and unions
of literals.
"""
raise Exception(f"expected never, got {value}")
9 changes: 5 additions & 4 deletions bot/kodiak/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing_extensions import Protocol

from kodiak import app_config, config, messages
from kodiak.assertions import assert_never
from kodiak.config import (
DEFAULT_TITLE_REGEX,
UNSET_TITLE_REGEX,
Expand Down Expand Up @@ -74,7 +75,7 @@ def get_body_content(
cut_body_after: str,
pull_request: PullRequest,
) -> str:
if body_type == BodyText.markdown:
if body_type is BodyText.markdown:
body = pull_request.body
if cut_body_before != "":
start_index = body.find(cut_body_before)
Expand All @@ -87,11 +88,11 @@ def get_body_content(
if strip_html_comments:
return strip_html_comments_from_markdown(body)
return body
if body_type == BodyText.plain_text:
if body_type is BodyText.plain_text:
return pull_request.bodyText
if body_type == BodyText.html:
if body_type is BodyText.html:
return pull_request.bodyHTML
raise Exception(f"Unknown body_type: {body_type}")
assert_never(body_type)


EMPTY_STRING = ""
Expand Down

0 comments on commit 4d4c2a3

Please sign in to comment.