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
Describe the bug
When a config field holds a
tupleofConfiginstances,print_code()renders each element via its standalone__repr__— including the# NOTE: This code is not intended to run...import header and thefrom ... import ...lines — instead of the clean inline nested form used for alistordictof Configs.The pretty-printer's value dispatch in
PrettyPrinterDataclass._print(pconfigs/config.py) special-cases a singleConfigvalue (config.py:224), alistcontaining anyConfig(config.py:237), and adictwhose values areConfig(config.py:263), but there is no branch for atupleofConfig. A tuple value falls through to the generic branch atconfig.py:317-344, which formats viastr(val). For a tuple,str()callsrepr()on each element, andConfig.__repr__isprint_code()(config.py:357-358), which prepends the warning header (config.py:153-156) and the imports for every element.Suggested fix: add a
tuplebranch alongside the existinglistbranch (mirroringconfig.py:237-261), emitting(/)delimiters instead of[/]; or generalize thelistcheck to any non-str/bytesSequence.Reproducer
Expected behavior
A
tupleof Configs should render inline, identically to alist:Actual behavior
items_listprints inline and clean, butitems_tupleprints each element as a standalone repr with its own header + imports:Environment
4c05636)