-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #212 from bioimage-io/remove_ruamel_yaml
Remove ruamel.yaml dependency
- Loading branch information
Showing
8 changed files
with
160 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import json | ||
import sys | ||
from argparse import ArgumentParser | ||
from pathlib import Path | ||
|
||
from marshmallow_jsonschema import JSONSchema | ||
|
||
import bioimageio.spec | ||
|
||
from bioimageio.spec.shared.common import ruamel_yaml, pyyaml_yaml | ||
|
||
if ruamel_yaml is None: | ||
raise RuntimeError("Cannot compare yaml syntax without the ruamel.yaml package") | ||
|
||
|
||
def parse_args(): | ||
p = ArgumentParser( | ||
description="Check for differences between yaml 1.1 (using PyYAML) and yaml 1.2 syntax (using ruamel.yaml)." | ||
) | ||
p.add_argument("resource_description_path", type=Path) | ||
args = p.parse_args() | ||
return args | ||
|
||
|
||
def main(resource_description_path: Path): | ||
|
||
pyyaml = pyyaml_yaml.load(resource_description_path) | ||
assert isinstance(pyyaml, dict) | ||
ruamel = ruamel_yaml.load(resource_description_path) | ||
assert isinstance(ruamel, dict) | ||
|
||
diff = {key: (value, ruamel[key]) for key, value in pyyaml.items() if value != ruamel[key]} | ||
if diff: | ||
print(f"Found differences between yaml syntax 1.1/1.2 for {resource_description_path}:") | ||
print(diff) | ||
else: | ||
print(f"No differences found between yaml syntax 1.1/1.2 for {resource_description_path}:") | ||
|
||
return len(diff) | ||
|
||
|
||
if __name__ == "__main__": | ||
args = parse_args() | ||
sys.exit(main(args.resource_description_path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
Tests ruamel.yaml replacement. | ||
Why? | ||
PyYAML defaults to YAML 1.1 and does not yet support YAML 1.2 (which is the default for ruamel.yaml atm). | ||
For yaml 1.2 in PyYAML see https://github.com/yaml/pyyaml/issues/116 and https://github.com/yaml/pyyaml/pull/512 . | ||
We want to ensure compatibility with yaml 1.2, while using yaml 1.1 for now to drop the ruamel.yaml dependency | ||
in order to more easily run with pyodide. | ||
For differences between yaml 1.1 and 1.2, see: | ||
https://yaml.readthedocs.io/en/latest/pyyaml.html#defaulting-to-yaml-1-2-support | ||
""" | ||
import json | ||
|
||
|
||
def test_unet2d_nuclei_broad_is_indifferent(unet2d_nuclei_broad_any_path): | ||
from bioimageio.spec.shared.common import pyyaml_yaml, ruamel_yaml | ||
|
||
expected = ruamel_yaml.load(unet2d_nuclei_broad_any_path) | ||
actual = pyyaml_yaml.load(unet2d_nuclei_broad_any_path) | ||
|
||
# ignore known difference: | ||
# timestamp contains default utc timezone (tzinfo.utc) for ruamel.yaml, but not for PyYAML | ||
expected.pop("timestamp") | ||
actual.pop("timestamp") | ||
|
||
assert expected == actual | ||
|
||
|
||
def test_flaoting_point_numbers_pyyaml(): | ||
from bioimageio.spec.shared.common import pyyaml_yaml, ruamel_yaml | ||
|
||
expected = {"one": 1, "low": 0.000001} | ||
data_str = json.dumps(expected) | ||
actual = pyyaml_yaml.load(data_str) | ||
assert expected == actual | ||
|
||
|
||
# just to be sure we test ruamel.yaml as well... | ||
def test_flaoting_point_numbers_ruamel(): | ||
from bioimageio.spec.shared.common import ruamel_yaml | ||
|
||
expected = {"one": 1, "low": 0.000001} | ||
data_str = json.dumps(expected) | ||
actual = ruamel_yaml.load(data_str) | ||
assert expected == actual | ||
|
||
|
||
def test_compare_script(unet2d_nuclei_broad_any_path): | ||
from scripts.compare_yaml_syntax import main | ||
|
||
diff = main(unet2d_nuclei_broad_any_path) | ||
|
||
assert diff == 1 # ignore difference for timestamp |