Skip to content

Commit b518e6a

Browse files
authored
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.
2 parents 61fc0ab + 998efb1 commit b518e6a

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

IPython/extensions/autoreload.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ def __init__(self, shell=None):
170170
# Cache module modification times
171171
self.check(check_all=True, do_reload=False)
172172

173+
# To hide autoreload errors
174+
self.hide_errors = False
175+
173176
def mark_module_skipped(self, module_name):
174177
"""Skip reloading the named module in the future"""
175178
try:
@@ -274,12 +277,13 @@ def check(self, check_all=False, do_reload=True):
274277
if py_filename in self.failed:
275278
del self.failed[py_filename]
276279
except:
277-
print(
278-
"[autoreload of {} failed: {}]".format(
279-
modname, traceback.format_exc(10)
280-
),
281-
file=sys.stderr,
282-
)
280+
if not self.hide_errors:
281+
print(
282+
"[autoreload of {} failed: {}]".format(
283+
modname, traceback.format_exc(10)
284+
),
285+
file=sys.stderr,
286+
)
283287
self.failed[py_filename] = pymtime
284288

285289

@@ -553,6 +557,12 @@ def __init__(self, *a, **kw):
553557
default=False,
554558
help="Show autoreload activity using the logger",
555559
)
560+
@magic_arguments.argument(
561+
"--hide-errors",
562+
action="store_true",
563+
default=False,
564+
help="Hide autoreload errors",
565+
)
556566
def autoreload(self, line=""):
557567
r"""%autoreload => Reload modules automatically
558568
@@ -579,6 +589,9 @@ def autoreload(self, line=""):
579589
is to act silently; --print (or -p) will print out the names of modules that are being
580590
reloaded, and --log (or -l) outputs them to the log at INFO level.
581591
592+
The optional argument --hide-errors hides any errors that can happen when trying to
593+
reload code.
594+
582595
Reloading Python modules in a reliable way is in general
583596
difficult, and unexpected things may occur. %autoreload tries to
584597
work around common pitfalls by replacing function code objects and
@@ -628,6 +641,8 @@ def pl(msg):
628641
elif args.log is True:
629642
self._reloader._report = l
630643

644+
self._reloader.hide_errors = args.hide_errors
645+
631646
if mode == "" or mode == "now":
632647
self._reloader.check(True)
633648
elif mode == "0" or mode == "off":

0 commit comments

Comments
 (0)