[Python-checkins] gh-98139: enhance namespace package repr

FFY00 webhook-mailer at python.org
Sun Nov 6 17:13:46 EST 2022


https://github.com/python/cpython/commit/bd221c01ddcd2cad102133611fab3569a99680b0
commit: bd221c01ddcd2cad102133611fab3569a99680b0
branch: main
author: Anh71me <iyumelive at gmail.com>
committer: FFY00 <filipe.lains at gmail.com>
date: 2022-11-06T22:13:40Z
summary:

gh-98139: enhance namespace package repr

files:
A Misc/NEWS.d/next/Library/2022-10-30-15-26-33.gh-issue-98139.qtm-9T.rst
M Lib/importlib/_bootstrap.py
M Lib/test/test_importlib/test_namespace_pkgs.py

diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index e0110e701d6c..1a1d5cf7c9c3 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -580,7 +580,7 @@ def _module_repr_from_spec(spec):
         if spec.loader is None:
             return f'<module {name!r}>'
         else:
-            return f'<module {name!r} ({spec.loader!r})>'
+            return f'<module {name!r} (namespace) from {list(spec.loader._path)}>'
     else:
         if spec.has_location:
             return f'<module {name!r} from {spec.origin!r}>'
diff --git a/Lib/test/test_importlib/test_namespace_pkgs.py b/Lib/test/test_importlib/test_namespace_pkgs.py
index f451f7547b35..65428c3d3ead 100644
--- a/Lib/test/test_importlib/test_namespace_pkgs.py
+++ b/Lib/test/test_importlib/test_namespace_pkgs.py
@@ -79,6 +79,10 @@ def test_cant_import_other(self):
         with self.assertRaises(ImportError):
             import foo.two
 
+    def test_simple_repr(self):
+        import foo.one
+        assert repr(foo).startswith("<module 'foo' (namespace) from [")
+
 
 class DynamicPathNamespacePackage(NamespacePackageTest):
     paths = ['portion1']
diff --git a/Misc/NEWS.d/next/Library/2022-10-30-15-26-33.gh-issue-98139.qtm-9T.rst b/Misc/NEWS.d/next/Library/2022-10-30-15-26-33.gh-issue-98139.qtm-9T.rst
new file mode 100644
index 000000000000..ed5dd0030627
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-10-30-15-26-33.gh-issue-98139.qtm-9T.rst
@@ -0,0 +1,2 @@
+In :mod:`importlib._bootstrap`, enhance namespace package repr to `<module
+'x' (namespace) from ['path']>`.



More information about the Python-checkins mailing list