From: Leonardo Freua Date: Tue, 20 Jul 2021 17:15:45 +0000 (-0300) Subject: bpo-44631: Make the repr() of the _Environ class more readable. (#27128) X-Git-Tag: v3.11.0a1~618 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=85fa3b6b7c11897732fedc443db0e4e8e380c8f8;p=thirdparty%2FPython%2Fcpython.git bpo-44631: Make the repr() of the _Environ class more readable. (#27128) Co-authored-by: Ɓukasz Langa --- diff --git a/Lib/os.py b/Lib/os.py index d26cfc99939f..8cc70a11e9bc 100644 --- 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) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 684e308ad3a0..00e738ecf9a1 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -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 index 000000000000..b0898fe1ad99 --- /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