Skip to content

Commit fa2929b

Browse files
committed
debugged bytes_repr for types for Py <3.9
1 parent ff06fb1 commit fa2929b

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

pydra/utils/hash.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -234,22 +234,21 @@ def bytes_repr_dict(obj: dict, cache: Cache) -> Iterator[bytes]:
234234
@register_serializer(ty._SpecialForm)
235235
@register_serializer(type)
236236
def bytes_repr_type(klass: type, cache: Cache) -> Iterator[bytes]:
237-
try:
238-
yield f"type:({klass.__module__}.{klass.__name__}".encode()
239-
except AttributeError:
240-
yield f"type:(typing.{klass._name}:(".encode() # type: ignore
241-
args = ty.get_args(klass)
242-
if args:
243-
244-
def sort_key(a):
245-
try:
246-
return a.__name__
247-
except AttributeError:
248-
return a._name
249-
250-
yield b"["
251-
yield from bytes_repr_sequence_contents(sorted(args, key=sort_key), cache)
237+
def type_name(tp):
238+
try:
239+
name = tp.__name__
240+
except AttributeError:
241+
name = tp._name
242+
return name
243+
244+
yield b"type:("
245+
origin = ty.get_origin(klass)
246+
if origin:
247+
yield f"{origin.__module__}.{type_name(origin)}[".encode()
248+
yield from bytes_repr_sequence_contents(ty.get_args(klass), cache)
252249
yield b"]"
250+
else:
251+
yield f"{klass.__module__}.{type_name(klass)}".encode()
253252
yield b")"
254253

255254

pydra/utils/tests/test_hash.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import attrs
66
import pytest
77
import typing as ty
8-
8+
from fileformats.application import Zip, Json
99
from ..hash import Cache, UnhashableError, bytes_repr, hash_object, register_serializer
1010

1111

@@ -172,6 +172,11 @@ def test_bytes_repr_type1():
172172
assert obj_repr == b"type:(pathlib.Path)"
173173

174174

175+
def test_bytes_repr_type1a():
176+
obj_repr = join_bytes_repr(Zip[Json])
177+
assert re.match(rb"type:\(fileformats.application.Zip\[.{16}\]\)", obj_repr)
178+
179+
175180
def test_bytes_repr_type2():
176181
T = ty.TypeVar("T")
177182

@@ -182,11 +187,32 @@ class MyClass(ty.Generic[T]):
182187
assert re.match(rb"type:\(pydra.utils.tests.test_hash.MyClass\[.{16}\]\)", obj_repr)
183188

184189

185-
def test_bytes_special_form():
190+
def test_bytes_special_form1():
186191
obj_repr = join_bytes_repr(ty.Union[int, float])
187192
assert re.match(rb"type:\(typing.Union\[.{32}\]\)", obj_repr)
188193

189194

195+
def test_bytes_special_form2():
196+
obj_repr = join_bytes_repr(ty.Any)
197+
assert re.match(rb"type:\(typing.Any\)", obj_repr)
198+
199+
200+
def test_bytes_special_form3():
201+
obj_repr = join_bytes_repr(ty.Optional[Path])
202+
assert re.match(rb"type:\(typing.Optional\[.{16}\]\)", obj_repr)
203+
204+
205+
def test_bytes_special_form4():
206+
obj_repr = join_bytes_repr(ty.Type[Path])
207+
assert re.match(rb"type:\(builtins.type\[.{16}\]\)", obj_repr)
208+
209+
210+
def test_bytes_special_form5():
211+
obj_repr = join_bytes_repr(ty.Callable[[Path, int], ty.Tuple[float, str]])
212+
assert re.match(rb"type:\(typing.Callable\[.{16}\]\)", obj_repr)
213+
assert obj_repr != join_bytes_repr(ty.Callable[[Path, int], ty.Tuple[float, bytes]])
214+
215+
190216
def test_recursive_object():
191217
a = []
192218
b = [a]

pydra/utils/tests/test_typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ...engine.specs import File, LazyOutField
99
from ..typing import TypeParser
1010
from pydra import Workflow
11-
from fileformats.serialization import Json
11+
from fileformats.application import Json
1212
from .utils import (
1313
generic_func_task,
1414
GenericShellTask,

0 commit comments

Comments
 (0)