The equality of code objects is determined by the code_richcompare() logic in Objects/codeobject.c.
Two code objects are equal if all of their attributes compare equal. That includes co_name, co_argcount, co_kwonlyargcount, co_nlocals, co_flags, co_firstlineno, co_code, co_consts, co_names, co_varnames, co_freevars, and co_cellvars.
At the heart of David Murray's minimal example, the reason the two distinct code objects compare equal is that their co_consts compare as equal.
If you wanted to fix this, code objects would need to recursively check for both normal equality and type equality.
>>> f1 = lambda: 1
>>> f2 = lambda: 1.0
>>> f1.__code__.co_consts == f2.__code__.co_consts
True
>>> map(type, f1.__code__.co_consts) == map(type, f2.__code__.co_consts)
False |