##// END OF EJS Templates
Merge pull request #8480 from Carreau/interactive-deprecation-warning...
Min RK -
r21415:3a35a5e6 merge
parent child Browse files
Show More
@@ -0,0 +1,4
1 Code raising ``DeprecationWarning``
2 entered by the user in an interactive session will now display the warning by
3 default. See :ghpull:`8480` an :ghissue:`8478`.
4
@@ -25,6 +25,7 import tempfile
25 import traceback
25 import traceback
26 import types
26 import types
27 import subprocess
27 import subprocess
28 import warnings
28 from io import open as io_open
29 from io import open as io_open
29
30
30 from pickleshare import PickleShareDB
31 from pickleshare import PickleShareDB
@@ -554,6 +555,7 class InteractiveShell(SingletonConfigurable):
554 self.init_pdb()
555 self.init_pdb()
555 self.init_extension_manager()
556 self.init_extension_manager()
556 self.init_payload()
557 self.init_payload()
558 self.init_deprecation_warnings()
557 self.hooks.late_startup_hook()
559 self.hooks.late_startup_hook()
558 self.events.trigger('shell_initialized', self)
560 self.events.trigger('shell_initialized', self)
559 atexit.register(self.atexit_operations)
561 atexit.register(self.atexit_operations)
@@ -670,6 +672,15 class InteractiveShell(SingletonConfigurable):
670 elif self.logstart:
672 elif self.logstart:
671 self.magic('logstart')
673 self.magic('logstart')
672
674
675 def init_deprecation_warnings(self):
676 """
677 register default filter for deprecation warning.
678
679 This will allow deprecation warning of function used interactively to show
680 warning to users, and still hide deprecation warning from libraries import.
681 """
682 warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__"))
683
673 def init_builtins(self):
684 def init_builtins(self):
674 # A single, static flag that we set to True. Its presence indicates
685 # A single, static flag that we set to True. Its presence indicates
675 # that an IPython shell has been created, and we make no attempts at
686 # that an IPython shell has been created, and we make no attempts at
@@ -901,3 +901,44 def test_warning_suppression():
901 ip.run_cell("warnings.warn('asdf')")
901 ip.run_cell("warnings.warn('asdf')")
902 finally:
902 finally:
903 ip.run_cell("del warnings")
903 ip.run_cell("del warnings")
904
905
906 def test_deprecation_warning():
907 ip.run_cell("""
908 import warnings
909 def wrn():
910 warnings.warn(
911 "I AM A WARNING",
912 DeprecationWarning
913 )
914 """)
915 try:
916 with tt.AssertPrints("I AM A WARNING", channel="stderr"):
917 ip.run_cell("wrn()")
918 finally:
919 ip.run_cell("del warnings")
920 ip.run_cell("del wrn")
921
922
923 class TestImportNoDeprecate(tt.TempFileMixin):
924
925 def setup(self):
926 """Make a valid python temp file."""
927 self.mktmp("""
928 import warnings
929 def wrn():
930 warnings.warn(
931 "I AM A WARNING",
932 DeprecationWarning
933 )
934 """)
935
936 def test_no_dep(self):
937 """
938 No deprecation warning should be raised from imported functions
939 """
940 ip.run_cell("from {} import wrn".format(self.fname))
941
942 with tt.AssertNotPrints("I AM A WARNING"):
943 ip.run_cell("wrn()")
944 ip.run_cell("del wrn")
General Comments 0
You need to be logged in to leave comments. Login now