Skip to content

Commit 0b42c3d

Browse files
committed
[MLIR][Python] remove PyYAML as a dep
1 parent cc7e206 commit 0b42c3d

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

.ci/all_requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ ml-dtypes==0.5.1 ; python_version < "3.13" \
341341
nanobind==2.9.2 \
342342
--hash=sha256:c37957ffd5eac7eda349cff3622ecd32e5ee1244ecc912c99b5bc8188bafd16e \
343343
--hash=sha256:e7608472de99d375759814cab3e2c94aba3f9ec80e62cfef8ced495ca5c27d6e
344-
# via -r mlir/python/requirements.txt
344+
# via -r mlir/python/requirements-dev.txt
345345
numpy==2.0.2 \
346346
--hash=sha256:0123ffdaa88fa4ab64835dcbde75dcdf89c453c922f18dced6e27c90d1d0ec5a \
347347
--hash=sha256:11a76c372d1d37437857280aa142086476136a8c0f373b2e648ab2c8f18fb195 \
@@ -446,7 +446,7 @@ pyasn1-modules==0.4.2 \
446446
pybind11==2.13.6 \
447447
--hash=sha256:237c41e29157b962835d356b370ededd57594a26d5894a795960f0047cb5caf5 \
448448
--hash=sha256:ba6af10348c12b24e92fa086b39cfba0eff619b61ac77c406167d813b096d39a
449-
# via -r mlir/python/requirements.txt
449+
# via -r mlir/python/requirements-dev.txt
450450
pycparser==2.23 \
451451
--hash=sha256:78816d4f24add8f10a06d6f05b4d424ad9e96cfebf68a4ddc99c65c0720d00c2 \
452452
--hash=sha256:e5c6e8d3fbad53479cab09ac03729e0a9faf2bee3db8208a550daf5af81a5934
@@ -540,7 +540,7 @@ pyyaml==6.0.1 \
540540
--hash=sha256:fca0e3a251908a499833aa292323f32437106001d436eca0e6e7833256674585 \
541541
--hash=sha256:fd1592b3fdf65fff2ad0004b5e363300ef59ced41c2e6b3a99d4089fa8c5435d \
542542
--hash=sha256:fd66fc5d0da6d9815ba2cebeb4205f95818ff4b79c3ebe268e75d961704af52f
543-
# via -r mlir/python/requirements.txt
543+
# via -r mlir/python/requirements-dev.txt
544544
requests==2.32.5 \
545545
--hash=sha256:2462f94637a34fd532264295e186976db0f5d453d1cdd31473c85a6a161affb6 \
546546
--hash=sha256:dbba0bac56e100853db0ea71b82b4dfd5fe2bf6d3754a8893c3af500cec7d7cf

mlir/python/mlir/dialects/linalg/opdsl/lang/yaml_helper.py

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,30 @@
44
"""YAML serialization is routed through here to centralize common logic."""
55

66
import sys
7+
import warnings
8+
9+
10+
def multiline_str_representer(dumper, data):
11+
if len(data.splitlines()) > 1:
12+
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")
13+
else:
14+
return dumper.represent_scalar("tag:yaml.org,2002:str", data)
15+
716

817
try:
9-
import yaml
18+
from yaml import YAMLObject, add_representer
19+
20+
add_representer(str, multiline_str_representer)
1021
except ModuleNotFoundError as e:
11-
raise ModuleNotFoundError(
12-
f"This tool requires PyYAML but it was not installed. "
13-
f"Recommend: {sys.executable} -m pip install PyYAML"
14-
) from e
22+
warnings.warn(
23+
"PyYAML is not installed; you will not be able to generate linalg yaml definitions."
24+
)
1525

16-
__all__ = [
17-
"yaml_dump",
18-
"yaml_dump_all",
19-
"YAMLObject",
20-
]
26+
class YAMLObject:
27+
pass
2128

2229

23-
class YAMLObject(yaml.YAMLObject):
30+
class YAMLObject(YAMLObject):
2431
@classmethod
2532
def to_yaml(cls, dumper, self):
2633
"""Default to a custom dictionary mapping."""
@@ -33,21 +40,34 @@ def as_linalg_yaml(self):
3340
return yaml_dump(self)
3441

3542

36-
def multiline_str_representer(dumper, data):
37-
if len(data.splitlines()) > 1:
38-
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")
39-
else:
40-
return dumper.represent_scalar("tag:yaml.org,2002:str", data)
43+
def yaml_dump(data, sort_keys=False, **kwargs):
44+
try:
45+
import yaml
4146

47+
return yaml.dump(data, sort_keys=sort_keys, **kwargs)
48+
except ModuleNotFoundError as e:
49+
raise ModuleNotFoundError(
50+
f"This tool requires PyYAML but it was not installed. "
51+
f"Recommend: {sys.executable} -m pip install PyYAML"
52+
) from e
4253

43-
yaml.add_representer(str, multiline_str_representer)
4454

55+
def yaml_dump_all(data, sort_keys=False, explicit_start=True, **kwargs):
56+
try:
57+
import yaml
4558

46-
def yaml_dump(data, sort_keys=False, **kwargs):
47-
return yaml.dump(data, sort_keys=sort_keys, **kwargs)
59+
return yaml.dump_all(
60+
data, sort_keys=sort_keys, explicit_start=explicit_start, **kwargs
61+
)
62+
except ModuleNotFoundError as e:
63+
raise ModuleNotFoundError(
64+
f"This tool requires PyYAML but it was not installed. "
65+
f"Recommend: {sys.executable} -m pip install PyYAML"
66+
) from e
4867

4968

50-
def yaml_dump_all(data, sort_keys=False, explicit_start=True, **kwargs):
51-
return yaml.dump_all(
52-
data, sort_keys=sort_keys, explicit_start=explicit_start, **kwargs
53-
)
69+
__all__ = [
70+
"yaml_dump",
71+
"yaml_dump_all",
72+
"YAMLObject",
73+
]

0 commit comments

Comments
 (0)