title: Include One Yaml File Inside Another
body: <p>I want to have a base config file which is used by other config files to share common config.</p>
  <p>E.g if I have one file <code>base.yml</code> with</p>
  <pre><code>foo: 1
  
  bar:
    - 2
    - 3
  </code></pre>
  <p>And then a second file <code>some_file.yml</code> with</p>
  <pre><code>foo: 2
  
  baz: &quot;baz&quot;
  </code></pre>
  <p>What I'd want to end up with a merged config file with</p>
  <pre><code>foo: 2
  
  bar:
    - 2
    - 3
  
  baz: &quot;baz&quot;
  </code></pre>
  <p>It's easy enough to write a custom loader that handles an <code>!include</code> tag.</p>
  <pre><code>class ConfigLoader(yaml.SafeLoader):
  
      def __init__(self, stream):
          super().__init__(stream)
          self._base = Path(stream.name).parent
  
      def include(self, node):
          file_name = self.construct_scalar(node)
          file_path = self._base.joinpath(file_name)
  
          with file_path.open(&quot;rt&quot;) as fh:
              return yaml.load(fh, IncludeLoader)
  </code></pre>
  <p>Then I can parse an <code>!include</code> tag. So if my file is</p>
  <pre><code>inherit:
     !include base.yml
  
  foo: 2
  
  baz: &quot;baz&quot;
  </code></pre>
  <p>But now the base config is a mapping. I.e. if I load the the file I'll end up with</p>
  <pre class="lang-py prettyprint-override"><code>config = {'a': [42], 'c': [3.6, [1, 2, 3]], 'include': [{'a': 1, 'b': [1.43, 543.55]}]}
  </code></pre>
  <p>But if I don't make the tag part of a mapping, e.g.</p>
  <pre><code>!include base.yml
  
  foo: 2
  
  baz: &quot;baz&quot;
  </code></pre>
  <p>I get an error. <code>yaml.scanner.ScannerError: mapping values are not allowed here</code>.</p>
  <p>But I know that the yaml parser can parse tags without needing a mapping. Because I can do things like</p>
  <pre><code>!!python/object:foo.Bar
  x: 1.0   
  y: 3.14
  </code></pre>
  <p>So how do I write a loader and/or structure my YAML file so that I can include another file in my configuration?</p>