Current Situation
Right now, strictly_equal does not understand how to check if named closures are the same. Here's an example of such a closure:
defadd(first):
definner(second):
returnfirst+secondreturninnerincr=add(1)
assertincr(2) ==3
Here, while add(1) is not add(1) both produce the same behavior, IDOM won't recognize that they're the same.
Proposed Actions
It turns out that we can fairly reliable look at a function's __qualname__, __closure__, and __defaults__ to determine whether it's the same function. The logic to check this would look like:
deffunction_is_strictly_equal(f1, f2):
return (
f1.__qualname__==f2.__qualname__and"<lamba>"notinf1.__qualname__andall(strictly_equal(c1, c2) forc1, c2inzip(f1.__closure__, f2.__closure__))
andall(strictly_equal(c1, c2) forc1, c2inzip(f1.__defaults__, f2.__defaults__))
)
The catch here is that technically, a user could do the following:
defmake_closures(x):
defdo_something(y): ...
do_something_else=do_somethingdefdo_something(z): ...
returndo_something, do_something_elsef1, f2=make_closures()
assertnotfunction_is_strictly_equal(f1, f2) # will fail
This will fail because both functions, while they may implement different logic, have the same qualname. This is basically the same reason that we cannot compare lambdas. Since they all have the same name.
There may be ways to work around this. For example, f1 and f1 were defined on different lines. You could check this using __code__.co_firstlineno.
Current Situation
Right now,
strictly_equaldoes not understand how to check if named closures are the same. Here's an example of such a closure:Here, while
add(1) is not add(1)both produce the same behavior, IDOM won't recognize that they're the same.Proposed Actions
It turns out that we can fairly reliable look at a function's
__qualname__,__closure__, and__defaults__to determine whether it's the same function. The logic to check this would look like:The catch here is that technically, a user could do the following:
This will fail because both functions, while they may implement different logic, have the same qualname. This is basically the same reason that we cannot compare lambdas. Since they all have the same name.
There may be ways to work around this. For example,
f1andf1were defined on different lines. You could check this using__code__.co_firstlineno.