sopel-irc / sopel

:robot::speech_balloon: An easy-to-use and highly extensible IRC Bot framework. Formerly Willie.
https://sopel.chat
Other
950 stars 405 forks source link

mypy: fix currently known type-checking errors #2462

Closed dgw closed 1 year ago

dgw commented 1 year ago

Description

This resolves all known errors in mypy sopel output, as of commit time. There are few enough other outstanding PRs, I don't think introducing new errors after this is merged will be much of an issue.

(It would help if I started checking make mypy before merging things, to avoid introducing new errors after fixing this set of them… I'll try to remember that step.)

Checklist

SnoopJ commented 1 year ago

I'm seeing an additional failure on 3.7.16, looks like mypy surfaced a subtle bug in that version's error handling:

00:12 [snoopjedi@denton ~/repos/sopel-src (mypy-error-cleanup)]
$ python3 -V && mypy -V && make mypy
Python 3.7.16
mypy 0.991 (compiled: yes)
mypy sopel
sopel/plugins/capabilities.py:64: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
sopel/plugins/capabilities.py:70: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
sopel/plugins/capabilities.py:71: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
sopel/plugins/capabilities.py:72: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
sopel/irc/backends.py:316: error: Module has no attribute "exceptions"  [attr-defined]
sopel/irc/backends.py:319: error: Module has no attribute "exceptions"  [attr-defined]
sopel/plugins/rules.py:1065: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
sopel/plugins/rules.py:1066: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
sopel/plugins/rules.py:1263: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
sopel/plugins/rules.py:1265: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
sopel/bot.py:59: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
Found 2 errors in 1 file (checked 86 source files)
make: *** [Makefile:9: mypy] Error 1

Looks like between 3.7 and 3.8, these exceptions were moved into a separate submodule but are still available at the module root. If we change asyncio.exceptions.* to asyncio.* on the failing lines, this resolves the issue.

I do like the asyncio.execeptions spelling better, but adding ad-hoc version comparison code doesn't seem worth it here, especially as 3.7 approaches its EOL and a presumptive dropping of it from Sopel's support window.

clicky click for diff ```diff diff --git a/sopel/irc/backends.py b/sopel/irc/backends.py index 42a6781f..2a44c9cb 100644 --- a/sopel/irc/backends.py +++ b/sopel/irc/backends.py @@ -313,10 +313,10 @@ class AsyncioBackend(AbstractIRCBackend): while not self._reader.at_eof(): try: line: bytes = await self._reader.readuntil(separator=b'\r\n') - except asyncio.exceptions.IncompleteReadError as err: + except asyncio.IncompleteReadError as err: LOGGER.warning('Receiving partial message from IRC.') line = err.partial - except asyncio.exceptions.LimitOverrunError: + except asyncio.LimitOverrunError: LOGGER.exception('Unable to read from IRC server.') break ```

Modulo this one wrinkle, this passes type-checking on all versions. Nice one!

dgw commented 1 year ago

Even in 3.12 alpha docs, there's no hint of those exceptions being removed from the asyncio namespace, so I added a commit to fix it w/SnoopJ as co-author.

Looks like @SnoopJ's efforts to set up tox locally went well. Incoming contrib/ PR? 😁