Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions IPython/core/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import traceback
import types
import subprocess
import warnings
from io import open as io_open

from pickleshare import PickleShareDB
Expand Down Expand Up @@ -547,6 +548,7 @@ def __init__(self, ipython_dir=None, profile_dir=None,
self.init_pdb()
self.init_extension_manager()
self.init_payload()
self.init_deprecation_warnings()
self.hooks.late_startup_hook()
self.events.trigger('shell_initialized', self)
atexit.register(self.atexit_operations)
Expand Down Expand Up @@ -663,6 +665,15 @@ def init_logstart(self):
elif self.logstart:
self.magic('logstart')

def init_deprecation_warnings(self):
"""
register default filter for deprecation warning.

This will allow deprecation warning of function used interactively to show
warning to users, and still hide deprecation warning from libraries import.
"""
warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__"))

def init_builtins(self):
# A single, static flag that we set to True. Its presence indicates
# that an IPython shell has been created, and we make no attempts at
Expand Down
41 changes: 41 additions & 0 deletions IPython/core/tests/test_interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,3 +901,44 @@ def test_warning_suppression():
ip.run_cell("warnings.warn('asdf')")
finally:
ip.run_cell("del warnings")


def test_deprecation_warning():
ip.run_cell("""
import warnings
def wrn():
warnings.warn(
"I AM A WARNING",
DeprecationWarning
)
""")
try:
with tt.AssertPrints("I AM A WARNING", channel="stderr"):
ip.run_cell("wrn()")
finally:
ip.run_cell("del warnings")
ip.run_cell("del wrn")


class TestImportNoDeprecate(tt.TempFileMixin):

def setup(self):
"""Make a valid python temp file."""
self.mktmp("""
import warnings
def wrn():
warnings.warn(
"I AM A WARNING",
DeprecationWarning
)
""")

def test_no_dep(self):
"""
No deprecation warning should be raised from imported functions
"""
ip.run_cell("from {} import wrn".format(self.fname))

with tt.AssertNotPrints("I AM A WARNING"):
ip.run_cell("wrn()")
ip.run_cell("del wrn")
4 changes: 4 additions & 0 deletions docs/source/whatsnew/pr/deprecation_warning.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Code raising ``DeprecationWarning``
entered by the user in an interactive session will now display the warning by
default. See :ghpull:`8480` an :ghissue:`8478`.