Config defaults not actually updating after a cog reload #5805

Closed
opened 2022-07-25 18:13:20 +02:00 by i-am-zaidali · 3 comments
i-am-zaidali commented 2022-07-25 18:13:20 +02:00 (Migrated from github.com)

What Red version are you using?

3.4.16

What were you trying to do?

Updating guild defaults for Config in my cog.

What did you expect to happen?

For the new defaults to be returned when .all() is called on a Group object.

What actually happened?

Old defaults being returned instead.

How can we reproduce this error?

  1. Create a cog and a config instance tied to the config with force registration set to True
  2. Set guild defaults for the config objects.
  3. Load the cog and modify the config values.
  4. Change the defaults set to config by removing a few keys and adding a few.
  5. Reload the cog and try running .clear() on a Group.

Anything else?

No response

### What Red version are you using? 3.4.16 ### What were you trying to do? Updating guild defaults for Config in my cog. ### What did you expect to happen? For the new defaults to be returned when `.all()` is called on a Group object. ### What actually happened? Old defaults being returned instead. ### How can we reproduce this error? 1. Create a cog and a config instance tied to the config with force registration set to True 2. Set guild defaults for the config objects. 3. Load the cog and modify the config values. 4. Change the defaults set to config by removing a few keys and adding a few. 5. Reload the cog and try running `.clear()` on a Group. ### Anything else? _No response_
Flame442 commented 2022-08-20 00:11:49 +02:00 (Migrated from github.com)

EDIT: Misread the report, see followup comment which actually addresses the real issue.

I believe this is intended behavior. Config does not have an option to remove defaults, only to add new defaults or to override current ones. As such, successive register_scope calls will add the provided keys (if they did not exist), and will override the current default (if they did exist) for each key. There is no way (I can see, through the public APIs) to remove a default once it has been set without creating a new Config object.

Through my testing, this behavior is correct:
When keys are added, they become functional, and have the new default (baz in the following example).
When keys are overridden, a clear causes the new default to be used instead of the old one (foo in the following example).
When keys are not included, they are not "removed", they just do not get changed. The old default continues to be used (bar in the following example).

If you are seeing different behavior, please provide a code sample to reproduce the behavior you are describing, as your reproduction steps do not have any specific lines of code used beyond .clear(). If your issue matches with this intended behavior and you want to make a feature request to add a way to remove existing defaults during the life of a Config option, please make a new issue requesting that instead.

Code (Hidden, as it is not relevant to the actual bug report, see followup comment)
Code I used to attempt to reproduce your issue (this is code suited for a testing eval, not code that should be considered at all reasonable to use in production)
from redbot.core.config import Config

async def printall():
  print("> Printing current values")
  print("foo=",await bot.x.guild(ctx.guild).foo())
  try:
    print("bar=",await bot.x.guild(ctx.guild).bar())
  except Exception as e:
    print("bar=",e)
  try:
    print("baz=",await bot.x.guild(ctx.guild).baz())
  except Exception as e:
    print("baz=",e)

bot.x = Config.get_conf("Testing", identifier=145519400223506432, cog_name="Testing", force_registration=True)
print("> Registering defaults foo=foo | bar=bar")
bot.x.register_guild(
  foo="foo",
  bar="bar",
)
await printall()
print("> Setting guild values foo=foofoo | bar=barbar | baz=bazbaz")
await bot.x.guild(ctx.guild).foo.set("foofoo")
await bot.x.guild(ctx.guild).bar.set("barbar")
try:
  await bot.x.guild(ctx.guild).baz.set("bazbaz")
except Exception as e:
  print("baz set: ", e)
await printall()
print("> Registering defaults foo=foofoofoo | baz=bazbazbaz")
bot.x.register_guild(
  foo="foofoofoo",
  baz="bazbazbaz",
)
await printall()
print("> Clearing guild values")
await bot.x.guild(ctx.guild).foo.clear()
try:
  await bot.x.guild(ctx.guild).bar.clear()
except Exception as e:
  print("bar clear: ", e)
await bot.x.guild(ctx.guild).baz.clear()
await printall()

Output

> Registering defaults foo=foo | bar=bar
> Printing current values
foo= foo
bar= bar
baz= 'baz' is not a valid registered Group or value.
> Setting guild values foo=foofoo | bar=barbar | baz=bazbaz
baz set:  'baz' is not a valid registered Group or value.
> Printing current values
foo= foofoo
bar= barbar
baz= 'baz' is not a valid registered Group or value.
> Registering defaults foo=foofoofoo | baz=bazbazbaz
> Printing current values
foo= foofoo
bar= barbar
baz= bazbazbaz
> Clearing guild values
> Printing current values
foo= foofoofoo
bar= bar
baz= bazbazbaz
EDIT: Misread the report, see followup comment which actually addresses the real issue. I believe this is intended behavior. Config **does not** have an option to **remove** defaults, only to **add** new defaults or to **override** current ones. As such, successive `register_scope` calls will **add** the provided keys (if they did not exist), and will **override** the current default (if they did exist) for each key. There is no way (I can see, through the public APIs) to **remove** a default once it has been set without creating a new Config object. Through my testing, this behavior is correct: When keys are **added**, they become functional, and have the new default (`baz` in the following example). When keys are **overridden**, a `clear` causes the new default to be used instead of the old one (`foo` in the following example). When keys are **not included**, they are not "removed", they just do not get changed. The old default continues to be used (`bar` in the following example). If you are seeing different behavior, please provide a code sample to reproduce the behavior you are describing, as your reproduction steps do not have any specific lines of code used beyond `.clear()`. If your issue matches with this intended behavior and you want to make a feature request to add a way to *remove* existing defaults during the life of a Config option, please make a new issue requesting that instead. <details> <summary>Code (Hidden, as it is not relevant to the actual bug report, see followup comment)</summary> <br> Code I used to attempt to reproduce your issue (this is code suited for a testing eval, not code that should be considered at all reasonable to use in production) ```py from redbot.core.config import Config async def printall(): print("> Printing current values") print("foo=",await bot.x.guild(ctx.guild).foo()) try: print("bar=",await bot.x.guild(ctx.guild).bar()) except Exception as e: print("bar=",e) try: print("baz=",await bot.x.guild(ctx.guild).baz()) except Exception as e: print("baz=",e) bot.x = Config.get_conf("Testing", identifier=145519400223506432, cog_name="Testing", force_registration=True) print("> Registering defaults foo=foo | bar=bar") bot.x.register_guild( foo="foo", bar="bar", ) await printall() print("> Setting guild values foo=foofoo | bar=barbar | baz=bazbaz") await bot.x.guild(ctx.guild).foo.set("foofoo") await bot.x.guild(ctx.guild).bar.set("barbar") try: await bot.x.guild(ctx.guild).baz.set("bazbaz") except Exception as e: print("baz set: ", e) await printall() print("> Registering defaults foo=foofoofoo | baz=bazbazbaz") bot.x.register_guild( foo="foofoofoo", baz="bazbazbaz", ) await printall() print("> Clearing guild values") await bot.x.guild(ctx.guild).foo.clear() try: await bot.x.guild(ctx.guild).bar.clear() except Exception as e: print("bar clear: ", e) await bot.x.guild(ctx.guild).baz.clear() await printall() ``` Output ``` > Registering defaults foo=foo | bar=bar > Printing current values foo= foo bar= bar baz= 'baz' is not a valid registered Group or value. > Setting guild values foo=foofoo | bar=barbar | baz=bazbaz baz set: 'baz' is not a valid registered Group or value. > Printing current values foo= foofoo bar= barbar baz= 'baz' is not a valid registered Group or value. > Registering defaults foo=foofoofoo | baz=bazbazbaz > Printing current values foo= foofoo bar= barbar baz= bazbazbaz > Clearing guild values > Printing current values foo= foofoofoo bar= bar baz= bazbazbaz ``` </details>
Flame442 commented 2022-08-20 00:22:37 +02:00 (Migrated from github.com)

So it looks like I missed the "reload" part of your original report - my bad. Updated code snippet below, which includes a simulated "reload", showing that creating a new Config object properly clears defaults (when used correctly).

The behavior is still consistent with what it should be in this case as well:
When keys are added, they become functional, and have the new default (baz in the following example).
When keys are overridden, a clear causes the new default to be used instead of the old one (foo in the following example).
When keys are not included, the default is "removed", but internally the current value (NOT the default) does not get changed If that key is registered later, it will still have the old value. However, attempting to get the value after clearing while it has not been registered for the current Config object will either raise an exception if force_registration=True, or will return the base default of None (bar in the following example).

Again, if this behavior is not what you expect for any reason, feel free to comment with a code snippet and an explanation of what you believe should be happening.

from redbot.core.config import Config

async def printall():
  print("> Printing current values")
  print("foo=",await bot.x.guild(ctx.guild).foo())
  try:
    print("bar=",await bot.x.guild(ctx.guild).bar())
  except Exception as e:
    print("bar=",e)
  try:
    print("baz=",await bot.x.guild(ctx.guild).baz())
  except Exception as e:
    print("baz=",e)

print("> Creating Config object")
bot.x = Config.get_conf("Testing", identifier=145519400223506432, cog_name="Testing", force_registration=True)
print("> Registering defaults foo=foo | bar=bar")
bot.x.register_guild(
  foo="foo",
  bar="bar",
)
await printall()
print("> Setting guild values foo=foofoo | bar=barbar | baz=bazbaz")
await bot.x.guild(ctx.guild).foo.set("foofoo")
await bot.x.guild(ctx.guild).bar.set("barbar")
try:
  await bot.x.guild(ctx.guild).baz.set("bazbaz")
except Exception as e:
  print("baz set: ", e)
await printall()
print("> Recreating Config object")
del bot.x
bot.x = Config.get_conf("Testing", identifier=145519400223506432, cog_name="Testing", force_registration=True)
print("> Registering defaults foo=foofoofoo | baz=bazbazbaz")
bot.x.register_guild(
  foo="foofoofoo",
  baz="bazbazbaz",
)
await printall()
print("> Clearing guild values")
await bot.x.guild(ctx.guild).foo.clear()
try:
  await bot.x.guild(ctx.guild).bar.clear()
except Exception as e:
  print("bar clear: ", e)
await bot.x.guild(ctx.guild).baz.clear()
await printall()

Output

> Creating Config object
> Registering defaults foo=foo | bar=bar
> Printing current values
foo= foo
bar= bar
baz= 'baz' is not a valid registered Group or value.
> Setting guild values foo=foofoo | bar=barbar | baz=bazbaz
baz set:  'baz' is not a valid registered Group or value.
> Printing current values
foo= foofoo
bar= barbar
baz= 'baz' is not a valid registered Group or value.
> Recreating Config object
> Registering defaults foo=foofoofoo | baz=bazbazbaz
> Printing current values
foo= foofoo
bar= 'bar' is not a valid registered Group or value.
baz= bazbazbaz
> Clearing guild values
bar clear:  'bar' is not a valid registered Group or value.
> Printing current values
foo= foofoofoo
bar= 'bar' is not a valid registered Group or value.
baz= bazbazbaz
So it looks like I missed the "reload" part of your original report - my bad. Updated code snippet below, which includes a simulated "reload", showing that creating a *new* Config object properly clears defaults (when used correctly). The behavior is still consistent with what it should be in *this* case as well: When keys are **added**, they become functional, and have the new default (baz in the following example). When keys are **overridden**, a clear causes the new default to be used instead of the old one (foo in the following example). When keys are **not included**, the default is "removed", but **internally** the current value (NOT the default) does not get changed If that key is registered later, it will **still have the old value**. However, attempting to get the value after clearing *while it has not been registered for the current Config object* will either raise an exception if `force_registration=True`, or will return the base default of `None` (bar in the following example). Again, if this behavior is not what you expect for any reason, feel free to comment with a code snippet and an explanation of what you believe should be happening. ```py from redbot.core.config import Config async def printall(): print("> Printing current values") print("foo=",await bot.x.guild(ctx.guild).foo()) try: print("bar=",await bot.x.guild(ctx.guild).bar()) except Exception as e: print("bar=",e) try: print("baz=",await bot.x.guild(ctx.guild).baz()) except Exception as e: print("baz=",e) print("> Creating Config object") bot.x = Config.get_conf("Testing", identifier=145519400223506432, cog_name="Testing", force_registration=True) print("> Registering defaults foo=foo | bar=bar") bot.x.register_guild( foo="foo", bar="bar", ) await printall() print("> Setting guild values foo=foofoo | bar=barbar | baz=bazbaz") await bot.x.guild(ctx.guild).foo.set("foofoo") await bot.x.guild(ctx.guild).bar.set("barbar") try: await bot.x.guild(ctx.guild).baz.set("bazbaz") except Exception as e: print("baz set: ", e) await printall() print("> Recreating Config object") del bot.x bot.x = Config.get_conf("Testing", identifier=145519400223506432, cog_name="Testing", force_registration=True) print("> Registering defaults foo=foofoofoo | baz=bazbazbaz") bot.x.register_guild( foo="foofoofoo", baz="bazbazbaz", ) await printall() print("> Clearing guild values") await bot.x.guild(ctx.guild).foo.clear() try: await bot.x.guild(ctx.guild).bar.clear() except Exception as e: print("bar clear: ", e) await bot.x.guild(ctx.guild).baz.clear() await printall() ``` Output ``` > Creating Config object > Registering defaults foo=foo | bar=bar > Printing current values foo= foo bar= bar baz= 'baz' is not a valid registered Group or value. > Setting guild values foo=foofoo | bar=barbar | baz=bazbaz baz set: 'baz' is not a valid registered Group or value. > Printing current values foo= foofoo bar= barbar baz= 'baz' is not a valid registered Group or value. > Recreating Config object > Registering defaults foo=foofoofoo | baz=bazbazbaz > Printing current values foo= foofoo bar= 'bar' is not a valid registered Group or value. baz= bazbazbaz > Clearing guild values bar clear: 'bar' is not a valid registered Group or value. > Printing current values foo= foofoofoo bar= 'bar' is not a valid registered Group or value. baz= bazbazbaz ```
Flame442 commented 2022-11-09 07:13:42 +01:00 (Migrated from github.com)

Due to not receiving a response, and this seeming like intended behavior, I'm closing this issue. If you still think there is an issue, and you can provide exact reproduction steps similar to what I wrote, I can take another look at this.

Due to not receiving a response, and this seeming like intended behavior, I'm closing this issue. If you still think there is an issue, and you can provide exact reproduction steps similar to what I wrote, I can take another look at this.
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#5805
No description provided.