From 7f3be5d9317aaadcf2fd740e43ed0a2c4e799afa Mon Sep 17 00:00:00 2001 From: linuxdaemon Date: Tue, 12 Mar 2024 23:54:19 +0000 Subject: [PATCH 1/3] Handle missing mode info for a mode --- cloudbot/util/irc.py | 10 +++++++-- tests/core_tests/util_tests/test_irc.py | 28 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tests/core_tests/util_tests/test_irc.py diff --git a/cloudbot/util/irc.py b/cloudbot/util/irc.py index 0de64a719..ddc4e50d7 100644 --- a/cloudbot/util/irc.py +++ b/cloudbot/util/irc.py @@ -45,8 +45,11 @@ class ModeChange: info = attr.ib(type=ChannelMode) @property - def is_status(self): - return self.info.type is ModeType.Status + def is_status(self) -> bool: + if not self.info: + return False + + return self.info.type == ModeType.Status @attr.s(hash=True) @@ -78,6 +81,9 @@ def parse_mode_string( adding = False else: mode_info = server_modes.get(c) + if not mode_info: + logger.warning("No ModeInfo found for %s", c) + if mode_info and mode_info.has_param(adding): param = params.pop(0) else: diff --git a/tests/core_tests/util_tests/test_irc.py b/tests/core_tests/util_tests/test_irc.py new file mode 100644 index 000000000..7ca06bcb7 --- /dev/null +++ b/tests/core_tests/util_tests/test_irc.py @@ -0,0 +1,28 @@ +from cloudbot.util import irc + + +def test_mode_parse(): + mode_info = { + "a": irc.ChannelMode(character="a", type=irc.ModeType.A), + "d": irc.StatusMode.make("^", "d", 3), + } + parsed = irc.parse_mode_string("+ad", ["foo", "bar"], mode_info) + + assert parsed == [ + irc.ModeChange("a", True, "foo", mode_info["a"]), + irc.ModeChange("d", True, "bar", mode_info["d"]), + ] + + +def test_mode_parse_missing_mode(): + mode_info = { + "a": irc.ChannelMode(character="a", type=irc.ModeType.A), + "d": irc.StatusMode.make("^", "d", 3), + } + parsed = irc.parse_mode_string("+adx", ["foo", "bar"], mode_info) + + assert parsed == [ + irc.ModeChange("a", True, "foo", mode_info["a"]), + irc.ModeChange("d", True, "bar", mode_info["d"]), + irc.ModeChange("x", True, None, None), + ] From 16687c8f9ceaed75e4206797f8211c3ae055cca0 Mon Sep 17 00:00:00 2001 From: linuxdaemon Date: Wed, 13 Mar 2024 00:39:51 +0000 Subject: [PATCH 2/3] Test status attributes --- tests/core_tests/util_tests/test_irc.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/core_tests/util_tests/test_irc.py b/tests/core_tests/util_tests/test_irc.py index 7ca06bcb7..1e51156b2 100644 --- a/tests/core_tests/util_tests/test_irc.py +++ b/tests/core_tests/util_tests/test_irc.py @@ -26,3 +26,5 @@ def test_mode_parse_missing_mode(): irc.ModeChange("d", True, "bar", mode_info["d"]), irc.ModeChange("x", True, None, None), ] + + assert [m.is_status for m in parsed] == [True, False, False] From a54946c568264e3a17d4497921c274028ab50cce Mon Sep 17 00:00:00 2001 From: linuxdaemon Date: Wed, 13 Mar 2024 22:30:02 +0000 Subject: [PATCH 3/3] Fix types --- plugins/cryptocurrency.py | 3 ++- plugins/minecraft_ping.py | 4 ++-- requirements-dev.txt | 3 ++- tests/core_tests/util_tests/test_irc.py | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/plugins/cryptocurrency.py b/plugins/cryptocurrency.py index 3655cf8a2..a6e48a806 100644 --- a/plugins/cryptocurrency.py +++ b/plugins/cryptocurrency.py @@ -9,6 +9,7 @@ License: GPL v3 """ + import inspect import time import warnings @@ -660,7 +661,7 @@ def crypto_command(text, event): def format_price(price: Union[int, float, Real]) -> str: price = float(price) if price < 1: - precision = max(2, min(10, -Decimal(str(price)).as_tuple().exponent)) + precision = max(2, min(10, len(str(Decimal(str(price)))) - 2)) num_format = "{:01,.{}f}".format(price, precision) else: num_format = "{:,.2f}".format(price) diff --git a/plugins/minecraft_ping.py b/plugins/minecraft_ping.py index 1a958bb19..38d3fc6bc 100644 --- a/plugins/minecraft_ping.py +++ b/plugins/minecraft_ping.py @@ -2,7 +2,7 @@ # TODO(linuxdaemon): Implement bedrock support from mcstatus import JavaServer as MinecraftServer -from mcstatus.pinger import PingResponse +from mcstatus.status_response import JavaStatusResponse from cloudbot import hook from cloudbot.util import colors @@ -48,7 +48,7 @@ def mcping(text): return str(e) try: - s = server.status() # type: PingResponse + s: JavaStatusResponse = server.status() except socket.gaierror: return "Invalid hostname" except socket.timeout: diff --git a/requirements-dev.txt b/requirements-dev.txt index 48df72dee..e5751cc41 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,6 +1,6 @@ darker[flynt,isort] == 1.6.1 freezegun == 1.1.0 -mypy == 0.910 +mypy == 1.0.0 pre-commit == 3.3.3 pylint == 3.1.0 pytest == 6.2.5 @@ -8,6 +8,7 @@ pytest-asyncio == 0.20.3 pytest-cov == 4.1.0 pytest-random-order == 1.0.4 responses == 0.25.0 +typing_extensions == 4.10.0 types-requests == 2.27.7 types-setuptools == 57.4.7 types-six == 1.16.2 diff --git a/tests/core_tests/util_tests/test_irc.py b/tests/core_tests/util_tests/test_irc.py index 1e51156b2..36a3f9ccc 100644 --- a/tests/core_tests/util_tests/test_irc.py +++ b/tests/core_tests/util_tests/test_irc.py @@ -27,4 +27,4 @@ def test_mode_parse_missing_mode(): irc.ModeChange("x", True, None, None), ] - assert [m.is_status for m in parsed] == [True, False, False] + assert [m.is_status for m in parsed] == [False, True, False]