question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow `create_config_file` to work with an external package

See original GitHub issue

Description

[EDIT] : see https://github.com/astropy/astropy/issues/12960#issuecomment-1072537490 before reading further.

I was unable to use create_config_file with configuration options defined in a package outside of astropy. Maybe there is a trick that I didn’t figure out, but I think there is a fundamental problem in generate_config() where it is getting the configuration options based on a hardwired ConfigNamespace which always points to the built-in astropy class instead of a user-defined version as documented here: https://docs.astropy.org/en/stable/config/index.html#customizing-config-location-in-affiliated-packages.

        # Parse the subclasses, ordered by their module name
        subclasses = ConfigNamespace.__subclasses__()  # <<== symbol is defined in same configuration.py file.
        processed = set()

I’d propose to make create_config_file and generate_config be class methods of ConfigNamespace so that the above would become:

        # Parse the subclasses, ordered by their module name
        subclasses = cls.__subclasses__()
        processed = set()

Before doing this I’d like input from a package maintainer that this is on the right track and reasonable.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
taldcroftcommented, Mar 18, 2022

After quite a circuitous path I have a better understanding of this now and the root cause of problems. Basically all the business about needing class method(s) was a red herring.

The real problem is that the code to find all the subclasses of the astropy ConfigNamespace stops at one subclass deep with ConfigNamespace.__subclasses(). But the example given in the docs has the user first create a subclass where the rootname class attribute is set and then subsequent creation of class Conf(ConfigNamespace): # from the package-specific subclass means that the package conf object is 2-subclasses deep and gets lost.

Instead I put in this code:

diff --git a/astropy/config/configuration.py b/astropy/config/configuration.py
index 3ebf6c67b0..6979136ca2 100644
--- a/astropy/config/configuration.py
+++ b/astropy/config/configuration.py
@@ -643,8 +643,13 @@ def generate_config(pkgname='astropy', filename=None, verbose=False):
             # assume it's a file object, or io.StringIO
             fp = filename
 
-        # Parse the subclasses, ordered by their module name
-        subclasses = ConfigNamespace.__subclasses__()
+        # Parse the subclasses, ordered by their module name. From
+        # https://stackoverflow.com/questions/3862310
+        def all_subclasses(cls):
+            return set(cls.__subclasses__()).union(
+                [s for c in cls.__subclasses__() for s in all_subclasses(c)])
+
+        subclasses = all_subclasses(ConfigNamespace)
         processed = set()

So that is just a bug and should be fixed, but I don’t have time right now to get to it.

Another mistake is in the docs. They recommend this pattern:

import astropy.config as astropyconfig

class ConfigNamespace(astropyconfig.ConfigNamespace):
    rootname = 'packagename'

class ConfigItem(astropyconfig.ConfigItem):
    rootname = 'packagename'

But it turns out the ConfigNamespace class never uses that rootname attribute in any way, there is no need to make that package-specific subclass in the first place! If you don’t do that (i.e. class Conf(astropy.config.ConfigNamespace):, then the subclasses() bug above doesn’t apply and everything just works with the current release.

Phew. So someone should fix the docs and that subclasses problem… but in the meantime I need to get back to just using astropy config instead of fixing it.

1reaction
taldcroftcommented, Mar 15, 2022

OK, I was planning a PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I create .config file and include it to web.config?
It is not entirely clear what you want to do, but all configuration sections can be stored in a separate files and be...
Read more >
Configuration File - Prettier
A "prettier" key in your package.json file. ... The options you can use in the configuration file are the same as the API...
Read more >
configparser — Configuration file parser — Python 3.11.1 ...
This module provides the ConfigParser class which implements a basic configuration language which provides a structure similar to what's found in Microsoft ...
Read more >
nuget.config File Reference - Microsoft Learn
A Boolean indicating whether to ignore the packages folder when working with source control. The default value is false. Example: XML Copy.
Read more >
Load config from file & environment variables in Golang with ...
Today we will learn how to use Viper to load configurations from file or ... Let's create a new file config.go inside the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found