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

Instant of custom python class as value #792

Open
adosar opened this issue Apr 11, 2024 · 0 comments
Open

Instant of custom python class as value #792

adosar opened this issue Apr 11, 2024 · 0 comments

Comments

@adosar
Copy link

adosar commented Apr 11, 2024

Hello,

I want to get a dictionary of the form {key: instance_of_a_class}.

I have the following .py file as an example:

import yaml

class Bar:
    def __init__(self):
        x = 3213


out = ''' 
key: !!python/object:__main__.Bar
'''

print(yaml.unsafe_load(out))

I get the following error:

Traceback (most recent call last):
  File "/home/ansar/trial/foo.py", line 12, in <module>
    print(yaml.unsafe_load(out))
          ^^^^^^^^^^^^^^^^^^^^^
  File "/home/ansar/venvir/ml/lib64/python3.11/site-packages/yaml/__init__.py", line 145, in unsafe_load
    return load(stream, UnsafeLoader)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ansar/venvir/ml/lib64/python3.11/site-packages/yaml/__init__.py", line 81, in load
    return loader.get_single_data()
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ansar/venvir/ml/lib64/python3.11/site-packages/yaml/constructor.py", line 51, in get_single_data
    return self.construct_document(node)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ansar/venvir/ml/lib64/python3.11/site-packages/yaml/constructor.py", line 60, in construct_document
    for dummy in generator:
  File "/home/ansar/venvir/ml/lib64/python3.11/site-packages/yaml/constructor.py", line 620, in construct_python_object
    state = self.construct_mapping(node, deep=deep)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ansar/venvir/ml/lib64/python3.11/site-packages/yaml/constructor.py", line 218, in construct_mapping
    return super().construct_mapping(node, deep=deep)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ansar/venvir/ml/lib64/python3.11/site-packages/yaml/constructor.py", line 134, in construct_mapping
    raise ConstructorError(None, None,
yaml.constructor.ConstructorError: expected a mapping node, but found scalar
  in "<unicode string>", line 2, column 11:
    instance: !!python/object:__main__.Bar
              ^
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

1 participant