Skip to content

Printing tuples of subconfigs fails #20

Description

@majmin7

Describe the bug

When a config field holds a tuple of Config instances, print_code() renders each element via its standalone __repr__ — including the # NOTE: This code is not intended to run... import header and the from ... import ... lines — instead of the clean inline nested form used for a list or dict of Configs.

The pretty-printer's value dispatch in PrettyPrinterDataclass._print (pconfigs/config.py) special-cases a single Config value (config.py:224), a list containing any Config (config.py:237), and a dict whose values are Config (config.py:263), but there is no branch for a tuple of Config. A tuple value falls through to the generic branch at config.py:317-344, which formats via str(val). For a tuple, str() calls repr() on each element, and Config.__repr__ is print_code() (config.py:357-358), which prepends the warning header (config.py:153-156) and the imports for every element.

Suggested fix: add a tuple branch alongside the existing list branch (mirroring config.py:237-261), emitting ( / ) delimiters instead of [ / ]; or generalize the list check to any non-str/bytes Sequence.

Reproducer

from pconfigs import pconfig, pdefaults

@pconfig
class Item:
    name: str

pdefaults += Item(name="")

@pconfig
class Holder:
    items_tuple: tuple
    items_list: list

pdefaults += Holder(items_tuple=(), items_list=[])

config = Holder(
    items_tuple=(Item(name="a"), Item(name="b")),
    items_list=[Item(name="a"), Item(name="b")],
)
print(config.print_code())

Expected behavior

A tuple of Configs should render inline, identically to a list:

items_tuple = (
    Item(name="a"),
    Item(name="b"),
),

Actual behavior

items_list prints inline and clean, but items_tuple prints each element as a standalone repr with its own header + imports:

items_list = [
    Item(name="a"),
    Item(name="b"),
],
items_tuple = (from pkg.mod import Item

# NOTE: This code is not intended to run. It is for reading and looking up type definitions.
# If you want to run or inspect these objects, import the config from where it is defined.

Item(
    name="a",
)
, from pkg.mod import Item

# NOTE: This code is not intended to run. ...

Item(
    name="b",
)
),

Environment

  • pconfigs version: 0.1 (repo commit 4c05636)
  • Python version: 3.10.20

Activity

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions