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

fix unnecessary explicit instantiations #543

Merged
merged 1 commit into from
Jan 14, 2025
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
2 changes: 1 addition & 1 deletion pyiron_workflow/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ def __call__(self, other: OutputSignal) -> None:
self.received_signals.update([other.scoped_label])
if (
len(
set(c.scoped_label for c in self.connections).difference(
{c.scoped_label for c in self.connections}.difference(
self.received_signals
)
)
Expand Down
14 changes: 6 additions & 8 deletions pyiron_workflow/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ def __setitem__(self, key, value):
def connections(self) -> list[Channel]:
"""All the unique connections across all channels"""
return list(
set([connection for channel in self for connection in channel.connections])
{connection for channel in self for connection in channel.connections}
)

@property
def connected(self):
return any([c.connected for c in self])
return any(c.connected for c in self)

@property
def fully_connected(self):
return all([c.connected for c in self])
return all(c.connected for c in self)

def disconnect(self) -> list[tuple[Channel, Channel]]:
"""
Expand Down Expand Up @@ -182,11 +182,11 @@ def to_value_dict(self):

def to_list(self):
"""A list of channel values (order not guaranteed)"""
return list(channel.value for channel in self.channel_dict.values())
return [channel.value for channel in self.channel_dict.values()]

@property
def ready(self):
return all([c.ready for c in self])
return all(c.ready for c in self)

def activate_strict_hints(self):
[c.activate_strict_hints() for c in self]
Expand Down Expand Up @@ -400,9 +400,7 @@ def set_input_values(self, *args, **kwargs) -> None:
f"Received {len(args)} args, but only have {len(self.inputs.labels)} "
f"input channels available"
)
keyed_args = {
label: value for label, value in zip(self.inputs.labels, args, strict=False)
}
keyed_args = dict(zip(self.inputs.labels, args, strict=False))

if len(set(keyed_args.keys()).intersection(kwargs.keys())) > 0:
raise ValueError(
Expand Down
2 changes: 1 addition & 1 deletion pyiron_workflow/mixin/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def _build_outputs_preview(cls):
type_hints = [None] * len(labels)
# Note that this nicely differs from `NoneType`, which is the hint when
# `None` is actually the hint!
return {label: hint for label, hint in zip(labels, type_hints, strict=False)}
return dict(zip(labels, type_hints, strict=False))

@classmethod
def _get_output_labels(cls):
Expand Down
2 changes: 1 addition & 1 deletion pyiron_workflow/nodes/for_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def dictionary_to_index_maps(
nested_data_lengths = (
[]
if (nested_keys is None or len(nested_keys) == 0)
else list(len(data[key]) for key in nested_keys)
else [len(data[key]) for key in nested_keys]
)
except TypeError as e:
raise TypeError(
Expand Down
4 changes: 2 additions & 2 deletions pyiron_workflow/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def _set_run_connections_according_to_dag(nodes: dict[str, Node]) -> list[Node]:

for node in nodes.values():
upstream_connections = [con for inp in node.inputs for con in inp.connections]
upstream_nodes = set([c.owner for c in upstream_connections])
upstream_nodes = {c.owner for c in upstream_connections}
upstream_rans = [n.signals.output.ran for n in upstream_nodes]
node.signals.input.accumulate_and_run.connect(*upstream_rans)
# Note: We can be super fast-and-loose here because the `nodes_to_data_digraph` call
Expand Down Expand Up @@ -225,7 +225,7 @@ def get_nodes_in_data_tree(node: Node) -> set[Node]:
Get a set of all nodes from this one and upstream through data connections.
"""
try:
nodes = set([node])
nodes = {node}
for channel in node.inputs:
for connection in channel.connections:
nodes = nodes.union(get_nodes_in_data_tree(connection.owner))
Expand Down
33 changes: 10 additions & 23 deletions pyiron_workflow/type_hinting.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,14 @@ def type_hint_to_tuple(type_hint) -> tuple:
def type_hint_is_as_or_more_specific_than(hint, other) -> bool:
hint_origin = typing.get_origin(hint)
other_origin = typing.get_origin(other)
if set([hint_origin, other_origin]) & set([types.UnionType, typing.Union]):
if {hint_origin, other_origin} & {types.UnionType, typing.Union}:
# If either hint is a union, turn both into tuples and call recursively
return all(
[
any(
[
type_hint_is_as_or_more_specific_than(h, o)
for o in type_hint_to_tuple(other)
]
)
for h in type_hint_to_tuple(hint)
]
any(
type_hint_is_as_or_more_specific_than(h, o)
for o in type_hint_to_tuple(other)
)
for h in type_hint_to_tuple(hint)
)
elif hint_origin is None and other_origin is None:
# Once both are raw classes, just do a subclass test
Expand All @@ -74,26 +70,17 @@ def type_hint_is_as_or_more_specific_than(hint, other) -> bool:
elif len(other_args) == len(hint_args):
# If they both specify arguments, they should be more specific 1:1
return all(
[
type_hint_is_as_or_more_specific_than(h, o)
for o, h in zip(other_args, hint_args, strict=False)
]
type_hint_is_as_or_more_specific_than(h, o)
for o, h in zip(other_args, hint_args, strict=False)
)
else:
# Otherwise they both specify but a mis-matching number of args
return False
else:
# Otherwise order doesn't matter so make sure the arguments are a subset
return all(
[
any(
[
type_hint_is_as_or_more_specific_than(h, o)
for o in other_args
]
)
for h in hint_args
]
any(type_hint_is_as_or_more_specific_than(h, o) for o in other_args)
for h in hint_args
)
else:
# Lastly, if they both have origins, but different ones, fail
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ select = [
"SIM",
# isort
"I",
# flake8-comprehensions
"C4",
]
ignore = ["E501"] #ignore line-length violations

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def test_failure(self):
f"written a recovery file, so after removing that the whole "
f"node directory for the workflow should be cleaned up."
f"Instead, {wf.as_path()} exists and has content "
f"{[f for f in wf.as_path().iterdir()] if wf.as_path().is_dir() else None}",
f"{list(wf.as_path().iterdir()) if wf.as_path().is_dir() else None}",
)


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_conversion(self):
)

def test_iteration(self):
self.assertTrue(all([c.label in self.input.labels for c in self.input]))
self.assertTrue(all(c.label in self.input.labels for c in self.input))

def test_connections_property(self):
self.assertEqual(
Expand Down
Loading