From d200959ab614c9de39e3e8262b26466a557812f2 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 26 Aug 2024 01:12:18 -0400 Subject: [PATCH] Fix Pyright errors in everything except wiki/copyvios --- pyproject.toml | 2 -- src/earwigbot/commands/cidr.py | 13 ++++++++----- src/earwigbot/commands/crypt.py | 1 + src/earwigbot/commands/notes.py | 2 +- src/earwigbot/commands/remind.py | 5 ++++- src/earwigbot/commands/stalk.py | 14 ++++++-------- src/earwigbot/commands/threads.py | 2 +- src/earwigbot/commands/trout.py | 2 +- src/earwigbot/tasks/__init__.py | 6 ++++-- src/earwigbot/tasks/wikiproject_tagger.py | 6 +++++- 10 files changed, 31 insertions(+), 22 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index caed9a0..10a3734 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -61,8 +61,6 @@ build-backend = "setuptools.build_meta" [tool.pyright] exclude = [ # TODO - "src/earwigbot/commands", - "src/earwigbot/tasks", "src/earwigbot/wiki/copyvios" ] pythonVersion = "3.11" diff --git a/src/earwigbot/commands/cidr.py b/src/earwigbot/commands/cidr.py index a6dfbee..a0e0d5e 100644 --- a/src/earwigbot/commands/cidr.py +++ b/src/earwigbot/commands/cidr.py @@ -62,8 +62,7 @@ class CIDR(Command): try: ips = [self._parse_ip(arg) for arg in data.args] except ValueError as exc: - msg = "Can't parse IP address \x0302{0}\x0f." - self.reply(data, msg.format(exc.message)) + self.reply(data, f"Can't parse IP address: \x0302{exc}\x0f") return if any(ip.family == AF_INET for ip in ips) and any( @@ -112,8 +111,9 @@ class CIDR(Command): return _IP(AF_INET6, socket.inet_pton(AF_INET6, arg), size) except OSError: raise ValueError(oldarg) - if size > 32: - raise ValueError(oldarg) + else: + if size and size > 32: + raise ValueError(oldarg) return ip def _parse_arg(self, arg): @@ -180,7 +180,10 @@ class CIDR(Command): """Convert an IP's binary representation to presentation format.""" return socket.inet_ntop( family, - "".join(chr(int(binary[i : i + 8], 2)) for i in range(0, len(binary), 8)), + b"".join( + chr(int(binary[i : i + 8], 2)).encode() + for i in range(0, len(binary), 8) + ), ) @staticmethod diff --git a/src/earwigbot/commands/crypt.py b/src/earwigbot/commands/crypt.py index 54d962d..1aeeff9 100644 --- a/src/earwigbot/commands/crypt.py +++ b/src/earwigbot/commands/crypt.py @@ -76,6 +76,7 @@ class Crypt(Command): data, "This command requires the 'cryptography' package: https://cryptography.io/", ) + return try: if data.command == "encrypt": diff --git a/src/earwigbot/commands/notes.py b/src/earwigbot/commands/notes.py index cd853df..1fa6b37 100644 --- a/src/earwigbot/commands/notes.py +++ b/src/earwigbot/commands/notes.py @@ -163,7 +163,7 @@ class Notes(Command): except IndexError: self.reply(data, "Please specify an entry to edit.") return - content = " ".join(data.args[2:]).strip().decode("utf8") + content = " ".join(data.args[2:]).strip() if not content: self.reply(data, "Please give some content to put in the entry.") return diff --git a/src/earwigbot/commands/remind.py b/src/earwigbot/commands/remind.py index d94cecf..d94b37d 100644 --- a/src/earwigbot/commands/remind.py +++ b/src/earwigbot/commands/remind.py @@ -22,8 +22,10 @@ import ast import operator import random import time +from collections.abc import Callable from itertools import chain from threading import RLock, Thread +from typing import Any from earwigbot.commands import Command from earwigbot.irc import Data @@ -69,11 +71,12 @@ class Remind(Command): return "snooze" if command in SNOOZE: # "adjust" == snoozing active reminders return "adjust" + raise ValueError(command) @staticmethod def _parse_time(arg): """Parse the wait time for a reminder.""" - ast_to_op = { + ast_to_op: dict[type[ast.operator], Callable[[Any, Any], Any]] = { ast.Add: operator.add, ast.Sub: operator.sub, ast.Mult: operator.mul, diff --git a/src/earwigbot/commands/stalk.py b/src/earwigbot/commands/stalk.py index 1bec40c..9d42cbd 100644 --- a/src/earwigbot/commands/stalk.py +++ b/src/earwigbot/commands/stalk.py @@ -126,6 +126,8 @@ class Stalk(Command): return else: stalkinfo = (data.nick, data.chan, modifiers) + else: + stalkinfo = None if data.command == "stalk": self._add_stalk("user", data, target, stalkinfo) @@ -330,10 +332,8 @@ class Stalk(Command): users = self._get_stalks_by_nick(nick, self._users) pages = self._get_stalks_by_nick(nick, self._pages) - if users: - uinfo = f" Users: {_format_stalks(users)}." - if pages: - pinfo = f" Pages: {_format_stalks(pages)}." + uinfo = f" Users: {_format_stalks(users)}." if users else None + pinfo = f" Pages: {_format_stalks(pages)}." if pages else None msg = "Currently stalking {0} user{1} and watching {2} page{3} for you.{4}{5}" return msg.format( @@ -368,10 +368,8 @@ class Stalk(Command): ) users, pages = self._users, self._pages - if users: - uinfo = f" Users: {_format_stalks(users)}." - if pages: - pinfo = f" Pages: {_format_stalks(pages)}." + uinfo = f" Users: {_format_stalks(users)}." if users else None + pinfo = f" Pages: {_format_stalks(pages)}." if pages else None msg = "Currently stalking {0} user{1} and watching {2} page{3}.{4}{5}" return msg.format( diff --git a/src/earwigbot/commands/threads.py b/src/earwigbot/commands/threads.py index e15d1ea..a324f54 100644 --- a/src/earwigbot/commands/threads.py +++ b/src/earwigbot/commands/threads.py @@ -69,7 +69,7 @@ class Threads(Command): for thread in threads: tname = thread.name - ident = thread.ident % 10000 + ident = thread.ident % 10000 if thread.ident else "unknown" if tname == "MainThread": t = "\x0302main\x0f (id {0})" normal_threads.append(t.format(ident)) diff --git a/src/earwigbot/commands/trout.py b/src/earwigbot/commands/trout.py index 216adb9..290bd96 100644 --- a/src/earwigbot/commands/trout.py +++ b/src/earwigbot/commands/trout.py @@ -40,7 +40,7 @@ class Trout(Command): target = " ".join(data.args) or data.nick target = "himself" if target == "yourself" else target - normal = normalize("NFKD", target.decode("utf8")).lower() + normal = normalize("NFKD", target).lower() if normal in self.exceptions: self.reply(data, self.exceptions[normal]) else: diff --git a/src/earwigbot/tasks/__init__.py b/src/earwigbot/tasks/__init__.py index a811ec8..b6a6d2c 100644 --- a/src/earwigbot/tasks/__init__.py +++ b/src/earwigbot/tasks/__init__.py @@ -1,4 +1,4 @@ -# Copyright (C) 2009-2015 Ben Kurtovic +# Copyright (C) 2009-2024 Ben Kurtovic # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -18,6 +18,8 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +from typing import Any + from earwigbot import exceptions __all__ = ["Task"] @@ -76,7 +78,7 @@ class Task: """ pass - def run(self, **kwargs): + def run(self, **kwargs: Any) -> None: """Main entry point to run a given task. This is called directly by :py:meth:`tasks.start() diff --git a/src/earwigbot/tasks/wikiproject_tagger.py b/src/earwigbot/tasks/wikiproject_tagger.py index c5c1ea3..c03811e 100644 --- a/src/earwigbot/tasks/wikiproject_tagger.py +++ b/src/earwigbot/tasks/wikiproject_tagger.py @@ -177,7 +177,9 @@ class WikiProjectTagger(Task): except IndexError: return text - def run(self, **kwargs: Unpack[JobKwargs]) -> None: + def run( # pyright: ignore[reportIncompatibleMethodOverride] + self, **kwargs: Unpack[JobKwargs] + ) -> None: """ Main entry point for the bot task. """ @@ -364,6 +366,7 @@ class WikiProjectTagger(Task): self.logger.error(f"Skipping invalid page: [[{page.title}]]") return + banner = None is_update = False for template in code.ifilter_templates(recursive=True): if template.name.matches(job.names): @@ -389,6 +392,7 @@ class WikiProjectTagger(Task): return if is_update: + assert banner is not None updated = self.update_banner(banner, job, code) if not updated: self.logger.info(