Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/zenml/config/docker_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pydantic import BaseModel, ConfigDict, Field, model_validator

from zenml.config.base_settings import BaseSettings
from zenml.logger import get_logger
from zenml.logger import get_logger, simple_warning_format
from zenml.utils import deprecation_utils
from zenml.utils.pydantic_utils import before_validator_handler

Expand Down Expand Up @@ -394,6 +394,38 @@ def _deprecate_replicate_local_environment_commands(
)
return self

@model_validator(mode="after")
def _docker_settings_usage_warnings(self) -> "DockerSettings":
"""Checks active environment and warns for potential issues.

Returns:
The validated settings values.
"""
import warnings

from zenml.client import Client
from zenml.orchestrators import ContainerizedOrchestrator

warning_tag = "non_containerized_orchestrator"

active_orchestrator = Client().active_stack.orchestrator

if (
isinstance(active_orchestrator, ContainerizedOrchestrator)
or warning_tag in _docker_settings_warnings_logged
):
return self

with simple_warning_format():
warnings.warn(
f"WARNING: You are specifying docker settings without a containerized orchestrator: "
f"{active_orchestrator.__class__.__name__}"
)

_docker_settings_warnings_logged.append(warning_tag)

return self

model_config = ConfigDict(
# public attributes are immutable
frozen=True,
Expand Down
45 changes: 44 additions & 1 deletion src/zenml/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import os
import re
import sys
import warnings
from contextlib import contextmanager
from contextvars import ContextVar
from typing import TYPE_CHECKING, Any, Dict
from typing import TYPE_CHECKING, Any, Dict, Generator, Optional, Type, Union

if TYPE_CHECKING:
from zenml.logging.step_logging import ArtifactStoreHandler
Expand Down Expand Up @@ -357,6 +359,9 @@ def init_logging() -> None:
for handler in root_logger.handlers
)

# logging capture warnings
logging.captureWarnings(True)

if not has_console_handler:
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(get_formatter())
Expand Down Expand Up @@ -393,3 +398,41 @@ def init_logging() -> None:
for logger_name in disabled_logger_names:
logging.getLogger(logger_name).setLevel(logging.WARNING)
logging.getLogger(logger_name).disabled = True


def simple_warning_formatter(
message: Union[Warning, str],
category: Type[Warning],
filename: str,
lineno: int,
line: Optional[str] = None,
) -> str:
"""Simplified warning formatter that ignores context and filename.

Args:
message : The warning instance or string to display.
category : The class of the warning (e.g., `UserWarning`).
filename : Name of the file where the warning occurred. (Ignored in this formatter.)
lineno : Line number where the warning occurred. (Ignored in this formatter.)
line : The line of source code to show. Defaults to None. (Ignored in this formatter.)

Returns:
Warning message.
"""
return f"{category.__name__}: {message}\n"


@contextmanager
def simple_warning_format() -> Generator[None, None, None]:
"""Warning utility: Simplify formatting for specific calls (Skip path rendering).

Yields:
Empty generator, used as context manager.
"""
original = warnings.formatwarning

warnings.formatwarning = simple_warning_formatter
try:
yield
finally:
warnings.formatwarning = original
33 changes: 32 additions & 1 deletion tests/unit/config/test_docker_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
import pytest
from pydantic import ValidationError

from zenml.config import DockerSettings
from zenml.config.docker_settings import (
DockerSettings,
_docker_settings_warnings_logged,
)
from zenml.orchestrators import ContainerizedOrchestrator


def test_build_skipping():
Expand All @@ -28,3 +32,30 @@ def test_build_skipping():
with does_not_raise():
DockerSettings(skip_build=False)
DockerSettings(skip_build=True, parent_image="my_parent_image")


def test_generated_warnings():
from zenml.client import Client

if isinstance(
Client().active_stack.orchestrator, ContainerizedOrchestrator
):
pytest.skip(reason="Check warning generation for local orchestrators")

if "non_containerized_orchestrator" in _docker_settings_warnings_logged:
_docker_settings_warnings_logged.remove(
"non_containerized_orchestrator"
)

with pytest.warns(UserWarning) as warning:
DockerSettings()

assert (
"You are specifying docker settings without a containerized orchestrator"
in str(warning[0].message)
)

with pytest.warns(None):
DockerSettings()

assert "non_containerized_orchestrator" in _docker_settings_warnings_logged
Loading