aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-05-06 22:46:07 -0700
committerGravatar MarkKoz <[email protected]>2020-06-13 11:21:06 -0700
commit808fe261cb0163fe5759da36e36418fc392cb846 (patch)
tree68d4557289303269c94b570a3212f7e82c59c9be
parentCode block: add trace logging (diff)
Code block: fix valid code block being parsed as a missing block
`find_code_blocks` was returning an empty tuple if there was at least one valid code block. However, the caller could not distinguish between that case and simply no code blocks being found. Therefore, None is explicitly returned to distinguish it from a lack of results.
-rw-r--r--bot/cogs/codeblock/cog.py3
-rw-r--r--bot/cogs/codeblock/parsing.py12
2 files changed, 9 insertions, 6 deletions
diff --git a/bot/cogs/codeblock/cog.py b/bot/cogs/codeblock/cog.py
index e4b87938d..15dffce7a 100644
--- a/bot/cogs/codeblock/cog.py
+++ b/bot/cogs/codeblock/cog.py
@@ -106,6 +106,9 @@ class CodeBlockCog(Cog, name="Code Block"):
return
blocks = parsing.find_code_blocks(msg.content)
+ if blocks is None:
+ # None is returned when there's at least one valid block with a language.
+ return
if not blocks:
log.trace(f"No code blocks were found in message {msg.id}.")
description = instructions.get_no_ticks_message(msg.content)
diff --git a/bot/cogs/codeblock/parsing.py b/bot/cogs/codeblock/parsing.py
index 7409653d7..055c21118 100644
--- a/bot/cogs/codeblock/parsing.py
+++ b/bot/cogs/codeblock/parsing.py
@@ -1,7 +1,7 @@
import ast
import logging
import re
-from typing import NamedTuple, Sequence
+from typing import NamedTuple, Optional, Sequence
log = logging.getLogger(__name__)
@@ -41,15 +41,15 @@ class CodeBlock(NamedTuple):
tick: str
-def find_code_blocks(message: str) -> Sequence[CodeBlock]:
+def find_code_blocks(message: str) -> Optional[Sequence[CodeBlock]]:
"""
Find and return all Markdown code blocks in the `message`.
Code blocks with 3 or less lines are excluded.
If the `message` contains at least one code block with valid ticks and a specified language,
- return an empty sequence. This is based on the assumption that if the user managed to get
- one code block right, they already know how to fix the rest themselves.
+ return None. This is based on the assumption that if the user managed to get one code block
+ right, they already know how to fix the rest themselves.
"""
log.trace("Finding all code blocks in a message.")
@@ -60,8 +60,8 @@ def find_code_blocks(message: str) -> Sequence[CodeBlock]:
language = groups["lang"].strip() # Strip the newline cause it's included in the group.
if groups["tick"] == BACKTICK and language:
- log.trace("Message has a valid code block with a language; returning empty tuple.")
- return ()
+ log.trace("Message has a valid code block with a language; returning None.")
+ return None
elif len(groups["code"].split("\n", 3)) > 3:
code_block = CodeBlock(groups["code"], language, groups["tick"])
code_blocks.append(code_block)