While trying to figure out if a function is called with the @decorator syntax, we realized that inspect has a different behaviour when looking at a decorated class that inherits from a superclass.
The following behaviour was found with CPython 3.6.2 under Windows 10.
It was also reproduced in CPython 3.7.0 under Linux 64 bits.
import inspect
def decorate(f):
lines = inspect.stack()[1].code_context
print(f.__name__, lines)
return f
@decorate
class Foo:
pass
@decorate
class Bar(dict):
pass
Output
Foo ['@decorate\n']
Bar ['class Bar(dict):\n']
Why does inheritance change the behaviour of inspect?