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

privileges, url: fix privilege check in .urlban command family #2650

Merged
merged 2 commits into from
Jan 29, 2025

Conversation

dgw
Copy link
Member

@dgw dgw commented Jan 27, 2025

Description

This is a possible resolution for #2649.

Maybe there's a better way to do this—maybe it would be better to just use AccessLevel._member_names_ directly—but suggesting an alternative approach when appropriate is what code review's for, right?

The commits are separated out so it'll be easy to bring whatever is useful (especially the new test case) over to a new approach if someone has a better idea.

Checklist

An alternative of my own

Defining these shortcut constants from sopel.plugin (below) in sopel.privileges instead could be useful:

sopel/sopel/plugin.py

Lines 26 to 34 in 99c4a00

# import and expose privileges as shortcut
from sopel.privileges import AccessLevel
VOICE = AccessLevel.VOICE
HALFOP = AccessLevel.HALFOP
OP = AccessLevel.OP
ADMIN = AccessLevel.ADMIN
OWNER = AccessLevel.OWNER
OPER = AccessLevel.OPER

I do wonder if we should in fact make sopel.privileges.__all__ export the list of privilege names, and keep AccessLevel as more of an internal thing. It would remove any need for a weird module-level constant (sopel.privileges.names), and simplify adding the aliases back to sopel.plugin (to one line: from sopel.privileges import *).

@dgw dgw added the Bugfix Generally, PRs that reference (and fix) one or more issue(s) label Jan 27, 2025
@dgw dgw added this to the 8.0.2 milestone Jan 27, 2025
@dgw dgw requested a review from a team January 27, 2025 00:00
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.

Nice patch!

test/builtins/test_builtins_url.py Show resolved Hide resolved
sopel/privileges.py Outdated Show resolved Hide resolved
@SnoopJ
Copy link
Contributor

SnoopJ commented Jan 27, 2025

Defining these shortcut constants from sopel.plugin (below) in sopel.privileges instead could be useful:

I think it would be better to add them to __all__ so that updates to the Enum automagically end up in the module namespace, but yet another alternative would be to make sopel.privileges into the enum class rather than the module where it is defined. That is probably too controversial in terms of public API though.

@dgw
Copy link
Member Author

dgw commented Jan 27, 2025

That is probably too controversial in terms of public API though.

Yeah it is now. Before 8.0 released, it was still changeable. Now we have to keep it this way until at least 9.0.

What I don't like about making sopel.privileges itself the enum is that nothing else* lives directly in the first-child level of hierarchy like that. It's sopel.bot.Sopel not sopel.bot, sopel.trigger.Trigger not sopel.trigger, etc.

Is there a better way to organize things such that the enum can become sopel._something_.privileges? Probably yes, and if it bothers you enough you should make an issue for 8.1 with other ideas. 😁 (If nothing else feels "righter", we can still have a fallback in that issue to reorganize the shortcuts/__all__ as already suggested, since that doesn't belong in a bugfix.)


Yes I know about sopel.version_info. But metadata about the package itself is about the only thing that makes sense in the root 🤷‍♂️

@Exirel
Copy link
Contributor

Exirel commented Jan 27, 2025

It would remove any need for a weird module-level constant (sopel.privileges.names), and simplify adding the aliases back to sopel.plugin (to one line: from sopel.privileges import *).

I really dislike the usage of the wildcard * import in Python. It's a code smell that is used in the most foolish ways.

Beside, no ones need names when you can do [e.name for e in AccessLevel] which gives the same thing, and remove the need to expose names in sopel.privileges.

sopel/builtins/url.py Outdated Show resolved Hide resolved
dgw and others added 2 commits January 27, 2025 08:49
The change from constants defined at `sopel.privileges` module level to
an enum wasn't *entirely* seamless: `sopel.privileges.__all__` no longer
contains the list of privilege level names.

Credit where credit is due: Exirel reminded me that list comprehensions
are a thing that works for this type of case.

Co-authored-by: Exirel <[email protected]>
We would have noticed sooner that `.urlban` was broken if it had test
coverage. Now it does.
@dgw dgw force-pushed the privilege-names branch from c178f45 to 04ea15c Compare January 27, 2025 14:52
@dgw
Copy link
Member Author

dgw commented Jan 27, 2025

Beside, no ones need names when you can do [e.name for e in AccessLevel] which gives the same thing, and remove the need to expose names in sopel.privileges.

Good point. I knew it was a good idea to keep each module's changes in a separate commit. Very easy to just drop the new attr in sopel.privileges and amend the patch to url, keeping the test unchanged 💪

@dgw dgw requested a review from Exirel January 27, 2025 14:54
@dgw dgw merged commit 834930e into master Jan 29, 2025
17 checks passed
@dgw dgw deleted the privilege-names branch January 29, 2025 20:32
dgw added a commit that referenced this pull request Jan 29, 2025
privileges, url: fix privilege check in `.urlban` command family
@dgw dgw linked an issue Jan 30, 2025 that may be closed by this pull request
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)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

url: .urlban privilege check is broken
3 participants