You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
warnings.warn(stacklevel=2) is a longstanding idiom.
It broke in 3.3 because python itself added some additional infrastructure frames in between; now stacklevel should be 8 or 10 in some releases.
bpo-24305 adds a workaround for 3.5, to ignore internal frames -- defined as those which contain both 'importlib' and '_bootstrap' in filename.
I would prefer to see a supported hook, so that either the caller or the program setup could list other modules or packages to ignore when counting frames. That way, I could write
mycall(otherlib(foo))
and otherlib could ensure that the warning pointed to mycall, rather than to something internal to otherlib, even if otherlib were refactored to a different stack depth.
Ignoring just the import machinery would of course be a good default.
Activity
jimjjewett commentedon Sep 22, 2015
warnings.warn(stacklevel=2) is a longstanding idiom.
It broke in 3.3 because python itself added some additional infrastructure frames in between; now stacklevel should be 8 or 10 in some releases.
bpo-24305 adds a workaround for 3.5, to ignore internal frames -- defined as those which contain both 'importlib' and '_bootstrap' in filename.
I would prefer to see a supported hook, so that either the caller or the program setup could list other modules or packages to ignore when counting frames. That way, I could write
and otherlib could ensure that the warning pointed to mycall, rather than to something internal to otherlib, even if otherlib were refactored to a different stack depth.
Ignoring just the import machinery would of course be a good default.
jayvdb commentedon Dec 10, 2015
This looks a lot like the patch on http://bugs.python.org/issue850482 aka #39615
gpshead commentedon Jan 1, 2023
duplicate of #39615