-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix capturing of ForwardModelStepWarning
- Loading branch information
1 parent
4bba695
commit 59eb4f3
Showing
4 changed files
with
115 additions
and
74 deletions.
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
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,37 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
from contextlib import contextmanager | ||
from dataclasses import dataclass, field | ||
from typing import Iterator, cast | ||
from warnings import catch_warnings | ||
|
||
from .parsing import ConfigValidationError, ConfigWarning, ErrorInfo, WarningInfo | ||
|
||
|
||
@dataclass | ||
class ValidationMessages: | ||
warnings: list[WarningInfo] = field(default_factory=list) | ||
deprecations: list[WarningInfo] = field(default_factory=list) | ||
errors: list[ErrorInfo] = field(default_factory=list) | ||
|
||
|
||
@contextmanager | ||
def capture_validation() -> Iterator[ValidationMessages]: | ||
logger = logging.getLogger(__name__) | ||
validations = ValidationMessages() | ||
with catch_warnings(record=True) as all_warnings: | ||
try: | ||
yield validations | ||
except ConfigValidationError as err: | ||
validations.errors += err.errors | ||
|
||
for wm in all_warnings: | ||
if issubclass(wm.category, ConfigWarning): | ||
warning = cast(ConfigWarning, wm.message) | ||
if warning.info.is_deprecation: | ||
validations.deprecations.append(warning.info) | ||
else: | ||
validations.warnings.append(warning.info) | ||
else: | ||
logger.warning(str(wm.message)) |
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
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,34 @@ | ||
from ert.config import ( | ||
ConfigValidationError, | ||
ConfigWarning, | ||
ForwardModelStepWarning, | ||
capture_validation, | ||
) | ||
|
||
|
||
def test_capture_validation_captures_warnings(): | ||
with capture_validation() as validation_messages: | ||
ConfigWarning.warn("Message") | ||
|
||
assert validation_messages.warnings[0].message == "Message" | ||
|
||
|
||
def test_capture_validation_captures_deprecations(): | ||
with capture_validation() as validation_messages: | ||
ConfigWarning.deprecation_warn("Message") | ||
|
||
assert validation_messages.deprecations[0].message == "Message" | ||
|
||
|
||
def test_capture_validation_captures_validation_errors(): | ||
with capture_validation() as validation_messages: | ||
raise ConfigValidationError("Message") | ||
|
||
assert validation_messages.errors[0].message == "Message" | ||
|
||
|
||
def test_capture_validation_captures_plugin_warnings(): | ||
with capture_validation() as validation_messages: | ||
ForwardModelStepWarning.warn("Message") | ||
|
||
assert validation_messages.warnings[0].message == "Message" |