From 85fa3b6b7c11897732fedc443db0e4e8e380c8f8 Mon Sep 17 00:00:00 2001 From: Leonardo Freua Date: Tue, 20 Jul 2021 14:15:45 -0300 Subject: bpo-44631: Make the repr() of the _Environ class more readable. (#27128) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ɓukasz Langa --- Lib/os.py | 8 +++++--- Lib/test/test_os.py | 8 +++++--- .../next/Documentation/2021-07-13-22-25-13.bpo-44631.qkGwe4.rst | 1 + 3 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Documentation/2021-07-13-22-25-13.bpo-44631.qkGwe4.rst diff --git a/Lib/os.py b/Lib/os.py index d26cfc9..8cc70a1 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 684e308..00e738e 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 0000000..b0898fe --- /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 -- cgit v0.12