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: add --disallow-incomplete-defs option #2616

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

Exirel
Copy link
Contributor

@Exirel Exirel commented Sep 1, 2024

Description

Tin, with the necessary type definition fix. Related to #2603 for Sopel 8.0.1.

Note: I used Any in several places, in particular in sopel.tools.identifiers. I'm not really happy about it. However, the best fix is to properly type annotate instances, i.e. doing something like users: dict[str, User] = SopelIdentifierMemory() as it should properly type check the users sopel memory object.

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 lint and make test)
  • I have tested the functionality of the things this change touches

@Exirel Exirel added the Housekeeping Code cleanup, removal of deprecated stuff, etc. label Sep 1, 2024
@Exirel Exirel added this to the 8.0.1 milestone Sep 1, 2024
@Exirel Exirel force-pushed the mypy-option-disallow-incomplete-defs branch from b71b791 to ed8f641 Compare October 12, 2024 11:23
@Exirel
Copy link
Contributor Author

Exirel commented Oct 12, 2024

I rebased that over master and it passes the 3.13 mypy check as well!

@Exirel Exirel requested a review from a team October 12, 2024 11:42
Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

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

It's a constant battle to keep the docs clean, with only the type annotations or hints in the :param: declarations, but not both.

While you go through and look for those (I didn't annotate every one, especially since many are outside where GH will allow line notes), also double check that merging #2285 didn't introduce any new wrinkles for this mypy option? 🙏

sopel/bot.py Show resolved Hide resolved
@dgw dgw mentioned this pull request Oct 14, 2024
4 tasks
@Exirel
Copy link
Contributor Author

Exirel commented Oct 14, 2024

but not both.

I fixed the functions I touched, and then fixed some for good measure!

double check that merging #2285 didn't introduce any new wrinkles for this mypy option? 🙏

That would require a rebase first, so you tell me if it's OK and I'll do.

Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

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

Lots of good stuff in here, but I do have a few questions (the same one in multiple places, actually) about the choice of type annotation syntax. Overall very impressed with how thorough you were with cleaning up the docstrings!

double check that merging #2285 didn't introduce any new wrinkles for this mypy option? 🙏

That would require a rebase first, so you tell me if it's OK and I'll do.

Now that I think about it, your fixups were tested against the latest master, including the merged changes from #2285. It passed, so you can just squash out the fixups after we settle the syntax question.

sopel/db.py Show resolved Hide resolved
sopel/plugins/rules.py Show resolved Hide resolved
sopel/tools/calculation.py Show resolved Hide resolved
sopel/tools/memories.py Outdated Show resolved Hide resolved
@Exirel Exirel force-pushed the mypy-option-disallow-incomplete-defs branch from bc7b2b5 to dcb9467 Compare October 14, 2024 19:57
Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

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

Cleaned history all good.

I'll have to check other open PRs against this after merging, or make them re-run CI somehow. That's a future-me problem, for later in the week. 😁

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.

Had some remarks, but if the type checker is happy then so am I!

@@ -1030,12 +1034,11 @@ def error(
self,
trigger: Optional[Trigger] = None,
exception: Optional[BaseException] = None,
):
) -> None:
"""Called internally when a plugin causes an error.

:param trigger: the ``Trigger``\\ing line (if available)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: could probably drop the reference to the type here? I would probably also omit the word "line"

Copy link
Member

Choose a reason for hiding this comment

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

Maybe this would work, but I wouldn't want to take too much context out of the docstring.

Suggested change
:param trigger: the ``Trigger``\\ing line (if available)
:param trigger: the ``Trigger`` that led to the error, if available

sopel/builtins/units.py Show resolved Hide resolved
@@ -199,3 +209,5 @@ def mass(bot, trigger):
stupid_part = '{:.2f} oz'.format(ounce)

bot.say('{} = {}'.format(metric_part, stupid_part))

return None
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here: I probably would have left off the explicit return

sopel/lifecycle.py Show resolved Hide resolved
@@ -13,21 +13,21 @@
import inspect
import logging
import traceback
from typing import Callable, Optional
from typing import Callable
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here: Is there a reason to prefer the deprecated typing.Callable over collections.abc.Callable?

Comment on lines +20 to +22
Callable,
Optional,
Sequence,
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason not to import these from collections.abc since the typing counterparts are deprecated since Python 3.9?

Copy link
Member

Choose a reason for hiding this comment

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

The only reason would be "not touching more than necessary", I think.

Switching from the deprecated imports to their replacements is unrelated to the PR's goal of making --disallow-incomplete-defs work.

@@ -26,6 +26,7 @@
import threading
from typing import (
Any,
Callable,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment here about collections.abc vs. typing

self,
expression_str: str,
timeout: float = 5.0,
) -> int | float:
Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC, int | float is redundant because float can annotate both per PEP 484

I'm okay with explicitly annotating it as a way to document the specific type that will be present but would personally prefer an alias for the union in that case, as a place to leave a comment about why we don't do it the "usual" way.

return super().__contains__(self._make_key(key))

def __setitem__(self, key: Optional[str], value):
def __setitem__(self, key: str | None, value: Any) -> Any:
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: that can be None

Suggested change
def __setitem__(self, key: str | None, value: Any) -> Any:
def __setitem__(self, key: str | None, value: Any) -> None:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Housekeeping Code cleanup, removal of deprecated stuff, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants