Add support for multi-line yaml values#34
Conversation
|
This case will still break it But there must be a limit, because even if we fix that case. Then, what if there is a |
|
I tried a simpler approach: import yaml
class IgnoreUnknownTagsLoader(yaml.SafeLoader):
def ignore_unknown(self, node):
return None
IgnoreUnknownTagsLoader.add_constructor(None, IgnoreUnknownTagsLoader.ignore_unknown)
with open("multi-line.yml", 'r') as file:
attributes = file.read().splitlines()[2:]
yaml_content = '\n'.join(['object:', ' attributes:'] + attributes)
data = yaml.load(yaml_content, Loader=IgnoreUnknownTagsLoader)
data['object']['raw_attributes']Output: It avoid all our custom parsing logic except the job class name. Try it on failing tests. It delegates all parsing to the standard PyYaml library so should be more robust. |
That alone without the squashing is failing the tests. But it is a good addition. I will include it in the PR |
hammady
left a comment
There was a problem hiding this comment.
The whole point is to avoid manually extracting attributes (by calling extract_attributes). You don't need this function and you don't need any squashing (which is semantically incorrect by the way).
There was a problem hiding this comment.
Pull Request Overview
Adds support for parsing multi-line YAML values in job handlers by refactoring the YAML parsing logic to handle quoted strings that span multiple lines and updating test fixtures to validate this functionality.
- Removed manual attribute extraction function and simplified to use handler lines directly
- Added custom YAML loader classes to handle Ruby objects and unknown tags
- Updated attribute extraction to work with
raw_attributesinstead ofattributes
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| pyworker/job.py | Refactored YAML parsing with custom loaders and simplified attribute extraction |
| tests/fixtures/handler_registered.yaml | Added multi-line string test case with quoted value |
| tests/test_job.py | Updated test expectations to include new multi-line attribute |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| # Construct mapping normally, ignoring Ruby-specific tags | ||
| return loader.construct_mapping(node) | ||
|
|
||
| yaml.SafeLoader.add_multi_constructor("!ruby/object:", no_ruby_objects) |
There was a problem hiding this comment.
This will be applied universally..
There was a problem hiding this comment.
Yes, I understand. In the previous commit, it was adding it to our custom loader (even though the name of that loader IgnoreUnknownTagsLoader was misleading). I see no problem in ignoring ruby classes in the Python runtime of the application using pyworker. Remember that we are not ignoring all unknown tags, but we are simply mapping ruby objects to simple python dicts regardless of their ruby class names.
pyworker fails when the handler looks like the following. We fixed this by delegating attribute extraction to PyYaml after adding a constructor for unknown ruby classes.