Skip to content

Commit 3db7344

Browse files
authored
Includes OrderedDict objects in the signature (#144)
* changes * changes * docs
1 parent b4135c9 commit 3db7344

File tree

8 files changed

+66
-8
lines changed

8 files changed

+66
-8
lines changed

dds/_introspect_indirect.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def _introspect(
6868

6969

7070
def _introspect_class(
71-
c: type, gctx: EvalMainContext, call_stack: List[CanonicalPath],
71+
c: type, gctx: EvalMainContext, call_stack: List[CanonicalPath], debug: bool = False
7272
) -> FunctionIndirectInteractions:
7373
# Check if the function has already been evaluated.
7474
fun_path = function_path(c)
@@ -93,7 +93,8 @@ def _introspect_class(
9393
ast_src = ast.parse(src)
9494
ast_f: ast.ClassDef = ast_src.body[0] # type: ignore
9595
assert isinstance(ast_f, ast.ClassDef), type(ast_f)
96-
_logger.debug(f"_introspect ast_src:\n {pformat(ast_f)}")
96+
if debug:
97+
_logger.debug(f"_introspect ast_src:\n {pformat(ast_f)}")
9798

9899
# For each of the functions in the body, look for interactions.
99100
fiis = InspectFunctionIndirect.inspect_class(

dds/_retrieve_objects.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import logging
77
import pathlib
88
import typing
9+
from collections import OrderedDict
910
from pathlib import PurePosixPath
1011
from types import ModuleType, FunctionType
1112
from typing import (
@@ -59,7 +60,7 @@ def _is_authorized_type(tpe: Type[Any], gctx: EvalMainContext) -> bool:
5960
# Some specific structural types are more complex and can be user-controlled.
6061
if get_option(accept_list_option) and tpe in (list,):
6162
return True
62-
if get_option(accept_dict_option) and tpe in (dict,):
63+
if get_option(accept_dict_option) and tpe in (dict, OrderedDict):
6364
return True
6465
if issubclass(tpe, object):
6566
mod = inspect.getmodule(tpe)

dds/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = "0.12.0"
1+
version = "0.12.1"

dds/fun_args.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ def _dds_hash0(elt: Any) -> PyHash:
126126
if isinstance(elt, OrderedDict):
127127
check_len(elt)
128128
# Directly using the ordering of the items in the dictionary.
129-
return _dds_hash(
130-
[name + "|" + _dds_hash(v, name) for (name, v) in elt.items()], None
131-
)
129+
return _dds_hash([_hash_dict_tuple(k, v) for (k, v) in elt.items()], None)
132130
if isinstance(elt, dict):
133131
# Starting from python 3.7 the order of the keys in a dictionary is the order of insertion
134132
# This code should return reliable results for CPython 3.6 and python 3.7+ (i.e. the overwhelming

dds/introspect.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def _introspect_class(
130130
arg_ctx: FunctionArgContext,
131131
gctx: EvalMainContext,
132132
call_stack: List[CanonicalPath],
133+
debug: bool = False,
133134
) -> FunctionInteractions:
134135
# Check if the function has already been evaluated.
135136
fun_path = function_path(c)
@@ -153,7 +154,8 @@ def _introspect_class(
153154
ast_src = ast.parse(src)
154155
ast_f: ast.ClassDef = ast_src.body[0] # type: ignore
155156
assert isinstance(ast_f, ast.ClassDef), type(ast_f)
156-
_logger.debug(f"_introspect ast_src:\n {pformat(ast_f)}")
157+
if debug:
158+
_logger.debug(f"_introspect ast_src:\n {pformat(ast_f)}")
157159
body_lines = src.split("\n")
158160

159161
# For each of the functions in the body, look for interactions.

dds_tests/test_mod/structures.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import dds
2+
from ..utils import Counter
3+
4+
5+
c = Counter()
6+
7+
l = [1]
8+
9+
10+
@dds.data_function("/p")
11+
def f1():
12+
c.increment()
13+
return len(l)

dds_tests/test_structures.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import dds
22
import pytest
3+
from collections import OrderedDict
34
from .utils import cleandir, Counter
5+
from .test_mod import structures
46

57
_ = cleandir
68

@@ -49,3 +51,38 @@ def test_gh140_dict():
4951
d[0] = 1
5052
f2()
5153
assert _c.value == 2
54+
55+
56+
@pytest.mark.usefixtures("cleandir")
57+
def test_gh140_list():
58+
structures.c.reset()
59+
structures.f1()
60+
assert structures.c.value == 1
61+
structures.l[0] = 2
62+
structures.f1()
63+
assert structures.c.value == 2
64+
structures.l[0] = 1
65+
structures.f1()
66+
assert structures.c.value == 2
67+
68+
69+
od = OrderedDict({0: 1})
70+
71+
72+
@dds.data_function("/p")
73+
def f3():
74+
_c.increment()
75+
return len(od)
76+
77+
78+
@pytest.mark.usefixtures("cleandir")
79+
def test_gh140_ordereddict():
80+
_c.reset()
81+
f3()
82+
assert _c.value == 1
83+
od[0] = 2
84+
f3()
85+
assert _c.value == 2
86+
od[0] = 1
87+
f3()
88+
assert _c.value == 2

doc_source/changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v0.12.1
4+
5+
Minor relase. Removes verbose logs and ensures that OrderedDict objects are
6+
also considered in the signature.
7+
8+
39
## v0.12.0
410

511
This is a major release. It adds hashing of lists and dictionaries into the

0 commit comments

Comments
 (0)