Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for enum.StrEnum #722

Open
trim21 opened this issue Jun 29, 2023 · 3 comments
Open

Support for enum.StrEnum #722

trim21 opened this issue Jun 29, 2023 · 3 comments

Comments

@trim21
Copy link

trim21 commented Jun 29, 2023

hope pyyaml can support enum.StrEnum

element of StrEnum is consider str in Python

import enum

import yaml


class Status(enum.StrEnum):
    A = 'a'
    B = 'b'


print(isinstance(Status.A, str)) # True
print(yaml.safe_dump({'status': Status.A}))
Traceback (most recent call last):
  File "C:\Users\Trim21\proj\test\a.py", line 12, in <module>
    print(yaml.safe_dump({'status': Status.A}))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Trim21\proj\test\.venv\Lib\site-packages\yaml\__init__.py", line 269, in safe_dump
    return dump_all([data], stream, Dumper=SafeDumper, **kwds)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Trim21\proj\test\.venv\Lib\site-packages\yaml\__init__.py", line 241, in dump_all
    dumper.represent(data)
  File "C:\Users\Trim21\proj\test\.venv\Lib\site-packages\yaml\representer.py", line 27, in represent
    node = self.represent_data(data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Trim21\proj\test\.venv\Lib\site-packages\yaml\representer.py", line 48, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Trim21\proj\test\.venv\Lib\site-packages\yaml\representer.py", line 207, in represent_dict
    return self.represent_mapping('tag:yaml.org,2002:map', data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Trim21\proj\test\.venv\Lib\site-packages\yaml\representer.py", line 118, in represent_mapping
    node_value = self.represent_data(item_value)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Trim21\proj\test\.venv\Lib\site-packages\yaml\representer.py", line 58, in represent_data
    node = self.yaml_representers[None](self, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Trim21\proj\test\.venv\Lib\site-packages\yaml\representer.py", line 231, in represent_undefined
    raise RepresenterError("cannot represent an object", data)
yaml.representer.RepresenterError: ('cannot represent an object', <Status.A: 'a'>)

stdlib json support this: print(json.dumps({'status': Status.A}))

@spanezz
Copy link

spanezz commented Feb 13, 2024

Current workaround for this specific case:

  yaml.SafeDumper.add_representer(
      Status,
      yaml.representer.SafeRepresenter.represent_str,
  )                                                            

It may need to be repeated for the all the various dumpers that may be used used, and it unfortunately does not work with StrEnum instead of Status

@bergtholdt
Copy link

Just use add_multi_representer for the base class, so all sub-classes will use the base representer:

  yaml.SafeDumper.add_multi_representer(
      StrEnum,
      yaml.representer.SafeRepresenter.represent_str,
  )

@trim21
Copy link
Author

trim21 commented Mar 7, 2024

Just use add_multi_representer for the base class, so all sub-classes will use the base representer:

  yaml.SafeDumper.add_multi_representer(
      StrEnum,
      yaml.representer.SafeRepresenter.represent_str,
  )

would be nice to have this included in pyyaml library

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

No branches or pull requests

3 participants