tanbro / pyyaml-include

yaml include other yaml
https://pypi.org/project/pyyaml-include/
GNU General Public License v3.0
78 stars 20 forks source link

It seems that I can't rewrite the inherited argments from base `yaml` file. #35

Closed minipuding closed 1 year ago

minipuding commented 1 year ago

When I use a test case like: base.yaml:

# base.yaml
name: John Doe
age: 30
address:
  city: New York
  state: NY

derived.yaml:

# derived.yaml
!include base.yaml
age: 35
address:
  state: CA

test.py

import yaml
from yamlinclude import YamlIncludeConstructor

YamlIncludeConstructor.add_to_loader_class(loader_class=yaml.FullLoader, base_dir='./')

with open('derived.yaml') as f:
    data = yaml.load(f, Loader=yaml.FullLoader)

print(data)

I expect the output is {'name': 'John Doe', 'age': 35, 'address': {'city': 'New York', 'state': 'CA'}} but there is a error occured. The traceback is as follows:

Traceback (most recent call last)
[d:\SelfDatas\SJTU\Projects\Labs\yaml\read_yaml.ipynb](file:///D:/SelfDatas/SJTU/Projects/Labs/yaml/read_yaml.ipynb) Cell 1 in 7
      [4](vscode-notebook-cell:/d%3A/SelfDatas/SJTU/Projects/Labs/yaml/read_yaml.ipynb#W0sZmlsZQ%3D%3D?line=3) YamlIncludeConstructor.add_to_loader_class(loader_class=yaml.FullLoader, base_dir='[./](https://file+.vscode-resource.vscode-cdn.net/d%3A/SelfDatas/SJTU/Projects/Labs/yaml/)')
      [6](vscode-notebook-cell:/d%3A/SelfDatas/SJTU/Projects/Labs/yaml/read_yaml.ipynb#W0sZmlsZQ%3D%3D?line=5) with open('derived.yaml') as f:
----> [7](vscode-notebook-cell:/d%3A/SelfDatas/SJTU/Projects/Labs/yaml/read_yaml.ipynb#W0sZmlsZQ%3D%3D?line=6)     data = yaml.load(f, Loader=yaml.FullLoader)
      [9](vscode-notebook-cell:/d%3A/SelfDatas/SJTU/Projects/Labs/yaml/read_yaml.ipynb#W0sZmlsZQ%3D%3D?line=8) print(data)

File [d:\Program](file:///D:/Program) Files\Anaconda\miniconda\envs\main\lib\site-packages\yaml\__init__.py:81, in load(stream, Loader)
     79 loader = Loader(stream)
     80 try:
---> 81     return loader.get_single_data()
     82 finally:
     83     loader.dispose()

File [d:\Program](file:///D:/Program) Files\Anaconda\miniconda\envs\main\lib\site-packages\yaml\constructor.py:49, in BaseConstructor.get_single_data(self)
     47 def get_single_data(self):
     48     # Ensure that the stream contains a single document and construct it.
---> 49     node = self.get_single_node()
     50     if node is not None:
     51         return self.construct_document(node)

File [d:\Program](file:///D:/Program) Files\Anaconda\miniconda\envs\main\lib\site-packages\yaml\composer.py:36, in Composer.get_single_node(self)
     34 document = None
     35 if not self.check_event(StreamEndEvent):
...
    583 # the parser.
    584 if not self.flow_level:

ScannerError: mapping values are not allowed here
  in "derived.yaml", line 4, column 4
minipuding commented 1 year ago

Oh, sorry, I find the same issue at #7 and #22. But why hasn't such an important feature been supported for almost four years.

minipuding commented 1 year ago

Oh, sorry, I find the same issue at #7 and #22. But why hasn't such an important feature been supported for almost four years.

Let's think of it as a PR.

tanbro commented 1 year ago

Duplicate of #7

minipuding commented 1 year ago

I found that it wasn't just a matter of not being able to be used at the top level said on #7, it was a matter of not being able to inherit at all, even at other levels. data.yaml

batch_size: 8
workers: 4

main.yaml

data:
  !include: data.yaml
  batch_size: 4

is also wrong.

I hope the output is:

data:
  batch_size: 4
  workers: 4
tanbro commented 1 year ago

No, sorry for that the extention can not do such a thing.

Such an including requires it to work like a template engine, the extention can not do that.