trivia_quiz: Remove Python < 3.4 compatibility code.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-11-01 19:56:05 -07:00
parent 8b3c50206c
commit 158480fe2c
2 changed files with 3 additions and 28 deletions

View file

@ -1,4 +1,3 @@
import html
import json
from typing import Any, Dict, Optional, Tuple
from unittest.mock import patch
@ -6,7 +5,6 @@ from unittest.mock import patch
from typing_extensions import override
from zulip_bots.bots.trivia_quiz.trivia_quiz import (
fix_quotes,
get_quiz_from_id,
get_quiz_from_payload,
handle_answer,
@ -57,17 +55,6 @@ class TestTriviaQuizBot(BotTestCase, DefaultTests):
with mock_request_exception():
self.verify_reply("new", "Uh-Oh! Trivia service is down.")
def test_fix_quotes(self) -> None:
self.assertEqual(fix_quotes("test &amp; test"), html.unescape("test &amp; test"))
print("f")
with patch("html.unescape") as mock_html_unescape:
mock_html_unescape.side_effect = Exception
with self.assertRaises(Exception) as exception:
fix_quotes("test")
self.assertEqual(
str(exception.exception), "Please use python3.4 or later for this bot."
)
def test_invalid_answer(self) -> None:
invalid_replies = ["answer A", "answer A Q10", "answer Q001 K", "answer 001 A"]
for reply in invalid_replies:

View file

@ -2,7 +2,7 @@ import html
import json
import random
import re
from typing import Any, Dict, Optional, Tuple
from typing import Any, Dict, Tuple
import requests
@ -107,18 +107,6 @@ def get_trivia_payload() -> Dict[str, Any]:
return payload
def fix_quotes(s: str) -> Optional[str]:
# opentdb is nice enough to escape HTML for us, but
# we are sending this to code that does that already :)
#
# Meanwhile Python took until version 3.4 to have a
# simple html.unescape function.
try:
return html.unescape(s)
except Exception:
raise Exception("Please use python3.4 or later for this bot.")
def get_quiz_from_payload(payload: Dict[str, Any]) -> Dict[str, Any]:
result = payload["results"][0]
question = result["question"]
@ -129,9 +117,9 @@ def get_quiz_from_payload(payload: Dict[str, Any]) -> Dict[str, Any]:
answers[correct_letter] = result["correct_answer"]
for i in range(3):
answers[letters[i + 1]] = result["incorrect_answers"][i]
answers = {letter: fix_quotes(answer) for letter, answer in answers.items()}
answers = {letter: html.unescape(answer) for letter, answer in answers.items()}
quiz: Dict[str, Any] = dict(
question=fix_quotes(question),
question=html.unescape(question),
answers=answers,
answered_options=[],
pending=True,