Skip to content

Commit bd221c0

Browse files
authored
pythongh-98139: enhance namespace package repr
1 parent 2db55e0 commit bd221c0

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

Lib/importlib/_bootstrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ def _module_repr_from_spec(spec):
580580
if spec.loader is None:
581581
return f'<module {name!r}>'
582582
else:
583-
return f'<module {name!r} ({spec.loader!r})>'
583+
return f'<module {name!r} (namespace) from {list(spec.loader._path)}>'
584584
else:
585585
if spec.has_location:
586586
return f'<module {name!r} from {spec.origin!r}>'

Lib/test/test_importlib/test_namespace_pkgs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ def test_cant_import_other(self):
7979
with self.assertRaises(ImportError):
8080
import foo.two
8181

82+
def test_simple_repr(self):
83+
import foo.one
84+
assert repr(foo).startswith("<module 'foo' (namespace) from [")
85+
8286

8387
class DynamicPathNamespacePackage(NamespacePackageTest):
8488
paths = ['portion1']
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In :mod:`importlib._bootstrap`, enhance namespace package repr to `<module
2+
'x' (namespace) from ['path']>`.

0 commit comments

Comments
 (0)