-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
register default deprecation warning filter. #8480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
warn user if (s)he use a deprecated feature interactively. Closes ipython#8478 In [1]: %run -i foo In [2]: foo() foo.py:4: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future print('x'*np.float64(3.5)) xxx In [3]: from foo import foo In [4]: foo() xxx
83b2cd8 to
a498028
Compare
|
Test added. Would @njsmith like to try ? And would that suit him. |
|
Looks reasonable to me. It is probably not quite right in some more obscure corner cases, but I'm not sure how much we should care about that. Specifically I am thinking of code run non-interactively through the InteractiveShell (does it exist? run_last_nodes takes an interactivity argument...), and InteractiveShells that are not using the main namespace. Do we care? (This is why I didn't submit a PR myself :-).) |
|
It is possible for code to be run non-interactively with InteractiveShell, but I think it is appropriate for any use of InteractiveShell to behave as if interactively executed. 👍 to this |
|
Yes you can run things with execute preprocessor, but then you still want stderr to be captured in the notebook (for the notebook case). So +1 on behaving the same. We can refine the behavior. I should add a note to the what's new. |
|
Hum, importing a deprecated module in main does not print the warning either... |
|
Side question, why in #6680 do we go for clearing the registry instead of "always" filters ? |
for i in range(100):
|
|
Ah, make sens ! |
Ah, this appears to be because the import system rewrite in CPython has totally screwed up module deprecation. A key feature of warnings is that they are attributed -- they are supposed to contain a pointer to the code that needs fixing. This is done via the Here the warning will be attributed to the last line of code, not to the body of This is crucial for our warnings filter -- we're requesting that we only see warnings that are attributed to code running in the So if you call # w.py
import sys
import warnings
import traceback
sys.stdout.write("Nominal call stack:\n")
traceback.print_stack()
sys.stdout.write("\nWarnings:\n")
for i in range(1, 12):
warnings.warn("At import, stacklevel %s" % (i,), stacklevel=i)
sys.stdout.write("\n")Output on python 2: so On python 3.3: So on python 3.3, if you want to correctly deprecate a module, you have to write And on python 3.4, it changed again: So basically the conclusion is that it's not possible to correctly deprecate a module on Python 3. Sweet! |
IPython/core/interactiveshell.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should instead say
module=self.user_ns.get("__name__")
?
|
Anyway, chatter aside, this LGTM except for the minor comment about using |
done. |
|
Waiting on CPython to choose a behavior. See mail thread https://mail.python.org/pipermail/python-ideas/2015-May/033740.html and bug on CPython http://bugs.python.org/issue24294 |
|
I'm 50/50 on whether we unhide PendingDeprecation. Based on Nick's comment, I might be slightly inclined to continue ignoring them, and only unhide actual deprecations. |
|
Only show deprecation warnings now. |
register default deprecation warning filter.
…o the user. Generating deprecation warnings with the correct stacklevel is tricky to get right. Up to now, users have not been seeing these deprecation warnigns because they had incorrect stacklevels. See python/cpython#68493 and python/cpython#67998 for good discussions, as well as ipython/ipython#8478 (comment) and ipython/ipython#8480 (comment) for more context. We see these issues directly. Normally stacklevel=2 might be enough to target the caller of a function. However, in our deprecation warning in the __init__ method, each level of subclasses adds one stack frame, so depending on the subclass, the appropriate stacklevel is different. Thus this PR implements a trick from a discussion in Python to calculate the first stack frame that is external to the ipywidgets library, and use that as the target stack frame for the deprecation warning.
…o the user. Generating deprecation warnings with the correct stacklevel is tricky to get right. Up to now, users have not been seeing these deprecation warnigns because they had incorrect stacklevels. See python/cpython#68493 and python/cpython#67998 for good discussions, as well as ipython/ipython#8478 (comment) and ipython/ipython#8480 (comment) for more context. We see these issues directly. Normally stacklevel=2 might be enough to target the caller of a function. However, in our deprecation warning in the __init__ method, each level of subclasses adds one stack frame, so depending on the subclass, the appropriate stacklevel is different. Thus this PR implements a trick from a discussion in Python to calculate the first stack frame that is external to the ipywidgets library, and use that as the target stack frame for the deprecation warning.
warn user if (s)he use a deprecated feature interactively.
Closes #8478
Not sure it is the exact right place though.