Skip to content

Allows for multiple -s|--section options - #148

Open
caruccio wants to merge 2 commits into
mattrobenolt:mainfrom
caruccio:multi-sections
Open

Allows for multiple -s|--section options#148
caruccio wants to merge 2 commits into
mattrobenolt:mainfrom
caruccio:multi-sections

Conversation

@caruccio

@caruccio caruccio commented Aug 4, 2026

Copy link
Copy Markdown

This PR adds support for multiple -s|--section options to be provided in the command line.
This allows for more than one section being filtered for the data context.

Signed-off-by: Mateus Caruccio <mateus.caruccio@getupcloud.com>
@caruccio

caruccio commented Aug 5, 2026

Copy link
Copy Markdown
Author

Hey @mattrobenolt any chance of this to be merged?

@mattrobenolt

Copy link
Copy Markdown
Owner

You literally opened this yesterday, 24 hours ago.

@caruccio

caruccio commented Aug 5, 2026

Copy link
Copy Markdown
Author

Oh, my apologies. I wasn't expecting you to respond that soon.

Signed-off-by: Mateus Caruccio <mateus.caruccio@getupcloud.com>

@mattrobenolt mattrobenolt left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feature makes sense and I want it, but this needs another round. The inline comments are the blocking stuff, and CI is red right now because of the first one.

One design question before you respin: is merging the sections into one flat namespace the behavior you're after? The description says "filtered", which sounds more like keeping the top-level dict and narrowing it to the named sections, something like data = {k: data[k] for k in opts.section}. That keeps {{ a.x }} and {{ b.y }} namespaced, has no key collisions, and the mapping check goes away entirely. Flattening means two sections iwth the same key silently clobber each other, which feels like a footgun. If merge is the deliberate choice, cool, but let's make it deliberate.

Also needs tests for the new behavior.

Comment thread jinja2cli/cli.py
if opts.section:
section = opts.section
# Use specified sections if needed
if len(opts.section) == 1:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This crashes when -s isn't passed at all. action="append" without a default leaves opts.section as None, and len(None) is why CI is fully red right now. The failing tests never pass -s, so the default path is just dead. default=[] on the argument fixes it.

Comment thread jinja2cli/cli.py
merged_data = {}
for k, v in data.items():
if k in opts.section:
if not isinstance(v, Iterable):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strings are Iterable, so this guard doesn't guard anything. A section whose value is a string sails through this check, then dict.update() explodes with ValueError: dictionary update sequence element #0 has length 1; 2 is required. You want collections.abc.Mapping here, and the error message means mapping, not iterable.

Comment thread jinja2cli/cli.py
elif len(opts.section) > 1:
# for multiple values, all must be iterables
merged_data = {}
for k, v in data.items():

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Walk opts.section instead of data.items(). Two problems as written: a typo'd section name silently succeeds (single -s raises unknown section, multi just renders with the data missing), and merge order follows the data file's key order instead of the command line, so -s a -s b and -s b -s a produce identical output when keys collide. Iterating the flags fixes both.

@caruccio

Copy link
Copy Markdown
Author

Hello Matt. I'm very busy right now, but am planning to come back ASAP. As you stated before, this is not working as intended.
Thanks for your review so far.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants