Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent extra attributes in component configs #2978

Merged
Merged
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
13 changes: 11 additions & 2 deletions src/zenml/stack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# permissions and limitations under the License.
"""Util functions for handling stacks, components, and flavors."""

from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, Type

from zenml.client import Client
from zenml.enums import StackComponentType, StoreType
Expand Down Expand Up @@ -75,7 +75,16 @@ def validate_stack_component_config(
return None
raise

configuration = flavor_class.config_class(**configuration_dict)
config_class = flavor_class.config_class
# Make sure extras are forbidden for the config class. Due to inheritance
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you mentioned in the description, for whatever reason, if a user sets the extra to allow, perhaps we can use a warning or a debug message here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is, I can't even detect that right? Whether someone did it explicitly or whether it happened due to inheritance of BaseSettings

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. Feel free to skip it.

# order, some config classes allow extras by accident which we patch here.
validation_config_class: Type[StackComponentConfig] = type(
config_class.__name__,
(config_class,),
{"model_config": {"extra": "forbid"}},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first, I thought that this line may overwrite the whole model_config but it actually updated it. Good one!

)
configuration = validation_config_class(**configuration_dict)

if not configuration.is_valid:
raise ValueError(
f"Invalid stack component configuration. Please verify "
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/stack/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) ZenML GmbH 2024. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing
# permissions and limitations under the License.

from contextlib import ExitStack as does_not_raise

import pytest
from pydantic import ValidationError

from tests.unit.conftest_new import empty_pipeline # noqa
from zenml.enums import StackComponentType
from zenml.orchestrators.local_docker.local_docker_orchestrator import (
LocalDockerOrchestratorConfig,
)
from zenml.stack.utils import validate_stack_component_config


def test_stack_component_validation_prevents_extras():
"""Tests that stack component validation prevents extra attributes."""
config_dict = {"not_a_valid_component_attribute": False}

with does_not_raise():
LocalDockerOrchestratorConfig(**config_dict)

with pytest.raises(ValidationError):
validate_stack_component_config(
config_dict,
flavor_name="local_docker",
component_type=StackComponentType.ORCHESTRATOR,
)
Loading