-
Notifications
You must be signed in to change notification settings - Fork 459
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
schustmi
merged 4 commits into
develop
from
bugfix/prevent-extra-attributes-in-component-config
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
# 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"}}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At first, I thought that this line may overwrite the whole |
||
) | ||
configuration = validation_config_class(**configuration_dict) | ||
|
||
if not configuration.is_valid: | ||
raise ValueError( | ||
f"Invalid stack component configuration. Please verify " | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
toallow
, perhaps we can use a warning or a debug message here.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.