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

config.types, test: add BooleanAttribute setting type #2044

Merged
merged 3 commits into from
Apr 13, 2021

Conversation

dgw
Copy link
Member

@dgw dgw commented Mar 25, 2021

Description

Was going to move the helper functions used in ValidatedAttribute when parse == bool, but it would've required some ridiculous polymorphism. Better to just leave the old stuff—which will also make it easier to mark as deprecated.

Maybe we need to add tests that cover the default parameter to some or all of these setting types? I'll chew on that.

When complete, this will implement and close #2019.

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 dgw added the Feature label Mar 25, 2021
@dgw dgw added this to the 7.1.0 milestone Mar 25, 2021
@dgw dgw requested a review from a team March 25, 2021 04:31
@lgtm-com
Copy link

lgtm-com bot commented Mar 25, 2021

This pull request introduces 1 alert when merging 6fb5c0a into 4639e00 - view on LGTM.com

new alerts:

  • 1 for Missing call to `__init__` during object initialization

@dgw
Copy link
Member Author

dgw commented Mar 26, 2021

Simply could not think of a better approach to emitting deprecation warnings for ValidatedAttribute(..., parse=bool). Obviously decorating ValidatedAttribute.__init__() as @deprecated would cover a LOT more cases than just the Boolean ones. Decorating the helpers _parse_boolean() and _serialize_boolean() caused tons of spam in the terminal. 🤷‍♂️

@Exirel
Copy link
Contributor

Exirel commented Mar 26, 2021

Decorating the helpers _parse_boolean() and _serialize_boolean() caused tons of spam in the terminal. man_shrugging

Indeed. If you want to reduce the duplicated code, you can:

  1. put everything from the if bool block into a function/a method
  2. put the deprecation warning on that function/method
  3. call it in the if bool and nowhere else

The content of said function would be something like:

@deprecate(...)
def _get_deprecated_boolean_handlers(parser=None, serializer=None):
    # ... logic from that if bool block
    return the_boolean_parser, the_boolean_serializer

Problem solved.

@dgw
Copy link
Member Author

dgw commented Mar 26, 2021

Problem solved.

Not really. I mean technically, it deduplicates the code, but the warning printed is this:

Deprecated since 7.1, will be removed in 9.0: Use BooleanAttribute instead of ValidatedAttribute with parse=bool
  File "/mnt/c/Users/dgw/github/sopel/sopel/config/types.py", line 317, in __init__
    parse, serialize = _deprecated_special_bool_handling(serialize)

That doesn't tell the user or plugin developer where the outdated usage lies.

If tools.deprecated() had a kwarg to specify which stack frame to show, that might help, but it's way out of scope for this PR. That said, I already put together the bare bones of such a new kwarg; I'd just need to document it to make it PR-ready.

@Exirel
Copy link
Contributor

Exirel commented Mar 26, 2021

We can always add the field's name in the deprecation message, that should help a bit.

@dgw
Copy link
Member Author

dgw commented Mar 27, 2021

I'm not at all sure how you'd do that from a decorator on a helper function called from the object constructor, but if I can learn something from you again, I'm all eyes.

@Exirel
Copy link
Contributor

Exirel commented Mar 27, 2021

I'd do that within the constructor:

def __init__(self, name, ...):
    @deprecate("some message with the name %s" % name)
    def _get_deprecated_boolean_handlers(parser=None, serializer=None):
        # ... logic from that if bool block
        return the_boolean_parser, the_boolean_serializer

    if bool:
        parser, serialiazer = _get_deprecated_boolean_handlers()

Not perfect, but it works and it's temporary.

@dgw
Copy link
Member Author

dgw commented Mar 27, 2021

I guess defining stuff inside other defs is not unheard of in Sopel's code, e.g. the old signal handlers. But even that approach would benefit from the PR I'm about to open against tools.deprecated.

And don't worry, I did see your muttering on IRC about using warnings.warn(). That's something tools.deprecated could do instead of directly printing to stderr itself. I've also mused recently that that decorator should output to the logs along with everything else. Lots to think about for 8.0!

@Exirel
Copy link
Contributor

Exirel commented Mar 27, 2021

even that approach would benefit from the PR

Yup, and if that PR can be merged first, go for it. I think it would be the better option here.

@dgw
Copy link
Member Author

dgw commented Mar 28, 2021

Pushed using the new kwarg to tools.deprecated. If this doesn't pass CI I'll do a rebase later.

Might want to clean up history later anyway, tbf.

@lgtm-com
Copy link

lgtm-com bot commented Mar 28, 2021

This pull request introduces 1 alert when merging bed0471 into 96c55af - view on LGTM.com

new alerts:

  • 1 for Unused import

dgw and others added 3 commits April 10, 2021 20:51
Was going to move the helper functions used in `ValidatedAttribute` when
`parse == bool`, but it would've required some ridiculous polymorphism.

Maybe we need to add tests that cover the `default` parameter to some or
all of these setting types? I'll chew on that.
This will fire only once, when the setting object is initialized.
Exi provided the inspiration, and I made it work after adding a new
feature to Sopel's deprecation decorator. 🙃

Co-Authored-By: Exirel <florian.strzelecki@gmail.com>
@dgw dgw force-pushed the config-type-BooleanAttribute branch from 2a0298f to 220b3af Compare April 11, 2021 01:52
@dgw
Copy link
Member Author

dgw commented Apr 11, 2021

I cleaned up the history. @Exirel presumably will be the one to review this, as it uses his suggested approach.

Copy link
Contributor

@Exirel Exirel left a comment

Choose a reason for hiding this comment

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

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sopel should have a BooleanAttribute config type
2 participants