Skip to content

Commit

Permalink
Use the identityHash of a TypeFunction's arguments in its lookup.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
braxtonmckee committed Nov 22, 2022
1 parent 888662f commit 306b121
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions typed_python/type_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

from types import FunctionType
from typed_python.compiler.runtime_lock import runtimeLock
from typed_python._types import Forward, ListOf, TupleOf, Dict, ConstDict, Class
from typed_python._types import (
Forward, ListOf, TupleOf, Dict, ConstDict, Class, identityHash
)
import typed_python


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

key = (args, kwargs)
key = identityHash((args, kwargs))

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

_memoForKey[key] = forward
_type_to_typefunction[forward] = (TypeFunction_, key)
_type_to_typefunction[forward] = (TypeFunction_, (args, kwargs))

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

if resultType not in _type_to_typefunction:
_type_to_typefunction[resultType] = (TypeFunction_, key)
_type_to_typefunction[resultType] = (TypeFunction_, (args, kwargs))

_memoForKey[key] = resultType

Expand Down

0 comments on commit 306b121

Please sign in to comment.