Skip to content

Commit

Permalink
Change non ascii dump to only affect materializers
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank995 committed Dec 18, 2024
1 parent 472269f commit 001adeb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/zenml/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ def handle_int_env_var(var: str, default: int = 0) -> int:
ENV_ZENML_PIPELINE_RUN_API_TOKEN_EXPIRATION = (
"ZENML_PIPELINE_API_TOKEN_EXPIRATION"
)
ENV_ZENML_ALLOW_NON_ASCII_MATERIALIZERS_DUMPS = "ZENML_ALLOW_NON_ASCII_MATERIALIZERS_DUMPS"

# Materializers environment variables
ENV_ZENML_MATERIALIAZERS_ALLOW_NON_ASCII_JSON_DUMPS = (
"ZENML_MATERIALIAZERS_ALLOW_NON_ASCII_JSON_DUMPS"
)

# ZenML Server environment variables
ENV_ZENML_SERVER_PREFIX = "ZENML_SERVER_"
Expand Down
18 changes: 15 additions & 3 deletions src/zenml/materializers/built_in_materializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
)

from zenml.artifact_stores.base_artifact_store import BaseArtifactStore
from zenml.constants import (
ENV_ZENML_MATERIALIAZERS_ALLOW_NON_ASCII_JSON_DUMPS,
handle_bool_env_var,
)
from zenml.enums import ArtifactType, VisualizationType
from zenml.logger import get_logger
from zenml.materializers.base_materializer import BaseMaterializer
Expand All @@ -48,7 +52,9 @@
str,
type(None),
) # complex/bytes are not JSON serializable

ZENML_MATERIALIAZERS_ALLOW_NON_ASCII_JSON_DUMPS = handle_bool_env_var(
ENV_ZENML_MATERIALIAZERS_ALLOW_NON_ASCII_JSON_DUMPS, False
)

class BuiltInMaterializer(BaseMaterializer):
"""Handle JSON-serializable basic types (`bool`, `float`, `int`, `str`)."""
Expand Down Expand Up @@ -94,7 +100,10 @@ def save(self, data: Union[bool, float, int, str]) -> None:
Args:
data: The data to store.
"""
yaml_utils.write_json(self.data_path, data)
yaml_utils.write_json(
self.data_path, data,
ensure_ascii=not ZENML_MATERIALIAZERS_ALLOW_NON_ASCII_JSON_DUMPS
)

def extract_metadata(
self, data: Union[bool, float, int, str]
Expand Down Expand Up @@ -371,7 +380,10 @@ def save(self, data: Any) -> None:

# If the data is serializable, just write it into a single JSON file.
if _is_serializable(data):
yaml_utils.write_json(self.data_path, data)
yaml_utils.write_json(
self.data_path, data,
ensure_ascii=not ZENML_MATERIALIAZERS_ALLOW_NON_ASCII_JSON_DUMPS
)
return

# non-serializable dict: Handle as non-serializable list of lists.
Expand Down
2 changes: 2 additions & 0 deletions src/zenml/utils/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def write_json(
file_path: str,
contents: Any,
encoder: Optional[Type[json.JSONEncoder]] = None,
**json_dump_args
) -> None:
"""Write contents as JSON format to file_path.
Expand All @@ -143,6 +144,7 @@ def write_json(
json.dumps(
contents,
cls=encoder,
**json_dump_args
),
)

Expand Down

0 comments on commit 001adeb

Please sign in to comment.