Skip to content

Commit

Permalink
[patch] Replace warnings.warn with pyiron_snippets logger (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamhuber committed Jun 11, 2024
1 parent 3fde08a commit 612b4ee
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 23 deletions.
1 change: 1 addition & 0 deletions pyiron_workflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from pyiron_workflow.nodes import standard as standard_nodes
from pyiron_workflow.nodes.for_loop import for_node, for_node_factory
from pyiron_workflow.nodes.function import Function, as_function_node, function_node
from pyiron_workflow.logging import logger
from pyiron_workflow.nodes.macro import Macro, as_macro_node, macro_node
from pyiron_workflow.nodes.transform import (
# as_dataclass_node, # Not pickling nicely yet
Expand Down
2 changes: 1 addition & 1 deletion pyiron_workflow/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from abc import ABC, abstractmethod
from typing import Any

from pyiron_snippets.logger import logger
from pyiron_snippets.dotdict import DotDict

from pyiron_workflow.channels import (
Expand All @@ -24,6 +23,7 @@
AccumulatingInputSignal,
NOT_DATA,
)
from pyiron_workflow.logging import logger
from pyiron_workflow.mixin.has_interface_mixins import (
HasChannel,
HasLabel,
Expand Down
3 changes: 3 additions & 0 deletions pyiron_workflow/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from pyiron_snippets.logger import logger

# For now, we use the snippets logger with absolutely zero changes
16 changes: 7 additions & 9 deletions pyiron_workflow/mixin/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from __future__ import annotations

import inspect
import warnings
from abc import ABC, abstractmethod
from functools import lru_cache, wraps
from typing import (
Expand All @@ -27,6 +26,7 @@
from pyiron_snippets.dotdict import DotDict

from pyiron_workflow.channels import NOT_DATA
from pyiron_workflow.logging import logger
from pyiron_workflow.output_parser import ParseOutput

if TYPE_CHECKING:
Expand Down Expand Up @@ -252,12 +252,7 @@ def _validate(cls):
cls._validate_degeneracy()
cls._validate_return_count()
except OSError:
warnings.warn(
f"Could not find the source code to validate {cls.__name__} output "
f"labels against the number of returned values -- proceeding without "
f"validation",
OutputLabelsNotValidated,
)
logger.warn(no_output_validation_warning(cls))

@classmethod
def _validate_degeneracy(cls):
Expand Down Expand Up @@ -292,5 +287,8 @@ def _validate_return_count(cls):
)


class OutputLabelsNotValidated(Warning):
pass
def no_output_validation_warning(cls: type):
return (
f"Could not find the source code to validate {cls.__name__} output labels "
f"against the number of returned values -- proceeding without validation"
)
2 changes: 1 addition & 1 deletion pyiron_workflow/mixin/semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from typing import Optional

from bidict import bidict
from pyiron_snippets.logger import logger

from pyiron_workflow.logging import logger
from pyiron_workflow.mixin.has_interface_mixins import HasLabel, HasParent, UsesState


Expand Down
4 changes: 2 additions & 2 deletions pyiron_workflow/mixin/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import os
import sys
from typing import Optional
import warnings

import h5io
from pyiron_snippets.files import FileObject, DirectoryObject

from pyiron_workflow.logging import logger
from pyiron_workflow.mixin.has_interface_mixins import HasLabel, HasParent


Expand Down Expand Up @@ -347,7 +347,7 @@ def import_ready(self) -> bool:
module = self.__class__.__module__
class_ = getattr(import_module(module), self.__class__.__name__)
if module == "__main__":
warnings.warn(f"{self.label} is only defined in __main__")
logger.warning(f"{self.label} is only defined in __main__")
return type(self) is class_
except (ModuleNotFoundError, AttributeError):
return False
Expand Down
6 changes: 3 additions & 3 deletions pyiron_workflow/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from abc import ABC
from concurrent.futures import Future
from typing import Any, Literal, Optional, TYPE_CHECKING
import warnings

from pyiron_snippets.colors import SeabornColors

from pyiron_workflow.draw import Node as GraphvizNode
from pyiron_workflow.logging import logger
from pyiron_workflow.mixin.has_to_dict import HasToDict
from pyiron_workflow.mixin.injection import HasIOWithInjection
from pyiron_workflow.mixin.run import Runnable, ReadinessError
Expand Down Expand Up @@ -374,7 +374,7 @@ def _after_node_setup(
"label to work in a new space, or give up on running after init."
)
elif do_load:
warnings.warn(
logger.info(
f"A saved file was found for the node {self.label} -- attempting to "
f"load it...(To delete the saved file instead, use "
f"`overwrite_save=True`)"
Expand Down Expand Up @@ -781,7 +781,7 @@ def replace_with(self, other: Node | type[Node]):
if self.parent is not None:
self.parent.replace_child(self, other)
else:
warnings.warn(f"Could not replace_child {self.label}, as it has no parent.")
logger.info(f"Could not replace_child {self.label}, as it has no parent.")

@property
def storage_directory(self) -> DirectoryObject:
Expand Down
17 changes: 10 additions & 7 deletions tests/unit/mixin/test_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from pyiron_snippets.factory import classfactory

from pyiron_workflow.channels import NOT_DATA
from pyiron_workflow.mixin.preview import ScrapesIO, OutputLabelsNotValidated
from pyiron_workflow.logging import logger
from pyiron_workflow.mixin.preview import ScrapesIO, no_output_validation_warning


class ScrapesFromDecorated(ScrapesIO):
Expand Down Expand Up @@ -187,9 +188,11 @@ def __source_code_not_available(x):
):
as_scraper()(f)

with self.assertWarns(
OutputLabelsNotValidated,
msg="If provided labels cannot be validated against the source code, "
"a warning should be issued"
):
as_scraper("y")(f)
with self.assertLogs(logger.name, level="WARNING") as log:
new_cls = as_scraper("y")(f)

self.assertIn(
f"WARNING:{logger.name}:" + no_output_validation_warning(new_cls),
log.output,
msg="Verify that the expected warning appears in the log"
)

0 comments on commit 612b4ee

Please sign in to comment.