Skip to content

Commit a45ed75

Browse files
committed
move tests, edit rst wording
1 parent 25f3d42 commit a45ed75

File tree

3 files changed

+51
-50
lines changed

3 files changed

+51
-50
lines changed

Doc/whatsnew/3.15.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ Other language changes
200200
* Several error messages incorrectly using the term "argument" have been corrected.
201201
(Contributed by Stan Ulbrych in :gh:`133382`.)
202202

203-
* The :meth:`repr` of :class:`ImportError` and :class:`ModuleNotFoundError` now
204-
contains attributes name and path.
203+
* The :meth:`~object.__repr__` of :class:`ImportError` and :class:`ModuleNotFoundError`
204+
now shows "name" and "path" as ``name=<name>`` and ``path=<path>`` if they were given
205+
as keyword arguments at construction time.
205206
(Contributed by Serhiy Storchaka, Oleg Iarygin, and Yoav Nir in :gh:`74185`.)
206207

207208

Lib/test/test_exceptions.py

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,6 +2079,50 @@ def test_copy_pickle(self):
20792079
self.assertEqual(exc.name, orig.name)
20802080
self.assertEqual(exc.path, orig.path)
20812081

2082+
def test_repr(self):
2083+
exc = ImportError()
2084+
self.assertEqual(repr(exc), "ImportError()")
2085+
2086+
exc = ImportError('test')
2087+
self.assertEqual(repr(exc), "ImportError('test')")
2088+
2089+
exc = ImportError('test', 'case')
2090+
self.assertEqual(repr(exc), "ImportError('test', 'case')")
2091+
2092+
exc = ImportError(name='somemodule')
2093+
self.assertEqual(repr(exc), "ImportError(name='somemodule')")
2094+
2095+
exc = ImportError('test', name='somemodule')
2096+
self.assertEqual(repr(exc), "ImportError('test', name='somemodule')")
2097+
2098+
exc = ImportError(path='somepath')
2099+
self.assertEqual(repr(exc), "ImportError(path='somepath')")
2100+
2101+
exc = ImportError('test', path='somepath')
2102+
self.assertEqual(repr(exc), "ImportError('test', path='somepath')")
2103+
2104+
exc = ImportError(name='somename', path='somepath')
2105+
self.assertEqual(repr(exc),
2106+
"ImportError(name='somename', path='somepath')")
2107+
2108+
exc = ImportError('test', name='somename', path='somepath')
2109+
self.assertEqual(repr(exc),
2110+
"ImportError('test', name='somename', path='somepath')")
2111+
2112+
exc = ModuleNotFoundError('test', name='somename', path='somepath')
2113+
self.assertEqual(repr(exc),
2114+
"ModuleNotFoundError('test', name='somename', path='somepath')")
2115+
2116+
def test_ModuleNotFoundError_repr_with_failed_import(self):
2117+
with self.assertRaises(ModuleNotFoundError) as cm:
2118+
import does_not_exist # type: ignore[import] # noqa: F401
2119+
2120+
self.assertEqual(cm.exception.name, "does_not_exist")
2121+
self.assertIsNone(cm.exception.path)
2122+
2123+
self.assertEqual(repr(cm.exception),
2124+
"ModuleNotFoundError(\"No module named 'does_not_exist'\", name='does_not_exist')")
2125+
20822126

20832127
def run_script(source):
20842128
if isinstance(source, str):
@@ -2596,50 +2640,5 @@ def after_with():
25962640
1/0
25972641
self.lineno_after_raise(after_with, 1, 1)
25982642

2599-
def test_repr(self):
2600-
exc = ImportError()
2601-
self.assertEqual(repr(exc), "ImportError()")
2602-
2603-
exc = ImportError('test')
2604-
self.assertEqual(repr(exc), "ImportError('test')")
2605-
2606-
exc = ImportError('test', 'case')
2607-
self.assertEqual(repr(exc), "ImportError('test', 'case')")
2608-
2609-
exc = ImportError(name='somemodule')
2610-
self.assertEqual(repr(exc), "ImportError(name='somemodule')")
2611-
2612-
exc = ImportError('test', name='somemodule')
2613-
self.assertEqual(repr(exc), "ImportError('test', name='somemodule')")
2614-
2615-
exc = ImportError(path='somepath')
2616-
self.assertEqual(repr(exc), "ImportError(path='somepath')")
2617-
2618-
exc = ImportError('test', path='somepath')
2619-
self.assertEqual(repr(exc), "ImportError('test', path='somepath')")
2620-
2621-
exc = ImportError(name='somename', path='somepath')
2622-
self.assertEqual(repr(exc),
2623-
"ImportError(name='somename', path='somepath')")
2624-
2625-
exc = ImportError('test', name='somename', path='somepath')
2626-
self.assertEqual(repr(exc),
2627-
"ImportError('test', name='somename', path='somepath')")
2628-
2629-
exc = ModuleNotFoundError('test', name='somename', path='somepath')
2630-
self.assertEqual(repr(exc),
2631-
"ModuleNotFoundError('test', name='somename', path='somepath')")
2632-
2633-
def test_ModuleNotFoundError_repr_with_failed_import(self):
2634-
with self.assertRaises(ModuleNotFoundError) as cm:
2635-
import does_not_exist # type: ignore[import] # noqa: F401
2636-
2637-
self.assertEqual(cm.exception.name, "does_not_exist")
2638-
self.assertIsNone(cm.exception.path)
2639-
2640-
self.assertEqual(repr(cm.exception),
2641-
"ModuleNotFoundError(\"No module named 'does_not_exist'\", name='does_not_exist')")
2642-
2643-
26442643
if __name__ == '__main__':
26452644
unittest.main()
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
The :meth:`repr` of :class:`ImportError` and :class:`ModuleNotFoundError` now
2-
contains attributes name and path.
3-
Patch by Serhiy Storchaka, Oleg Iarygin, and Yoav Nir
1+
The :meth:`~object.__repr__` of :class:`ImportError` and :class:`ModuleNotFoundError`
2+
now shows "name" and "path" as ``name=<name>`` and ``path=<path>`` if they were given
3+
as keyword arguments at construction time.
4+
(Contributed by Serhiy Storchaka, Oleg Iarygin, and Yoav Nir in :gh:`74185`.)

0 commit comments

Comments
 (0)