From ee22c0344b7d303f6354296abadd17ac480404d5 Mon Sep 17 00:00:00 2001 From: Wouter Zwerink <35296208+wouterzwerink@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:09:26 +0100 Subject: [PATCH 1/5] Use CDumper whenever possible --- omegaconf/_utils.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/omegaconf/_utils.py b/omegaconf/_utils.py index ec07aa51f..d3ba58de8 100644 --- a/omegaconf/_utils.py +++ b/omegaconf/_utils.py @@ -41,6 +41,13 @@ except ImportError: # pragma: no cover BaseLoader = yaml.SafeLoader +try: + from yaml import CDumper + + BaseDumper = CDumper +except ImportError: # pragma: no cover + BaseDumper = yaml.Dumper + NoneType: Type[None] = type(None) BUILTIN_VALUE_TYPES: Tuple[Type[Any], ...] = ( @@ -105,11 +112,11 @@ def __repr__(self) -> str: _DEFAULT_MARKER_: Any = Marker("_DEFAULT_MARKER_") -class OmegaConfDumper(yaml.Dumper): # type: ignore +class OmegaConfDumper(BaseDumper): # type: ignore str_representer_added = False @staticmethod - def str_representer(dumper: yaml.Dumper, data: str) -> yaml.ScalarNode: + def str_representer(dumper: BaseDumper, data: str) -> yaml.ScalarNode: with_quotes = yaml_is_bool(data) or is_int(data) or is_float(data) return dumper.represent_scalar( yaml.resolver.BaseResolver.DEFAULT_SCALAR_TAG, From fc33a80f78ef2d1a779cba93d27017a740b6cf7a Mon Sep 17 00:00:00 2001 From: Wouter Zwerink <35296208+wouterzwerink@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:12:13 +0100 Subject: [PATCH 2/5] Create 1152.feature --- news/1152.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/1152.feature diff --git a/news/1152.feature b/news/1152.feature new file mode 100644 index 000000000..72aaff48c --- /dev/null +++ b/news/1152.feature @@ -0,0 +1 @@ +The YAML dumper will now use `yaml.CDumper` instead of `yaml.Dumper` whenever possible to speed up dumping From a9490a84e23ae2bf1647212b9a957e12bfb43a39 Mon Sep 17 00:00:00 2001 From: Wouter Zwerink <35296208+wouterzwerink@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:29:23 +0100 Subject: [PATCH 3/5] Attempt to make mypy happy --- omegaconf/_utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/omegaconf/_utils.py b/omegaconf/_utils.py index d3ba58de8..ef34e951a 100644 --- a/omegaconf/_utils.py +++ b/omegaconf/_utils.py @@ -45,8 +45,10 @@ from yaml import CDumper BaseDumper = CDumper + BaseDumperType: Type[CDumper] = type(CDumper) except ImportError: # pragma: no cover BaseDumper = yaml.Dumper + BaseDumperType: Type[yaml.Dumper] = type(yaml.Dumper) NoneType: Type[None] = type(None) @@ -112,11 +114,11 @@ def __repr__(self) -> str: _DEFAULT_MARKER_: Any = Marker("_DEFAULT_MARKER_") -class OmegaConfDumper(BaseDumper): # type: ignore +class OmegaConfDumper(BaseDumperType): # type: ignore str_representer_added = False @staticmethod - def str_representer(dumper: BaseDumper, data: str) -> yaml.ScalarNode: + def str_representer(dumper: BaseDumperType, data: str) -> yaml.ScalarNode: with_quotes = yaml_is_bool(data) or is_int(data) or is_float(data) return dumper.represent_scalar( yaml.resolver.BaseResolver.DEFAULT_SCALAR_TAG, From 981cbb51cd817cf19d928825fe30d8d2f1b3111e Mon Sep 17 00:00:00 2001 From: Wouter Zwerink <35296208+wouterzwerink@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:41:57 +0100 Subject: [PATCH 4/5] Revert mistake --- omegaconf/_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/omegaconf/_utils.py b/omegaconf/_utils.py index ef34e951a..95942cc2a 100644 --- a/omegaconf/_utils.py +++ b/omegaconf/_utils.py @@ -114,7 +114,7 @@ def __repr__(self) -> str: _DEFAULT_MARKER_: Any = Marker("_DEFAULT_MARKER_") -class OmegaConfDumper(BaseDumperType): # type: ignore +class OmegaConfDumper(BaseDumper): # type: ignore str_representer_added = False @staticmethod From 21b34e4cb9ee03a27b7805d63971738907c49d0e Mon Sep 17 00:00:00 2001 From: Wouter Zwerink <35296208+wouterzwerink@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:54:03 +0100 Subject: [PATCH 5/5] Simplify --- omegaconf/_utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/omegaconf/_utils.py b/omegaconf/_utils.py index 95942cc2a..2575f0452 100644 --- a/omegaconf/_utils.py +++ b/omegaconf/_utils.py @@ -45,10 +45,8 @@ from yaml import CDumper BaseDumper = CDumper - BaseDumperType: Type[CDumper] = type(CDumper) except ImportError: # pragma: no cover BaseDumper = yaml.Dumper - BaseDumperType: Type[yaml.Dumper] = type(yaml.Dumper) NoneType: Type[None] = type(None) @@ -118,7 +116,7 @@ class OmegaConfDumper(BaseDumper): # type: ignore str_representer_added = False @staticmethod - def str_representer(dumper: BaseDumperType, data: str) -> yaml.ScalarNode: + def str_representer(dumper: yaml.Dumper, data: str) -> yaml.ScalarNode: with_quotes = yaml_is_bool(data) or is_int(data) or is_float(data) return dumper.represent_scalar( yaml.resolver.BaseResolver.DEFAULT_SCALAR_TAG,