Skip to content

Commit 306b121

Browse files
committed
Use the identityHash of a TypeFunction's arguments in its lookup.
We can't rely on the (python) identity of two Function types being the same even if they are the same - this happens when we deserialize a type before we actually create it at the module level (which can happen in the compiler cache) in which case we can have two python objects representing the same type. At the moment we don't have a good way of deduplicating these, so instead, we need to make sure that we use type-identity-hash instead of python identity/hash in dictionaries involving types.
1 parent 888662f commit 306b121

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

typed_python/type_function.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
from types import FunctionType
1818
from typed_python.compiler.runtime_lock import runtimeLock
19-
from typed_python._types import Forward, ListOf, TupleOf, Dict, ConstDict, Class
19+
from typed_python._types import (
20+
Forward, ListOf, TupleOf, Dict, ConstDict, Class, identityHash
21+
)
2022
import typed_python
2123

2224

@@ -99,7 +101,7 @@ def buildType(*args, **kwargs):
99101
args = tuple(mapArg(a) for a in args)
100102
kwargs = tuple(sorted([(k, mapArg(v)) for k, v in kwargs.items()]))
101103

102-
key = (args, kwargs)
104+
key = identityHash((args, kwargs))
103105

104106
if key in _memoForKey:
105107
res = _memoForKey[key]
@@ -121,7 +123,7 @@ def buildType(*args, **kwargs):
121123
forward = Forward(nameFor(args, kwargs))
122124

123125
_memoForKey[key] = forward
124-
_type_to_typefunction[forward] = (TypeFunction_, key)
126+
_type_to_typefunction[forward] = (TypeFunction_, (args, kwargs))
125127

126128
try:
127129
resultType = f(*args, **dict(kwargs))
@@ -131,7 +133,7 @@ def buildType(*args, **kwargs):
131133
_type_to_typefunction.pop(forward)
132134

133135
if resultType not in _type_to_typefunction:
134-
_type_to_typefunction[resultType] = (TypeFunction_, key)
136+
_type_to_typefunction[resultType] = (TypeFunction_, (args, kwargs))
135137

136138
_memoForKey[key] = resultType
137139

0 commit comments

Comments
 (0)