title: Represent instance of different classes with the same base class in pyyaml
body: <p>I have some set of unittests and want to store results of each test run as YAML file for further analysis. Dump data in YAML format fit my needs in several ways. But tests belongs to different suits and results have different parent class, This is example of what I have:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; rz # shorthand for result
  [&lt;upstream_suite.fftest.SimpleTest testMethod=test_is_fsType_not_set&gt;,
  &lt;upstream_suite.openfolder.UfoOpenTest testMethod=test_is_A&gt;,
  &lt;upstream_suite.openfolder.UfoOpenTest testMethod=test_is_A_a_glyph_instance&gt;,
  &lt;upstream_suite.openfolder.UfoOpenTest testMethod=test_is_ended_ufo&gt;,
  &lt;upstream_suite.openfolder.UfoOpenTest testMethod=test_is_folder&gt;,
  &lt;upstream_suite.openfolder.UfoOpenTest testMethod=test_is_fsType_eq_1&gt;,
  &lt;upstream_suite.openfolder.UfoOpenTest testMethod=test_it_exists&gt;]
  # etc.
  </code></pre>
  
  <p>Each test can have its own parent class.</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; type(rz[0]) == type(rz[1])
  False
  </code></pre>
  
  <p>I've added my own base class for all classes with <code>__getstate__</code> method, but it is not working for some reason:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; rz[0].__getstate__()
  {'targets': ['upstream'], 'methodDoc': 'Is the OS/2 table fsType set to 0?', 'methodName': 'test_is_fsType_not_set', 'tool': 'FontForge', 'name': 'upstream_suite.fftest'}
  &gt;&gt;&gt; yaml.safe_dump(rz[0])
  *** RepresenterError: cannot represent an object: test_is_fsType_not_set (upstream_suite.fftest.SimpleTest)
  </code></pre>
  
  <p>Documentations say that I can make representer for each class, so I tried it and wrote this code:</p>
  
  <pre class="lang-py prettyprint-override"><code>import yaml
  from somepath import MyTestCase
  
  def repr_testcase(dumper, data):
      return dumper.represent_mapping(u'tag:yaml.org,2002:map', {
          'methodDoc': data._testMethodDoc,
          'tool': data.tool,
          'name': data.name,
          'methodName': data._testMethodName,
          'targets': data.targets,
          })
  
  # Register representer 
  yaml.SafeDumper.add_representer(MyTestCase, repr_testcase)
  </code></pre>
  
  <p>The same time:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; isinstance(rz[0], MyTestCase)
  True
  </code></pre>
  
  <p>Even after that <code>safe_dump</code> isn't working. But if I made this trick, then it works and I can get result:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; yaml.SafeDumper.add_representer(type(rz[0]), repr_testcase)
  &gt;&gt;&gt; print(yaml.safe_dump(rz[0]))
  methodDoc: Is the OS/2 table fsType set to 0?
  methodName: test_is_fsType_not_set
  name: checker.upstream_suite.fontforge_suite.fftest
  targets: [upstream]
  tool: FontForge
  </code></pre>
  
  <p>But that means that I need to register all possible occurrences, which is, well... at least, does not looks smart. Actually my real <code>result</code> is more complex dictionary, tests harvested from different places, etc.</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; for x in rz: yaml.SafeDumper.add_representer(type(x), repr_testcase)
  &gt;&gt;&gt; yaml.safe_dump(rz)
  ... here is expected result
  </code></pre>
  
  <p>So, question is, what I'm doing wrong with <code>__getstate__</code> or how I can make work pyyaml with all instances of particular base class with only one representer registration. </p>