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

tools, tools.calculation: docs/type-hint improvements, API fixes, better test coverage #2543

Merged
merged 6 commits into from
Nov 3, 2023

Conversation

dgw
Copy link
Member

@dgw dgw commented Nov 3, 2023

Description

This starts with some tweaks to the docstring data in tools (.. versionadded:: kinda stuff) and tools.calculation that I found sitting in a forgotten branch; apparently those smaller tweaks never reached the point of a full PR.

That stalled work's finally done: sopel/tools/calculation.py is now fully type-annotated and has 100% test coverage. Along the way I discovered a few API glitches (e.g. incorrectly documented parameter types, missing parameters) and fixed 'em.

I admit some of the test assertions are pretty ugly, but with only one custom exception type it's necessary to make sure that the right kind of ExpressionEvaluator.Error is being raised. A future refactoring opportunity, that is (which #2508 already tracks).

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

Notes

The framework for fully exercising tools.calculation in tests came from both #2530 and #2464 (comment) — thanks, @SnoopJ! Getting around to this would have taken a lot longer if you didn't kick-start it.

Just one thing I haven't cracked: Two new Sphinx warnings from type annotations that it can't resolve to cross-reference links:

/home/dgw/github/sopel-irc/sopel/sopel/tools/calculation.py:docstring of
sopel.tools.calculation.ExpressionEvaluator:1: WARNING: py:class reference
target not found: ast.operator
/home/dgw/github/sopel-irc/sopel/sopel/tools/calculation.py:docstring of
sopel.tools.calculation.ExpressionEvaluator:1: WARNING: py:class reference
target not found: ast.unaryop

Those are, however, the types required to satisfy mypy. Trying to use the CamelCase classes in ast's documentation (BinOp and UnaryOp) never passed type-checking. The class hierarchy there doesn't work like one might think it does (or should).

At the moment, I'm ready to give up on these like what eventually happened with the unfixed warnings left over in #2496. Really seems like Sphinx just can't handle certain things. Even the Python docs themselves reference these, and the rendered HTML shows them as xref py py-class but they're not linked.

dgw added 3 commits December 31, 2022 14:48
I forgot to put `.. deprecated:: 8.0` somewhere when deprecating the
`OutputRedirect` class, last time I took a swing at this module.
Sopelunked into the history and found when all this stuff was originally
added to the codebase. (Spoiler: A long time ago.)

There was no docstring at all for `EquationEvaluator`, so I wrote one.

Tweaked the docs page for `tools.calculation` to show everything instead
of only what's listed in `__all__`, and noted that most of the module is
considered internal machinery as we've been doing in `irc` etc.

NOTE: I would like the "public function" to appear at the top, but could
not find a nice way to do that in ~15 min of googling+experimenting.
Everything in this module can be considered "added" at the same time as
the module itself, in v5.3. Even going back that far crosses a rename
boundary (from `willie` to `sopel`) and it's likely a fool's errand to
try and write a plugin that functions equally well under Sopel 8.0-dev,
7.x, and everything else back to 4.x.

In the immortal words of Lisa Landry, "Let it go, Ray."

(But this "undo" operation is kept as a separate commit in case other
maintainers feel differently.)
@dgw dgw added Documentation Bugfix Generally, PRs that reference (and fix) one or more issue(s) Housekeeping Code cleanup, removal of deprecated stuff, etc. labels Nov 3, 2023
@dgw dgw added this to the 8.0.0 milestone Nov 3, 2023
@dgw dgw requested a review from a team November 3, 2023 06:57
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.

LGTM, some fine Sopelunking here! 100% coverage train, choo choo! 🚂💨

sopel/tools/calculation.py Outdated Show resolved Hide resolved
sopel/tools/calculation.py Outdated Show resolved Hide resolved
@dgw dgw force-pushed the tools.calculation-typing branch from bbaafc6 to 3f09032 Compare November 3, 2023 17:50
dgw and others added 3 commits November 3, 2023 12:52
Contrary to its parameter descriptions, `pow_complexity()` was NOT able
to handle `float` inputs. The `&` operator does not work with floats,
nor do floats have a `bit_length()` method.

`EquationEvaluator.__call__()` now takes an optional `timeout` argument
just like its upstream counterpart in `ExpressionEvaluator`.
Nasty assertions on exception args... Really need to refactor that silly
ExpressionEvaluator.Error type into some sensible PUBLIC hierarchy.
@dgw dgw force-pushed the tools.calculation-typing branch from 3f09032 to 3c4e477 Compare November 3, 2023 17:52
@dgw dgw merged commit 3b91a76 into master Nov 3, 2023
8 checks passed
@dgw dgw deleted the tools.calculation-typing branch November 3, 2023 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bugfix Generally, PRs that reference (and fix) one or more issue(s) Documentation Housekeeping Code cleanup, removal of deprecated stuff, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants