@@ -93,17 +93,69 @@ def func(a):
93
93
94
94
obj1 = FloatAggregator (func )
95
95
96
- @functools .wraps (func )
97
- def _func (a ):
98
- return func (a )
96
+ def decorator (func ):
97
+ @functools .wraps (func )
98
+ def _func (a ):
99
+ return func (a )
99
100
100
- obj2 = FloatAggregator (_func )
101
+ return _func
102
+
103
+ obj2 = FloatAggregator (decorator (func ))
101
104
102
105
assert custom_hash (obj1 ) != custom_hash (obj2 )
103
106
104
107
108
+ def test_hash_lambdas_same ():
109
+ def func (a , b ):
110
+ return np .mean (a ) + b
111
+
112
+ def func2 ():
113
+ return FloatAggregator (lambda a : func (a , 1 ))
114
+
115
+ obj1 = func2 ()
116
+ obj2 = func2 ()
117
+
118
+ assert custom_hash (obj1 ) == custom_hash (obj2 )
119
+
120
+
105
121
def test_hash_lambdas_different ():
122
+ # This is quite interesting, these two lambdas are different, as they have different names, as they are
123
+ # defined in the same scope. in the pevious test, where there was only on lambda defined, the names were the same
124
+ # hence the hash the same.
106
125
obj1 = FloatAggregator (lambda a : np .mean (a ))
107
- obj2 = FloatAggregator (lambda a : np .mean (a ))
126
+ obj2 = obj1
127
+ obj1 = FloatAggregator (lambda a : np .mean (a ))
128
+ assert custom_hash (obj1 ) != custom_hash (obj2 )
129
+
130
+
131
+ def test_hash_partials_same ():
132
+ def func (a , b ):
133
+ return np .mean (a ) + b
134
+
135
+ obj1 = FloatAggregator (functools .partial (func , b = 1 ))
136
+ obj2 = FloatAggregator (functools .partial (func , b = 1 ))
137
+
138
+ assert custom_hash (obj1 ) == custom_hash (obj2 )
139
+
140
+
141
+ def test_hash_partials_different ():
142
+ def func (a , b ):
143
+ return np .mean (a ) + b
144
+
145
+ obj1 = FloatAggregator (functools .partial (func , b = 1 ))
146
+ obj2 = FloatAggregator (functools .partial (func , b = 2 ))
147
+
148
+ assert custom_hash (obj1 ) != custom_hash (obj2 )
149
+
150
+
151
+ def test_hash_partials_different2 ():
152
+ def func (a , b ):
153
+ return np .mean (a ) + b
154
+
155
+ def func2 (a , b ):
156
+ return np .mean (a ) + b
157
+
158
+ obj1 = FloatAggregator (functools .partial (func , b = 1 ))
159
+ obj2 = FloatAggregator (functools .partial (func2 , b = 1 ))
108
160
109
161
assert custom_hash (obj1 ) != custom_hash (obj2 )
0 commit comments