question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regression - RepresenterError on dict subclass

See original GitHub issue

Due to #126, I’m running pyyaml at master. When I try to run code that passes on released versions of pyyaml, it fails in master:

-> raise RepresenterError("cannot represent an object", data)
(Pdb) type(data)
<class 'pmxbot.dictlib.ConfigDict'>
(Pdb) isinstance(data, dict)
True

As the object is a subclass of dict, it would previously be treated as a dict for the purpose of serialization. Is this an intentional change?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

3reactions
jaracocommented, Oct 19, 2019

I tried using the dangerous dumper, but found that creates a YAML file that requires a dangerous loader. What I really want is to instruct YAML to serialize a dict subclass exactly as it would a dict.

0reactions
dhimmelcommented, Nov 24, 2020

I also ran into this issue trying to dump an instance of a dict subclass.

I found an ugly workaround on StackOverflow that you must run before calling yaml.dump:

yaml.add_representer(
    Dict_Subclass,
    lambda dumper, data: dumper.represent_mapping('tag:yaml.org,2002:map', data.items())
)

Limitations:

  1. Dict_Subclass must be the specific dict subclass. This doesn’t fix the issue for all dict subclasses, just those explicitly overriden.
  2. It works with yaml.dump but not yaml.safe_dump.
Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How to properly subclass dict and override __getitem ...
I am debugging some code and I want to find out when a particular dictionary is accessed. Well, it's actually a class that...
Read more >
What is more pythonic - subclassing dict or creating external ...
In general, in Python I favor using functions over subclassing internal classes. In the end, if you write your String -> dict method, ......
Read more >
Using Counter - a Dictionary Subclass in Python - Template
Explore the Counter Dictionary subclass, learn how to determine the frequency of all characters in a string by downloading this free .ipynb template....
Read more >
Understand __slots__ in Python - Towards Data Science
We use this dictionary to get and set attributes. ... Or, you don't put __slots__ in the subclass, it will by default have...
Read more >
How To Work With Python Dictionary [With Code Examples]
This tutorial explains what is a Dictionary in Python and how to work ... It is a dict subclass that holds an integer...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found