Skip to content

reload_config no longer properly reloads user-set values #18138

Closed

Description

@mhvk

Description

config.reload_config is meant to reset all user-defined configuration items, but no longer does in 7.1 (it works on 7.0).

It is related to #17461, which optimized getting config item values by storing them on the items themselves. Unfortunately, reload_config only clears the general storage, not these cached values.

Expected behavior

It should reload.

How to Reproduce

from astropy import config
from astropy.utils.data import conf
conf.remote_timeout
# 10.0
conf.remote_timeout = 5
config.reload_config()
conf.remote_timeout
# 5.0
conf.reload()  # This does work
conf.remote_timeout
# 10.0

Versions

import astropy
astropy.system_info()
platform
--------
platform.platform() = 'Linux-6.12.25-amd64-x86_64-with-glibc2.41'
platform.version() = '#1 SMP PREEMPT_DYNAMIC Debian 6.12.25-1 (2025-04-25)'
platform.python_version() = '3.13.3'

packages
--------
astropy              7.2.dev45+g76354c823e
numpy                2.2.5
scipy                --
matplotlib           --
pandas               --
pyerfa               2.0.1.5

Activity

  1. mhvk commented on May 13, 2025

    @mhvk
    ContributorAuthor

    A fix is the following, but this is astropy specific, so not quite correct. It may be better to make value some kind of weakref link to the actual configuration. CC @saimn since the caching in #17461 is what is causing the problems.

    git diff
    diff --git a/astropy/config/configuration.py b/astropy/config/configuration.py
    index 8b08f9c55d..3a57abf096 100644
    --- a/astropy/config/configuration.py
    +++ b/astropy/config/configuration.py
    @@ -771,6 +771,16 @@ def reload_config(packageormod=None, rootname=None):
             Name of the root configuration directory - see `get_config`
             for details.
         """
    +    sec = get_config(packageormod, False, rootname=rootname)
    +    # look for the section that is its own parent - that's the base object
    +    while sec.parent is not sec:
    +        sec = sec.parent
    +    # Reset any items explicitly set, to clear possible cached values.
    +    for modname, keys in sec.items():
    +        mod = importlib.import_module(f"astropy.{modname}")
    +        conf = getattr(mod, "conf", None)
    +        if conf is not None:
    +            conf.reload()
         sec = get_config(packageormod, True, rootname=rootname)
         # look for the section that is its own parent - that's the base object
         while sec.parent is not sec:
  2. added this to the v7.1.0 milestone on May 13, 2025
  3. pllim commented on May 13, 2025

    @pllim
    Member

    Thanks for investigating, @mhvk !

    I think this is a release blocker for astropy v7.1.0. We cannot ship a release with broken public API that affects config for downstream packages. Simplest is to revert #17461 unless someone has a better idea. And definitely need RC2 now after this is patched. What do you think @astropy/astropy-project-release-team ?

    Also cc @Cadair since apparently he is very into subclassing config stuff downstream.

  4. saimn commented on May 13, 2025

    @saimn
    Contributor

    Darn! There are too many ways to do stuff with this config API...
    We could do what @mhvk proposes, just need to get the package name, which can be done the same was as get_config. (I think but actually maybe not, not sure how it works for e.g. astroquery which puts its config file in .astropy/config/... edit: forget this, people would need to specify packageormod in that case)

  5. saimn commented on May 14, 2025

    @saimn
    Contributor
    +    for modname, keys in sec.items():
    +        mod = importlib.import_module(f"astropy.{modname}")
    

    actually this doesn't work, sec.items() just returns the caller module (config.configuration) and not the list of modules that would need to be reloaded. And I don't know how to get this list (we would need to reload all conf objects for the package but we don't store that list).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions