Skip to content

fix(text): don't crash on --query lists mixing scalars and objects - #10527

Open
devteamaegis wants to merge 1 commit into
aws:developfrom
devteamaegis:fix/text-output-mixed-query-list
Open

fix(text): don't crash on --query lists mixing scalars and objects#10527
devteamaegis wants to merge 1 commit into
aws:developfrom
devteamaegis:fix/text-output-mixed-query-list

Conversation

@devteamaegis

Copy link
Copy Markdown

What's broken

--output text exits 255 on a --query multi-select list that mixes a scalar with an object. --output json handles the same data fine.

$ aws ec2 describe-instances --output text \
    --query '[Reservations[0].Instances[0].InstanceId, Reservations[0].Instances[0].State]'
AttributeError: 'str' object has no attribute 'items'

[InstanceId, State] is ordinary JMESPath — InstanceId is a string and State is an object — so any query of that shape kills the command. Nothing is printed at all.

Why it happens

_format_list decides a list is "a list of dicts" if any element is a dict:

if any(isinstance(el, dict) for el in item):
    all_keys = _all_scalar_keys(item)

but _all_scalar_keys then calls .items() on every element, so one non-dict in the list raises. The sibling branch for nested lists already tolerates mixed content (_partition_list); this branch did not.

The fix

Skip non-dict elements when collecting scalar keys. The loop right below already routes each element through _format_text, which renders scalars and nested lists on their own — so the mixed list now prints:

i-123
16	running

The test

test_dicts_mixed_with_scalars and test_dicts_mixed_with_lists in tests/unit/test_text.py. Both fail on develop with the AttributeError and pass with the fix:

$ python -m pytest tests/unit/test_text.py -k dicts_mixed     # before
E   AttributeError: 'str' object has no attribute 'items'
2 failed, 25 deselected

$ python -m pytest tests/unit/test_text.py                    # after
27 passed

tests/unit/test_text.py tests/unit/output/ tests/unit/test_table.py — 59 passed.

_format_list treats a list as a list of dicts when *any* element is a
dict, but _all_scalar_keys then called .items() on every element.  A
--query multi-select list such as '[InstanceId, State]' returns a
scalar alongside an object, so --output text exited 255 with
AttributeError: 'str' object has no attribute 'items'.

Skip non-dict elements when collecting the scalar keys; _format_text
already renders them on their own.
@devteamaegis
devteamaegis marked this pull request as ready for review August 3, 2026 16:45
@devteamaegis
devteamaegis requested a review from a team as a code owner August 3, 2026 16:45
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.

1 participant