diff --git a/redbot/core/config.py b/redbot/core/config.py index dc6bbed7464..3b3f921d032 100644 --- a/redbot/core/config.py +++ b/redbot/core/config.py @@ -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 @@ -291,6 +294,9 @@ class Group(Value): """ + # to reserve these attributes for __getattr__ + __slots__ = ("_defaults", "force_registration") + def __init__( self, identifier_data: IdentifierData, @@ -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, @@ -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: diff --git a/tests/core/test_config.py b/tests/core/test_config.py index ac34ae6885b..f3861354dba 100644 --- a/tests/core/test_config.py +++ b/tests/core/test_config.py @@ -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