summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_exceptions.py
diff options
context:
space:
mode:
authorDennis Sweeney <36520290+sweeneyde@users.noreply.github.com>2021-04-27 00:22:27 (GMT)
committerGitHub <noreply@github.com>2021-04-27 00:22:27 (GMT)
commit284c52da092438522949d6f96d8c1f9ff37f9f00 (patch)
tree8bbd5d4c0b4e396dd27f52b94f94e5c90b9e97f6 /Lib/test/test_exceptions.py
parent7244c0060dc3ef909f34b0791c3e7490b0340d5e (diff)
downloadcpython-284c52da092438522949d6f96d8c1f9ff37f9f00.zip
cpython-284c52da092438522949d6f96d8c1f9ff37f9f00.tar.gz
cpython-284c52da092438522949d6f96d8c1f9ff37f9f00.tar.bz2
bpo-38530: Require 50% similarity in NameError and AttributeError suggestions (GH-25584)
Diffstat (limited to 'Lib/test/test_exceptions.py')
-rw-r--r--Lib/test/test_exceptions.py112
1 files changed, 112 insertions, 0 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index b3c00b0..590935c 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -1507,6 +1507,61 @@ class NameErrorTests(unittest.TestCase):
self.assertNotIn("somethingverywronghehe", err.getvalue())
+ def test_name_error_bad_suggestions_do_not_trigger_for_small_names(self):
+ vvv = mom = w = id = pytho = None
+
+ with self.subTest(name="b"):
+ try:
+ b
+ except NameError as exc:
+ with support.captured_stderr() as err:
+ sys.__excepthook__(*sys.exc_info())
+ self.assertNotIn("you mean", err.getvalue())
+ self.assertNotIn("vvv", err.getvalue())
+ self.assertNotIn("mom", err.getvalue())
+ self.assertNotIn("'id'", err.getvalue())
+ self.assertNotIn("'w'", err.getvalue())
+ self.assertNotIn("'pytho'", err.getvalue())
+
+ with self.subTest(name="v"):
+ try:
+ v
+ except NameError as exc:
+ with support.captured_stderr() as err:
+ sys.__excepthook__(*sys.exc_info())
+ self.assertNotIn("you mean", err.getvalue())
+ self.assertNotIn("vvv", err.getvalue())
+ self.assertNotIn("mom", err.getvalue())
+ self.assertNotIn("'id'", err.getvalue())
+ self.assertNotIn("'w'", err.getvalue())
+ self.assertNotIn("'pytho'", err.getvalue())
+
+ with self.subTest(name="m"):
+ try:
+ m
+ except NameError as exc:
+ with support.captured_stderr() as err:
+ sys.__excepthook__(*sys.exc_info())
+ self.assertNotIn("you mean", err.getvalue())
+ self.assertNotIn("vvv", err.getvalue())
+ self.assertNotIn("mom", err.getvalue())
+ self.assertNotIn("'id'", err.getvalue())
+ self.assertNotIn("'w'", err.getvalue())
+ self.assertNotIn("'pytho'", err.getvalue())
+
+ with self.subTest(name="py"):
+ try:
+ py
+ except NameError as exc:
+ with support.captured_stderr() as err:
+ sys.__excepthook__(*sys.exc_info())
+ self.assertNotIn("you mean", err.getvalue())
+ self.assertNotIn("vvv", err.getvalue())
+ self.assertNotIn("mom", err.getvalue())
+ self.assertNotIn("'id'", err.getvalue())
+ self.assertNotIn("'w'", err.getvalue())
+ self.assertNotIn("'pytho'", err.getvalue())
+
def test_name_error_suggestions_do_not_trigger_for_too_many_locals(self):
def f():
# Mutating locals() is unreliable, so we need to do it by hand
@@ -1661,6 +1716,63 @@ class AttributeErrorTests(unittest.TestCase):
self.assertNotIn("blech", err.getvalue())
+ def test_getattr_error_bad_suggestions_do_not_trigger_for_small_names(self):
+ class MyClass:
+ vvv = mom = w = id = pytho = None
+
+ with self.subTest(name="b"):
+ try:
+ MyClass.b
+ except AttributeError as exc:
+ with support.captured_stderr() as err:
+ sys.__excepthook__(*sys.exc_info())
+ self.assertNotIn("you mean", err.getvalue())
+ self.assertNotIn("vvv", err.getvalue())
+ self.assertNotIn("mom", err.getvalue())
+ self.assertNotIn("'id'", err.getvalue())
+ self.assertNotIn("'w'", err.getvalue())
+ self.assertNotIn("'pytho'", err.getvalue())
+
+ with self.subTest(name="v"):
+ try:
+ MyClass.v
+ except AttributeError as exc:
+ with support.captured_stderr() as err:
+ sys.__excepthook__(*sys.exc_info())
+ self.assertNotIn("you mean", err.getvalue())
+ self.assertNotIn("vvv", err.getvalue())
+ self.assertNotIn("mom", err.getvalue())
+ self.assertNotIn("'id'", err.getvalue())
+ self.assertNotIn("'w'", err.getvalue())
+ self.assertNotIn("'pytho'", err.getvalue())
+
+ with self.subTest(name="m"):
+ try:
+ MyClass.m
+ except AttributeError as exc:
+ with support.captured_stderr() as err:
+ sys.__excepthook__(*sys.exc_info())
+ self.assertNotIn("you mean", err.getvalue())
+ self.assertNotIn("vvv", err.getvalue())
+ self.assertNotIn("mom", err.getvalue())
+ self.assertNotIn("'id'", err.getvalue())
+ self.assertNotIn("'w'", err.getvalue())
+ self.assertNotIn("'pytho'", err.getvalue())
+
+ with self.subTest(name="py"):
+ try:
+ MyClass.py
+ except AttributeError as exc:
+ with support.captured_stderr() as err:
+ sys.__excepthook__(*sys.exc_info())
+ self.assertNotIn("you mean", err.getvalue())
+ self.assertNotIn("vvv", err.getvalue())
+ self.assertNotIn("mom", err.getvalue())
+ self.assertNotIn("'id'", err.getvalue())
+ self.assertNotIn("'w'", err.getvalue())
+ self.assertNotIn("'pytho'", err.getvalue())
+
+
def test_getattr_suggestions_do_not_trigger_for_big_dicts(self):
class A:
blech = None