Reduce config calls when changing white/blacklist and use sets for their cache #3910

Merged
Drapersniper merged 9 commits from improve-config-use-in-cache into V3/develop 2020-07-08 19:25:15 +02:00
Drapersniper commented 2020-06-04 13:32:12 +02:00 (Migrated from github.com)

Type

  • Bugfix
  • Enhancement
  • New feature

Description of the changes

### Type - [ ] Bugfix - [x] Enhancement - [ ] New feature ### Description of the changes
tekulvw (Migrated from github.com) reviewed 2020-06-04 13:32:12 +02:00
Jackenmen (Migrated from github.com) reviewed 2020-07-06 18:08:46 +02:00
Jackenmen (Migrated from github.com) left a comment

This seems okay, but wouldn't it be better to just set the whitelist/blacklist in config to self._cached_whitelist[gid] after iterating through all IDs in role_or_user?

This seems okay, but wouldn't it be better to just set the whitelist/blacklist in config to `self._cached_whitelist[gid]` *after* iterating through all IDs in `role_or_user`?
Jackenmen (Migrated from github.com) requested changes 2020-07-08 15:50:09 +02:00
Jackenmen (Migrated from github.com) left a comment

Despite the amount of review comments, there are just few issues here and the comment count is high only because they just occur in multiple places.

Despite the amount of review comments, there are just few issues here and the comment count is high only because they just occur in multiple places.
@ -122,11 +122,11 @@ class IgnoreManager:
class WhitelistBlacklistManager:
Jackenmen (Migrated from github.com) commented 2020-07-08 15:18:05 +02:00

Wrong type hint (also, look at the other comment below)

    async def add_to_whitelist(self, guild: Optional[discord.Guild], role_or_user: Iterable[int]):
Wrong type hint (also, look at the [other comment](https://github.com/Cog-Creators/Red-DiscordBot/pull/3910/files#r451548486) below) ```suggestion async def add_to_whitelist(self, guild: Optional[discord.Guild], role_or_user: Iterable[int]): ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:23:32 +02:00

This is not particularly important, but you could just use set's .update() method directly instead of iterating and adding one by one. I'm guessing this probably also has some benefits when role_or_user is a set.

self._cached_whitelist[gid].update(role_or_user)
This is not particularly important, but you could just use set's `.update()` method directly instead of iterating and adding one by one. I'm guessing this probably also has some benefits when `role_or_user` is a `set`. ```py self._cached_whitelist[gid].update(role_or_user) ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:24:35 +02:00

Wrong type hint (also, look at the other comment below)

        self, guild: Optional[discord.Guild], role_or_user: Iterable[int]
Wrong type hint (also, look at the [other comment](https://github.com/Cog-Creators/Red-DiscordBot/pull/3910/files#r451548946) below) ```suggestion self, guild: Optional[discord.Guild], role_or_user: Iterable[int] ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:26:20 +02:00

This is not particularly important, but you could just use set's .difference_update() method directly instead of iterating and discarding one by one. I'm guessing this probably also has some benefits when role_or_user is a set.

self._cached_whitelist[gid].difference_update(role_or_user)
This is not particularly important, but you could just use set's `.difference_update()` method directly instead of iterating and discarding one by one. I'm guessing this probably also has some benefits when `role_or_user` is a `set`. ```py self._cached_whitelist[gid].difference_update(role_or_user) ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:26:36 +02:00

This is not particularly important, but you could just use set's .update() method directly instead of iterating and adding one by one. I'm guessing this probably also has some benefits when role_or_user is a set.

self._cached_whitelist[gid].update(role_or_user)
This is not particularly important, but you could just use set's `.update()` method directly instead of iterating and adding one by one. I'm guessing this probably also has some benefits when `role_or_user` is a `set`. ```py self._cached_whitelist[gid].update(role_or_user) ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:27:09 +02:00

This is not particularly important, but you could just use set's .difference_update() method directly instead of iterating and discarding one by one. I'm guessing this probably also has some benefits when role_or_user is a set.

self._cached_whitelist[gid].difference_update(role_or_user)
This is not particularly important, but you could just use set's `.difference_update()` method directly instead of iterating and discarding one by one. I'm guessing this probably also has some benefits when `role_or_user` is a `set`. ```py self._cached_whitelist[gid].difference_update(role_or_user) ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:27:55 +02:00

Wrong type hint (also, look at the other comment below)

    async def add_to_blacklist(self, guild: Optional[discord.Guild], role_or_user: Iterable[int]):
Wrong type hint (also, look at the [other comment](https://github.com/Cog-Creators/Red-DiscordBot/pull/3910/files#r451549196) below) ```suggestion async def add_to_blacklist(self, guild: Optional[discord.Guild], role_or_user: Iterable[int]): ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:28:58 +02:00

This is not particularly important, but you could just use set's .update() method directly instead of iterating and adding one by one. I'm guessing this probably also has some benefits when role_or_user is a set.

self._cached_blacklist[gid].update(role_or_user)
This is not particularly important, but you could just use set's `.update()` method directly instead of iterating and adding one by one. I'm guessing this probably also has some benefits when `role_or_user` is a `set`. ```py self._cached_blacklist[gid].update(role_or_user) ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:29:07 +02:00

This is not particularly important, but you could just use set's .update() method directly instead of iterating and adding one by one. I'm guessing this probably also has some benefits when role_or_user is a set.

self._cached_blacklist[gid].update(role_or_user)
This is not particularly important, but you could just use set's `.update()` method directly instead of iterating and adding one by one. I'm guessing this probably also has some benefits when `role_or_user` is a `set`. ```py self._cached_blacklist[gid].update(role_or_user) ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:30:03 +02:00

Wrong type hint (also, look at the other comment below)

        self, guild: Optional[discord.Guild], role_or_user: Iterable[int]
Wrong type hint (also, look at the [other comment](https://github.com/Cog-Creators/Red-DiscordBot/pull/3910/files#r451549368) below) ```suggestion self, guild: Optional[discord.Guild], role_or_user: Iterable[int] ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:30:35 +02:00

This is not particularly important, but you could just use set's .difference_update() method directly instead of iterating and discarding one by one. I'm guessing this probably also has some benefits when role_or_user is a set.

self._cached_blacklist[gid].difference_update(role_or_user)
This is not particularly important, but you could just use set's `.difference_update()` method directly instead of iterating and discarding one by one. I'm guessing this probably also has some benefits when `role_or_user` is a `set`. ```py self._cached_blacklist[gid].difference_update(role_or_user) ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:30:46 +02:00

This is not particularly important, but you could just use set's .difference_update() method directly instead of iterating and discarding one by one. I'm guessing this probably also has some benefits when role_or_user is a set.

self._cached_blacklist[gid].difference_update(role_or_user)
This is not particularly important, but you could just use set's `.difference_update()` method directly instead of iterating and discarding one by one. I'm guessing this probably also has some benefits when `role_or_user` is a `set`. ```py self._cached_blacklist[gid].difference_update(role_or_user) ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:35:04 +02:00

There's some conflict here - type hint says Iterable (well, Iterator but that's not the point) but you're checking if it's an instance of list or set here. I'm guessing we can just make it allow any iterable as your type hint suggests. And since we're iterating right in this if, we can just drop the first isinstance check as the TypeError will be raised for non-iterable anyway.

        if not all(isinstance(r_or_u, int) for r_or_u in role_or_user):

Remember to also update the message in raised TypeError to reflect this change.

There's some conflict here - type hint says `Iterable` (well, `Iterator` but that's not the point) but you're checking if it's an instance of `list` or `set` here. I'm guessing we can just make it allow any iterable as your type hint suggests. And since we're iterating right in this `if`, we can just drop the first `isinstance` check as the `TypeError` will be raised for non-iterable anyway. ```suggestion if not all(isinstance(r_or_u, int) for r_or_u in role_or_user): ``` Remember to also update the message in raised `TypeError` to reflect this change.
Jackenmen (Migrated from github.com) commented 2020-07-08 15:35:44 +02:00

There's some conflict here - type hint says Iterable (well, Iterator but that's not the point) but you're checking if it's an instance of list or set here. I'm guessing we can just make it allow any iterable as your type hint suggests. And since we're iterating right in this if, we can just drop the first isinstance check as the TypeError will be raised for non-iterable anyway.

        if not all(isinstance(r_or_u, int) for r_or_u in role_or_user):

Remember to also update the message in raised TypeError to reflect this change.

There's some conflict here - type hint says `Iterable` (well, `Iterator` but that's not the point) but you're checking if it's an instance of `list` or `set` here. I'm guessing we can just make it allow any iterable as your type hint suggests. And since we're iterating right in this `if`, we can just drop the first `isinstance` check as the `TypeError` will be raised for non-iterable anyway. ```suggestion if not all(isinstance(r_or_u, int) for r_or_u in role_or_user): ``` Remember to also update the message in raised `TypeError` to reflect this change.
Jackenmen (Migrated from github.com) commented 2020-07-08 15:36:06 +02:00

There's some conflict here - type hint says Iterable (well, Iterator but that's not the point) but you're checking if it's an instance of list or set here. I'm guessing we can just make it allow any iterable as your type hint suggests. And since we're iterating right in this if, we can just drop the first isinstance check as the TypeError will be raised for non-iterable anyway.

        if not all(isinstance(r_or_u, int) for r_or_u in role_or_user):

Remember to also update the message in raised TypeError to reflect this change.

There's some conflict here - type hint says `Iterable` (well, `Iterator` but that's not the point) but you're checking if it's an instance of `list` or `set` here. I'm guessing we can just make it allow any iterable as your type hint suggests. And since we're iterating right in this `if`, we can just drop the first `isinstance` check as the `TypeError` will be raised for non-iterable anyway. ```suggestion if not all(isinstance(r_or_u, int) for r_or_u in role_or_user): ``` Remember to also update the message in raised `TypeError` to reflect this change.
Jackenmen (Migrated from github.com) commented 2020-07-08 15:36:19 +02:00

There's some conflict here - type hint says Iterable (well, Iterator but that's not the point) but you're checking if it's an instance of list or set here. I'm guessing we can just make it allow any iterable as your type hint suggests. And since we're iterating right in this if, we can just drop the first isinstance check as the TypeError will be raised for non-iterable anyway.

        if not all(isinstance(r_or_u, int) for r_or_u in role_or_user):

Remember to also update the message in raised TypeError to reflect this change.

There's some conflict here - type hint says `Iterable` (well, `Iterator` but that's not the point) but you're checking if it's an instance of `list` or `set` here. I'm guessing we can just make it allow any iterable as your type hint suggests. And since we're iterating right in this `if`, we can just drop the first `isinstance` check as the `TypeError` will be raised for non-iterable anyway. ```suggestion if not all(isinstance(r_or_u, int) for r_or_u in role_or_user): ``` Remember to also update the message in raised `TypeError` to reflect this change.
Jackenmen (Migrated from github.com) commented 2020-07-08 15:42:58 +02:00

You missed a config call here.

                self._cached_whitelist[gid] = set(await self._config.whitelist())
You missed a config call here. ```suggestion self._cached_whitelist[gid] = set(await self._config.whitelist()) ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:43:45 +02:00

You missed a config call here.

                self._cached_whitelist[gid] = set(
                    await self._config.guild_from_id(gid).whitelist()
                )
You missed a config call here. ```suggestion self._cached_whitelist[gid] = set( await self._config.guild_from_id(gid).whitelist() ) ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:45:05 +02:00

You missed a config call here.

                self._cached_whitelist[gid] = set(
                    await self._config.guild_from_id(gid).whitelist()
                )
You missed a config call here. ```suggestion self._cached_whitelist[gid] = set( await self._config.guild_from_id(gid).whitelist() ) ```
Jackenmen (Migrated from github.com) commented 2020-07-08 15:46:36 +02:00

You missed a config call here.

                self._cached_blacklist[gid] = set(
                    await self._config.guild_from_id(gid).blacklist()
                )
You missed a config call here. ```suggestion self._cached_blacklist[gid] = set( await self._config.guild_from_id(gid).blacklist() ) ```
Jackenmen (Migrated from github.com) requested changes 2020-07-08 16:01:34 +02:00
@ -122,11 +122,11 @@ class IgnoreManager:
class WhitelistBlacklistManager:
Jackenmen (Migrated from github.com) commented 2020-07-08 16:00:11 +02:00
            raise TypeError("`role_or_user` must be an iterable of `int`s.")
```suggestion raise TypeError("`role_or_user` must be an iterable of `int`s.") ```
Jackenmen (Migrated from github.com) commented 2020-07-08 16:00:44 +02:00
            raise TypeError("`role_or_user` must be an iterable of `int`s.")
```suggestion raise TypeError("`role_or_user` must be an iterable of `int`s.") ```
Jackenmen (Migrated from github.com) commented 2020-07-08 16:00:59 +02:00
            raise TypeError("`role_or_user` must be an iterable of `int`s.")
```suggestion raise TypeError("`role_or_user` must be an iterable of `int`s.") ```
Jackenmen (Migrated from github.com) commented 2020-07-08 16:01:03 +02:00
            raise TypeError("`role_or_user` must be an iterable of `int`s.")
```suggestion raise TypeError("`role_or_user` must be an iterable of `int`s.") ```
Jackenmen (Migrated from github.com) approved these changes 2020-07-08 19:24:57 +02:00
Jackenmen (Migrated from github.com) left a comment

LGTM

LGTM
Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!3910
No description provided.