Skip to content

Commit 09b17d2

Browse files
refactor: review items
1 parent 5849913 commit 09b17d2

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

Lib/test/test_traceback.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3802,6 +3802,26 @@ def test_traceback_header(self):
38023802
exc = traceback.TracebackException(Exception, Exception("haven"), None)
38033803
self.assertEqual(list(exc.format()), ["Exception: haven\n"])
38043804

3805+
def test_exception_punctuation_handling_with_suggestions(self):
3806+
def raise_mssage(message, name):
3807+
try:
3808+
raise NameError(message, name=name)
3809+
except Exception as e:
3810+
return traceback.TracebackException.from_exception(e)._str
3811+
3812+
test_cases = [
3813+
("Error.", "time", "Error. Did you forget to import 'time'?"),
3814+
("Error?", "time", "Error? Did you forget to import 'time'?"),
3815+
("Error!", "time", "Error! Did you forget to import 'time'?"),
3816+
("Error", "time", "Error. Did you forget to import 'time'?"),
3817+
("Error", "foo123", "Error"),
3818+
]
3819+
for puctuation, name, expected in test_cases:
3820+
with self.subTest(puctuation=puctuation):
3821+
messsage = raise_mssage(puctuation, name)
3822+
self.assertEqual(messsage, expected)
3823+
3824+
38053825
@requires_debug_ranges()
38063826
def test_print(self):
38073827
def f():
@@ -4878,28 +4898,6 @@ class PurePythonSuggestionFormattingTests(
48784898
traceback printing in traceback.py.
48794899
"""
48804900

4881-
def test_exception_punctuation_handling_with_suggestions(self):
4882-
def raise_with_period(): raise NameError("Error.", name='time')
4883-
def raise_with_exclamation(): raise NameError("Error!", name='time')
4884-
def raise_with_question(): raise NameError("Error?", name='time')
4885-
def raise_without_punctuation(): raise NameError("Error", name='time')
4886-
4887-
test_cases = [
4888-
(raise_with_period, "."),
4889-
(raise_with_exclamation, "!"),
4890-
(raise_with_question, "?"),
4891-
(raise_without_punctuation, "."),
4892-
]
4893-
4894-
for raise_function, punctuation in test_cases:
4895-
with self.subTest(raise_func=raise_function.__name__):
4896-
result_lines = self.get_exception(
4897-
raise_function, slice_start=-1, slice_end=None
4898-
)
4899-
expected = f"NameError: Error{punctuation} Did you forget to import 'time'?"
4900-
self.assertEqual(result_lines[0], expected)
4901-
4902-
49034901

49044902
@cpython_only
49054903
class CPythonSuggestionFormattingTests(

Lib/traceback.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,16 +1732,16 @@ def _suggestion_message(exc_type, exc_value, exc_traceback):
17321732
other_name = _compute_suggestion_error(
17331733
exc_value, exc_traceback, wrong_name
17341734
)
1735-
maybe_builtin_import = (
1735+
maybe_stdlib_import = (
17361736
issubclass(exc_type, NameError)
17371737
and wrong_name in sys.stdlib_module_names
17381738
)
17391739
if not other_name:
1740-
if maybe_builtin_import:
1740+
if maybe_stdlib_import:
17411741
return f"Did you forget to import '{wrong_name}'?"
17421742
return None
17431743
text = f"Did you mean: '{other_name}'?"
1744-
if maybe_builtin_import:
1744+
if maybe_stdlib_import:
17451745
return f"{text} Or did you forget to import '{wrong_name}'?"
17461746
return text
17471747
return None

0 commit comments

Comments
 (0)