[Python-checkins] bpo-44631: Make the repr() of the _Environ class more readable. (#27128)

ambv webhook-mailer at python.org
Tue Jul 20 13:15:50 EDT 2021


https://github.com/python/cpython/commit/85fa3b6b7c11897732fedc443db0e4e8e380c8f8
commit: 85fa3b6b7c11897732fedc443db0e4e8e380c8f8
branch: main
author: Leonardo Freua <leonardo.batista.freua at gmail.com>
committer: ambv <lukasz at langa.pl>
date: 2021-07-20T19:15:45+02:00
summary:

bpo-44631: Make the repr() of the _Environ class more readable. (#27128)

Co-authored-by: Łukasz Langa <lukasz at langa.pl>

files:
A Misc/NEWS.d/next/Documentation/2021-07-13-22-25-13.bpo-44631.qkGwe4.rst
M Lib/os.py
M Lib/test/test_os.py

diff --git a/Lib/os.py b/Lib/os.py
index d26cfc99939f3..8cc70a11e9bc8 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -704,9 +704,11 @@ def __len__(self):
         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)
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 684e308ad3a05..00e738ecf9a1c 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -1029,9 +1029,11 @@ def test_items(self):
     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
index 0000000000000..b0898fe1ad999
--- /dev/null
+++ b/Misc/NEWS.d/next/Documentation/2021-07-13-22-25-13.bpo-44631.qkGwe4.rst
@@ -0,0 +1 @@
+Refactored the ``repr()`` code of the ``_Environ`` (os module).
\ No newline at end of file



More information about the Python-checkins mailing list