[Mlir-commits] [mlir] [MLIR][Python] remove PyYAML as a dep (PR #169145)
Maksim Levental
llvmlistbot at llvm.org
Fri Nov 21 22:12:31 PST 2025
https://github.com/makslevental updated https://github.com/llvm/llvm-project/pull/169145
>From 18d16bbe731ef5ccd8d3e76329d84719ece35a48 Mon Sep 17 00:00:00 2001
From: Maksim Levental <maksim.levental at gmail.com>
Date: Sat, 22 Nov 2025 00:29:58 -0500
Subject: [PATCH] [MLIR][Python] remove PyYAML as a dep
---
.../dialects/linalg/opdsl/lang/yaml_helper.py | 66 ++++++++++++-------
1 file changed, 43 insertions(+), 23 deletions(-)
diff --git a/mlir/python/mlir/dialects/linalg/opdsl/lang/yaml_helper.py b/mlir/python/mlir/dialects/linalg/opdsl/lang/yaml_helper.py
index 1672656b3a1f8..f3ad92d782eff 100644
--- a/mlir/python/mlir/dialects/linalg/opdsl/lang/yaml_helper.py
+++ b/mlir/python/mlir/dialects/linalg/opdsl/lang/yaml_helper.py
@@ -4,23 +4,30 @@
"""YAML serialization is routed through here to centralize common logic."""
import sys
+import warnings
+
+
+def multiline_str_representer(dumper, data):
+ if len(data.splitlines()) > 1:
+ return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")
+ else:
+ return dumper.represent_scalar("tag:yaml.org,2002:str", data)
+
try:
- import yaml
+ from yaml import YAMLObject, add_representer
+
+ add_representer(str, multiline_str_representer)
except ModuleNotFoundError as e:
- raise ModuleNotFoundError(
- f"This tool requires PyYAML but it was not installed. "
- f"Recommend: {sys.executable} -m pip install PyYAML"
- ) from e
+ warnings.warn(
+ "PyYAML is not installed; you will not be able to generate linalg yaml definitions."
+ )
-__all__ = [
- "yaml_dump",
- "yaml_dump_all",
- "YAMLObject",
-]
+ class YAMLObject:
+ pass
-class YAMLObject(yaml.YAMLObject):
+class YAMLObject(YAMLObject):
@classmethod
def to_yaml(cls, dumper, self):
"""Default to a custom dictionary mapping."""
@@ -33,21 +40,34 @@ def as_linalg_yaml(self):
return yaml_dump(self)
-def multiline_str_representer(dumper, data):
- if len(data.splitlines()) > 1:
- return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")
- else:
- return dumper.represent_scalar("tag:yaml.org,2002:str", data)
+def yaml_dump(data, sort_keys=False, **kwargs):
+ try:
+ import yaml
+ return yaml.dump(data, sort_keys=sort_keys, **kwargs)
+ except ModuleNotFoundError as e:
+ raise ModuleNotFoundError(
+ f"This tool requires PyYAML but it was not installed. "
+ f"Recommend: {sys.executable} -m pip install PyYAML"
+ ) from e
-yaml.add_representer(str, multiline_str_representer)
+def yaml_dump_all(data, sort_keys=False, explicit_start=True, **kwargs):
+ try:
+ import yaml
-def yaml_dump(data, sort_keys=False, **kwargs):
- return yaml.dump(data, sort_keys=sort_keys, **kwargs)
+ return yaml.dump_all(
+ data, sort_keys=sort_keys, explicit_start=explicit_start, **kwargs
+ )
+ except ModuleNotFoundError as e:
+ raise ModuleNotFoundError(
+ f"This tool requires PyYAML but it was not installed. "
+ f"Recommend: {sys.executable} -m pip install PyYAML"
+ ) from e
-def yaml_dump_all(data, sort_keys=False, explicit_start=True, **kwargs):
- return yaml.dump_all(
- data, sort_keys=sort_keys, explicit_start=explicit_start, **kwargs
- )
+__all__ = [
+ "yaml_dump",
+ "yaml_dump_all",
+ "YAMLObject",
+]
More information about the Mlir-commits
mailing list