Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mypy: fix currently known type-checking errors #2462

Merged
merged 6 commits into from
Jun 11, 2023
Merged

Conversation

dgw
Copy link
Member

@dgw dgw commented Jun 4, 2023

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

  • I have read CONTRIBUTING.md
  • I can and do license this contribution under the EFLv2
  • No issues are reported by make qa (runs make quality and make test)
  • I have tested the functionality of the things this change touches

dgw added 4 commits June 2, 2023 03:46
Resolving type-checking in `bot` led to new errors, because `Rule` had
some methods & properties related to rate-limiting that weren't present
on the parent `AbstractRule` class, which defines the interface.
@dgw dgw added this to the 8.0.0 milestone Jun 4, 2023
@dgw dgw requested a review from a team June 4, 2023 17:06
@dgw dgw changed the title plugins.rules: fix mypy check errors with optional datetimes and </> mypy: fix currently known type-checking errors Jun 4, 2023
@SnoopJ
Copy link
Contributor

SnoopJ commented Jun 8, 2023

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 --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!

The documentation goes straight to listing the exception classes as
direct members of `asyncio`, not `asyncio.exceptions`
(https://docs.python.org/3/library/asyncio-exceptions.html)

Fixes an error flagged by mypy under Python 3.7, not that we need to
worry about 3.7 for much longer.

Co-authored-by: SnoopJ <snoopjedi@gmail.com>
@dgw
Copy link
Member Author

dgw commented Jun 8, 2023

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? 😁

Copy link
Contributor

@SnoopJ SnoopJ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 ✔️

sopel/plugins/rules.py Outdated Show resolved Hide resolved
@SnoopJ SnoopJ mentioned this pull request Jun 9, 2023
4 tasks
Together, we found the balance between brevity + clarity, and also
eliminated a local variable as a bonus.

Co-authored-by: Exirel <florian.strzelecki@gmail.com>
Co-authored-by: SnoopJ <snoopjedi@gmail.com>
Copy link
Contributor

@SnoopJ SnoopJ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No further remarks, nice one!

@dgw dgw merged commit 488071b into master Jun 11, 2023
@dgw dgw deleted the mypy-error-cleanup branch June 11, 2023 05:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants