diff options
author | Leonardo Freua <leonardo.batista.freua@gmail.com> | 2021-07-20 17:15:45 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-20 17:15:45 (GMT) |
commit | 85fa3b6b7c11897732fedc443db0e4e8e380c8f8 (patch) | |
tree | 98985ed2b78e1ec4689b1da1204b729cfe2deab3 | |
parent | 42205ee512159de62c01e202ff799d78fac9ac26 (diff) | |
download | cpython-85fa3b6b7c11897732fedc443db0e4e8e380c8f8.zip cpython-85fa3b6b7c11897732fedc443db0e4e8e380c8f8.tar.gz cpython-85fa3b6b7c11897732fedc443db0e4e8e380c8f8.tar.bz2 |
bpo-44631: Make the repr() of the _Environ class more readable. (#27128)
Co-authored-by: Ćukasz Langa <lukasz@langa.pl>
-rw-r--r-- | Lib/os.py | 8 | ||||
-rw-r--r-- | Lib/test/test_os.py | 8 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Documentation/2021-07-13-22-25-13.bpo-44631.qkGwe4.rst | 1 |
3 files changed, 11 insertions, 6 deletions
@@ -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 |