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

Unable to save Config or convert to str if header ends with * #62

Closed
HLasse opened this issue Jan 26, 2024 · 2 comments
Closed

Unable to save Config or convert to str if header ends with * #62

HLasse opened this issue Jan 26, 2024 · 2 comments
Labels
duplicate This issue or pull request already exists

Comments

@HLasse
Copy link

HLasse commented Jan 26, 2024

When using variadic arguments in the ending of headers in a config, the Config can not be saved.

Minimal example:

import catalogue
from confection import Config, registry


class Registry(registry):
    registry = catalogue.create("psycop", "test_reg")


@Registry.registry.register("test_fn")
def test_fn(*args: str):
    return(args)


config_str = """
[test_fn]
@registry = "test_fn"

[test_fn.*]
arg1 = "test1"
arg2 = "test2"
"""

if __name__ == "__main__":
    cfg = Config().from_str(text=config_str)
    resolved = Registry.resolve(cfg)
    print(resolved["test_fn"])
    cfg.to_str()

Output

('test1', 'test2')
Traceback (most recent call last):
  File "/workspaces/psycop-common/confection_test.py", line 27, in <module>
    cfg.to_str()
  File "/usr/local/lib/python3.10/site-packages/confection/__init__.py", line 447, in to_str
    flattened.set(section_name, key, try_dump_json(value, node))
  File "/usr/local/lib/python3.10/configparser.py", line 1206, in set
    super().set(section, option, value)
  File "/usr/local/lib/python3.10/configparser.py", line 904, in set
    raise NoSectionError(section) from None
configparser.NoSectionError: No section: 'test_fn.*'

The config is resolved correctly, but we encounter a NoSectionError when attempting to output the config to a str.

Workaround
Specifying the variadic arguments under the test_fn header makes it both resolve and output as str correctly, but might not to viable in all cases if the input is of a more complex type.

config_str = """
[test_fn]
@registry = "test_fn"
* = ["test1", "test2"]
"""

Is this something you'd want to support?

@svlandeg
Copy link
Member

Merging with #52

@svlandeg svlandeg added the duplicate This issue or pull request already exists label Jan 26, 2024
@HLasse
Copy link
Author

HLasse commented Jan 26, 2024

Whoops, my bad - I had forgotten everything about the other issue 😶‍🌫️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants