microsoft / microsoft/knack

Request to add support for handling bytearray when outputting data in YAML format

Open
#291 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
358
Forks
101
PR merge metrics
No merged PRs in 30d

Description

The current implementation of yaml formatter uses the safe_dump method, which does not support data of type bytearray, which leads to the following issue

File "/home/fumingzhang/aenv/lib/python3.12/site-packages/yaml/representer.py", line 58, in represent_data
node = self.yaml_representers[None](self, data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/fumingzhang/aenv/lib/python3.12/site-packages/yaml/representer.py", line 231, in represent_undefined
raise RepresenterError("cannot represent an object", data)
yaml.representer.RepresenterError: ('cannot represent an object', bytearray(b'-----BEGIN CERTIFICATE-----\nxxxx\n-----END CERTIFICATE-----'))

def format_yaml(obj):
    import yaml
    try:
        return yaml.safe_dump(obj.result, default_flow_style=False, allow_unicode=True)
    except yaml.representer.RepresenterError:
        # yaml.safe_dump fails when obj.result is an OrderedDict. knack's --query implementation converts the result to an OrderedDict. https://github.com/microsoft/knack/blob/af674bfea793ff42ae31a381a21478bae4b71d7f/knack/query.py#L46. # pylint: disable=line-too-long
        return yaml.safe_dump(json.loads(json.dumps(obj.result)), default_flow_style=False, allow_unicode=True)

https://github.com/microsoft/knack/blob/dev/knack/output.py#L46C1-L50C46

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in knack/output.py at the YAML formatter linked in the issue and reproduce the failure with a result containing a bytearray. Check how the existing safe_dump fallback handles unsupported values. Done means YAML output succeeds for bytearray data without the RepresenterError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.