Skip to content

Commit

Permalink
Tweak type tests for happens_after
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Sep 9, 2024
1 parent b9c5eae commit 1bd25d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
15 changes: 8 additions & 7 deletions loopy/kernel/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ def __init__(self,
raise LoopyError("Setting depends_on_is_final to True requires "
"actually specifying happens_after/depends_on")

if happens_after is None:
if isinstance(happens_after, immutabledict):
pass
elif happens_after is None:
happens_after = immutabledict()
elif isinstance(happens_after, str):
warn("Passing a string for happens_after/depends_on is deprecated and "
Expand All @@ -338,9 +340,8 @@ def __init__(self,
variable_name=None,
instances_rel=None)
for after_id in happens_after})
elif isinstance(happens_after, MappingABC):
if isinstance(happens_after, dict):
happens_after = immutabledict(happens_after)
elif isinstance(happens_after, dict):
happens_after = immutabledict(happens_after)
else:
raise TypeError("'happens_after' has unexpected type: "
f"{type(happens_after)}")
Expand Down Expand Up @@ -394,9 +395,9 @@ def __init__(self,
assert isinstance(happens_after, MappingABC) or happens_after is None
assert isinstance(groups, abc_Set)
assert isinstance(conflicts_with_groups, abc_Set)
if isinstance(happens_after, MappingABC):
# Verify that happens_after is hashable.
assert not isinstance(happens_after, MutableMappingABC)

from loopy.tools import is_hashable
assert is_hashable(happens_after)

ImmutableRecord.__init__(self,
id=id,
Expand Down
9 changes: 9 additions & 0 deletions loopy/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,4 +972,13 @@ def _get_persistent_hashable_arg(arg):

# }}}


def is_hashable(o: object) -> bool:
try:
hash(o)
except TypeError:
return False
return True


# vim: fdm=marker

0 comments on commit 1bd25d0

Please sign in to comment.