mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-40066: [Enum] update str() and format() output (GH-30582)
Undo rejected PEP-663 changes: - restore `repr()` to its 3.10 status - restore `str()` to its 3.10 status New changes: - `IntEnum` and `IntFlag` now leave `__str__` as the original `int.__str__` so that str() and format() return the same result - zero-valued flags without a name have a slightly changed repr(), e.g. `repr(Color(0)) == '<Color: 0>'` - update `dir()` for mixed-in types to return all the methods and attributes of the mixed-in type - added `_numeric_repr_` to `Flag` to control display of unnamed values - enums without doc strings have a more comprehensive doc string added - `ReprEnum` added -- inheriting from this makes it so only `__repr__` is replaced, not `__str__` nor `__format__`; `IntEnum`, `IntFlag`, and `StrEnum` all inherit from `ReprEnum`
This commit is contained in:
parent
37eab55ac9
commit
acf7403f9b
14 changed files with 2085 additions and 2019 deletions
|
@ -1517,9 +1517,11 @@ class GeneralModuleTests(unittest.TestCase):
|
|||
infos = socket.getaddrinfo(HOST, 80, socket.AF_INET, socket.SOCK_STREAM)
|
||||
for family, type, _, _, _ in infos:
|
||||
self.assertEqual(family, socket.AF_INET)
|
||||
self.assertEqual(str(family), 'AF_INET')
|
||||
self.assertEqual(repr(family), '<AddressFamily.AF_INET: 2>')
|
||||
self.assertEqual(str(family), '2')
|
||||
self.assertEqual(type, socket.SOCK_STREAM)
|
||||
self.assertEqual(str(type), 'SOCK_STREAM')
|
||||
self.assertEqual(repr(type), '<SocketKind.SOCK_STREAM: 1>')
|
||||
self.assertEqual(str(type), '1')
|
||||
infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM)
|
||||
for _, socktype, _, _, _ in infos:
|
||||
self.assertEqual(socktype, socket.SOCK_STREAM)
|
||||
|
@ -1793,8 +1795,10 @@ class GeneralModuleTests(unittest.TestCase):
|
|||
# Make sure that the AF_* and SOCK_* constants have enum-like string
|
||||
# reprs.
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
self.assertEqual(str(s.family), 'AF_INET')
|
||||
self.assertEqual(str(s.type), 'SOCK_STREAM')
|
||||
self.assertEqual(repr(s.family), '<AddressFamily.AF_INET: 2>')
|
||||
self.assertEqual(repr(s.type), '<SocketKind.SOCK_STREAM: 1>')
|
||||
self.assertEqual(str(s.family), '2')
|
||||
self.assertEqual(str(s.type), '1')
|
||||
|
||||
def test_socket_consistent_sock_type(self):
|
||||
SOCK_NONBLOCK = getattr(socket, 'SOCK_NONBLOCK', 0)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue