We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
enum.StrEnum
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}))
print(json.dumps({'status': Status.A}))
The text was updated successfully, but these errors were encountered:
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
StrEnum
Status
Sorry, something went wrong.
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, )
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
No branches or pull requests
hope pyyaml can support
enum.StrEnum
element of StrEnum is consider str in Python
stdlib json support this:
print(json.dumps({'status': Status.A}))
The text was updated successfully, but these errors were encountered: