]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44631: Make the repr() of the _Environ class more readable. (#27128)
authorLeonardo Freua <leonardo.batista.freua@gmail.com>
Tue, 20 Jul 2021 17:15:45 +0000 (14:15 -0300)
committerGitHub <noreply@github.com>
Tue, 20 Jul 2021 17:15:45 +0000 (19:15 +0200)
Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Lib/os.py
Lib/test/test_os.py
Misc/NEWS.d/next/Documentation/2021-07-13-22-25-13.bpo-44631.qkGwe4.rst [new file with mode: 0644]

index d26cfc99939f390f6de2cf9e4f18a63a028b1425..8cc70a11e9bc898aa3f5e0c9380d0a05baed8794 100644 (file)
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -704,9 +704,11 @@ class _Environ(MutableMapping):
         return len(self._data)
 
     def __repr__(self):
-        return 'environ({{{}}})'.format(', '.join(
-            ('{!r}: {!r}'.format(self.decodekey(key), self.decodevalue(value))
-            for key, value in self._data.items())))
+        formatted_items = ", ".join(
+            f"{self.decodekey(key)!r}: {self.decodevalue(value)!r}"
+            for key, value in self._data.items()
+        )
+        return f"environ({{{formatted_items}}})"
 
     def copy(self):
         return dict(self)
index 684e308ad3a0596af73d3f8103bc4c7ff4d10526..00e738ecf9a1c35e387e0a69a49ac35137f2174f 100644 (file)
@@ -1029,9 +1029,11 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol):
     def test___repr__(self):
         """Check that the repr() of os.environ looks like environ({...})."""
         env = os.environ
-        self.assertEqual(repr(env), 'environ({{{}}})'.format(', '.join(
-            '{!r}: {!r}'.format(key, value)
-            for key, value in env.items())))
+        formatted_items = ", ".join(
+            f"{key!r}: {value!r}"
+            for key, value in env.items()
+        )
+        self.assertEqual(repr(env), f"environ({{{formatted_items}}})")
 
     def test_get_exec_path(self):
         defpath_list = os.defpath.split(os.pathsep)
diff --git a/Misc/NEWS.d/next/Documentation/2021-07-13-22-25-13.bpo-44631.qkGwe4.rst b/Misc/NEWS.d/next/Documentation/2021-07-13-22-25-13.bpo-44631.qkGwe4.rst
new file mode 100644 (file)
index 0000000..b0898fe
--- /dev/null
@@ -0,0 +1 @@
+Refactored the ``repr()`` code of the ``_Environ`` (os module).
\ No newline at end of file