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] prevents registering defaults for reserved identifiers #6459

Open
wants to merge 4 commits into
base: V3/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions redbot/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ class Value:

"""

# to reserve these attributes for __getattr__
__slots__ = ("identifier_data", "default", "_driver", "_config", "__dict__")

def __init__(self, identifier_data: IdentifierData, default_value, driver, config: "Config"):
self.identifier_data = identifier_data
self.default = default_value
Expand Down Expand Up @@ -291,6 +294,9 @@ class Group(Value):

"""

# to reserve these attributes for __getattr__
__slots__ = ("_defaults", "force_registration")

def __init__(
self,
identifier_data: IdentifierData,
Expand Down Expand Up @@ -640,6 +646,19 @@ class Config(metaclass=ConfigMeta):
USER = "USER"
MEMBER = "MEMBER"

# to reserve these attributes for __getattr__
__slots__ = (
"cog_name",
"unique_identifier",
"_driver",
"force_registration",
"_defaults",
"custom_groups",
"_lock_cache",
"__weakref__",
"__dict__",
)

def __init__(
self,
cog_name: str,
Expand Down Expand Up @@ -782,6 +801,8 @@ def _get_defaults_dict(key: str, value) -> dict:
for i, k in enumerate(splitted, start=1):
if not k.isidentifier():
raise RuntimeError("'{}' is an invalid config key.".format(k))
if k in [*dir(Config), *dir(Group), *dir(Value)]:
raise RuntimeError("'{}' is a reserved config key.".format(k))
if i == len(splitted):
partial[k] = value
else:
Expand Down
7 changes: 7 additions & 0 deletions tests/core/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ def test_config_register_global_badvalues(config):
config.register_global(**{"invalid var name": True})


def test_config_register_reserved_keys(config):
config.register_global(**{"group": {"value": True}, "value": True})
for attr in [*dir(config), *dir(config.group), *dir(config.value)]:
with pytest.raises(RuntimeError):
config.register_global(**{attr: True})


async def test_config_register_guild(config, empty_guild):
config.register_guild(enabled=False, some_list=[], some_dict={})
assert config.defaults[config.GUILD]["enabled"] is False
Expand Down
Loading