From b518e6a546ef99e00c2a2fbdf35122ee341057aa 2023-06-02 08:37:11 From: Matthias Bussonnier Date: 2023-06-02 08:37:11 Subject: [PATCH] Add option to `%autoreload` to hide errors when reloading code (#14056) * We have `%autoreload` enabled by default in Spyder and think it's a bit annoying to show its error messages to users because they make little sense to them. These errors are not uncommon when you are working with some code that is slightly different between two git branches. * However, I didn't change the current behavior (i.e. showing errors) because it's been like that for as long as I can remember. We'd simply use the new `--hide-errors` option in our kernel. --- diff --git a/IPython/extensions/autoreload.py b/IPython/extensions/autoreload.py index cb63a55..0025ad5 100644 --- a/IPython/extensions/autoreload.py +++ b/IPython/extensions/autoreload.py @@ -170,6 +170,9 @@ class ModuleReloader: # Cache module modification times self.check(check_all=True, do_reload=False) + # To hide autoreload errors + self.hide_errors = False + def mark_module_skipped(self, module_name): """Skip reloading the named module in the future""" try: @@ -274,12 +277,13 @@ class ModuleReloader: if py_filename in self.failed: del self.failed[py_filename] except: - print( - "[autoreload of {} failed: {}]".format( - modname, traceback.format_exc(10) - ), - file=sys.stderr, - ) + if not self.hide_errors: + print( + "[autoreload of {} failed: {}]".format( + modname, traceback.format_exc(10) + ), + file=sys.stderr, + ) self.failed[py_filename] = pymtime @@ -553,6 +557,12 @@ class AutoreloadMagics(Magics): default=False, help="Show autoreload activity using the logger", ) + @magic_arguments.argument( + "--hide-errors", + action="store_true", + default=False, + help="Hide autoreload errors", + ) def autoreload(self, line=""): r"""%autoreload => Reload modules automatically @@ -579,6 +589,9 @@ class AutoreloadMagics(Magics): is to act silently; --print (or -p) will print out the names of modules that are being reloaded, and --log (or -l) outputs them to the log at INFO level. + The optional argument --hide-errors hides any errors that can happen when trying to + reload code. + Reloading Python modules in a reliable way is in general difficult, and unexpected things may occur. %autoreload tries to work around common pitfalls by replacing function code objects and @@ -628,6 +641,8 @@ class AutoreloadMagics(Magics): elif args.log is True: self._reloader._report = l + self._reloader.hide_errors = args.hide_errors + if mode == "" or mode == "now": self._reloader.check(True) elif mode == "0" or mode == "off":