Skip to content

Commit

Permalink
enable ruff-pl checks with some exclusions (#549)
Browse files Browse the repository at this point in the history
* enable ruff-pl checks with some exclusions

* black

Signed-off-by: liamhuber <[email protected]>

* Ignore complaints about static method taking self

The method is static for the sake of dynamic subclassing; calling it self is very intentional here as it is the owning instance that gets passed in.

Signed-off-by: liamhuber <[email protected]>

* pylint: don't override local vars

Signed-off-by: liamhuber <[email protected]>

* pylint: don't override local var

Signed-off-by: liamhuber <[email protected]>

* Fix Node._run_args signature and hint

Signed-off-by: liamhuber <[email protected]>

* Make Macro.graph_creator a normal method

With accompanying change to applications/invocations, type hint narrowing, and docstrings

Signed-off-by: liamhuber <[email protected]>

---------

Signed-off-by: liamhuber <[email protected]>
Co-authored-by: liamhuber <[email protected]>
  • Loading branch information
XzzX and liamhuber authored Jan 23, 2025
1 parent c332bba commit b3c0289
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 24 deletions.
4 changes: 2 additions & 2 deletions pyiron_workflow/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ def _copy_connections(
except Exception as e:
if fail_hard:
# If you run into trouble, unwind what you've done
for this, other in new_connections:
this.disconnect(other)
for this, that in new_connections:
this.disconnect(that)
raise ConnectionCopyError(
f"{self.label} could not copy connections from "
f"{other.label} due to the channel {key} on "
Expand Down
2 changes: 1 addition & 1 deletion pyiron_workflow/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def run_args(self) -> tuple[tuple, dict]:

@property
@abstractmethod
def _run_args(self, *args, **kwargs) -> Any:
def _run_args(self) -> tuple[tuple, dict]:
pass

def run(
Expand Down
25 changes: 12 additions & 13 deletions pyiron_workflow/nodes/for_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,12 @@ def merge(d1, d2):
key_index_maps = tuple(
zipped_index_map(zipped_index) for zipped_index in zipped_generator()
)
elif nested_keys is None and zipped_keys is None:
raise ValueError(
"At least one of `nested_keys` or `zipped_keys` must be specified."
)
else:
if nested_keys is None and zipped_keys is None:
raise ValueError(
"At least one of `nested_keys` or `zipped_keys` must be specified."
)
else:
raise ValueError(
"Received keys to iterate over, but all values had length 0."
)
raise ValueError("Received keys to iterate over, but all values had length 0.")

return key_index_maps

Expand Down Expand Up @@ -376,9 +373,12 @@ def _build_inputs_preview(cls) -> dict[str, tuple[Any, Any]]:
for label, (hint, default) in cls._body_node_class.preview_inputs().items():
# TODO: Leverage hint and default, listing if it's looped on
if label in cls._zip_on + cls._iter_on:
hint = list if hint is None else list[hint] # type: ignore[valid-type]
default = NOT_DATA # TODO: Figure out a generator pattern to get lists
preview[label] = (hint, default)
preview[label] = (
list if hint is None else list[hint], # type: ignore[valid-type]
NOT_DATA, # TODO: Figure out a generator pattern to get lists
)
else:
preview[label] = (hint, default)
return preview

@classmethod
Expand All @@ -392,8 +392,7 @@ def _build_outputs_preview(cls) -> dict[str, Any]:
_default,
) in cls._body_node_class.preview_inputs().items():
if label in cls._zip_on + cls._iter_on:
hint = list if hint is None else list[hint] # type: ignore[valid-type]
preview[label] = hint
preview[label] = list if hint is None else list[hint] # type: ignore[valid-type]
for label, hint in cls._body_node_class.preview_outputs().items():
preview[cls.output_column_map()[label]] = (
list if hint is None else list[hint] # type: ignore[valid-type]
Expand Down
12 changes: 6 additions & 6 deletions pyiron_workflow/nodes/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from pyiron_snippets.factory import classfactory

from pyiron_workflow.compatibility import Self
from pyiron_workflow.io import Inputs
from pyiron_workflow.mixin.has_interface_mixins import HasChannel
from pyiron_workflow.mixin.injection import OutputsWithInjection
Expand Down Expand Up @@ -196,7 +197,6 @@ class Macro(Composite, StaticNode, ScrapesIO, ABC):
>>> class AddThreeMacro(Macro):
... _output_labels = ["three"]
...
... @staticmethod
... def graph_creator(self, x):
... add_three_macro(self, one__x=x)
... return self.three
Expand Down Expand Up @@ -252,7 +252,7 @@ def _setup_node(self) -> None:
super()._setup_node()

ui_nodes = self._prepopulate_ui_nodes_from_graph_creator_signature()
returned_has_channel_objects = self.graph_creator(self, *ui_nodes)
returned_has_channel_objects = self.graph_creator(*ui_nodes)
if returned_has_channel_objects is None:
returned_has_channel_objects = ()
elif isinstance(returned_has_channel_objects, HasChannel):
Expand All @@ -271,10 +271,9 @@ def _setup_node(self) -> None:
remaining_ui_nodes = self._purge_single_use_ui_nodes(ui_nodes)
self._configure_graph_execution(remaining_ui_nodes)

@staticmethod
@abstractmethod
def graph_creator(
self: Macro, *args, **kwargs
self: Self, *args, **kwargs
) -> HasChannel | tuple[HasChannel, ...] | None:
"""Build the graph the node will run."""

Expand Down Expand Up @@ -480,7 +479,8 @@ def macro_node_factory(
Create a new :class:`Macro` subclass using the given graph creator function.
Args:
graph_creator (callable): Function to create the graph for the :class:`Macro`.
graph_creator (callable): Function to create the graph for this subclass of
:class:`Macro`.
validate_output_labels (bool): Whether to validate the output labels against
the return values of the wrapped function.
use_cache (bool): Whether nodes of this type should default to caching their
Expand All @@ -495,7 +495,7 @@ def macro_node_factory(
graph_creator.__name__,
(Macro,), # Define parentage
{
"graph_creator": staticmethod(graph_creator),
"graph_creator": graph_creator,
"__module__": graph_creator.__module__,
"__qualname__": graph_creator.__qualname__,
"_output_labels": None if len(output_labels) == 0 else output_labels,
Expand Down
19 changes: 18 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,25 @@ select = [
"C4",
# eradicate
"ERA",
# pylint
"PL",
]
ignore = [
# ignore line-length violations
"E501",
# Too many arguments in function definition
"PLR0913",
# Magic value used in comparison
"PLR2004",
# Import alias does not rename original package
"PLC0414",
# Too many branches
"PLR0912",
# Too many statements
"PLR0915",
# Too many return statements
"PLR0911",
]
ignore = ["E501"] #ignore line-length violations

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"] # Ignore unused imports in init files -- we specify APIs this way
Expand Down
1 change: 0 additions & 1 deletion tests/unit/nodes/test_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ def test_creation_from_subclass(self):
class MyMacro(Macro):
_output_labels = ("three__result",)

@staticmethod
def graph_creator(self, one__x):
add_three_macro(self, one__x)
return self.three
Expand Down

0 comments on commit b3c0289

Please sign in to comment.