From 4b3cc8515ac2c0e2ae5c6613ad42a13a7acfaf93 Mon Sep 17 00:00:00 2001 From: Nacho Caballero Date: Sun, 20 Jul 2025 14:06:23 +0200 Subject: [PATCH 1/3] improve `StrEnum` docs --- Doc/library/enum.rst | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index c9b2c7d76b6746..ed5f2bb9590474 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -504,16 +504,28 @@ Data Types .. class:: StrEnum - ``StrEnum`` is the same as :class:`Enum`, but its members are also strings and can be used - in most of the same places that a string can be used. The result of any string - operation performed on or with a *StrEnum* member is not part of the enumeration. + *StrEnum* is the same as :class:`Enum`, but its members are also strings and + can be used in most of the same places that a string can be used. The result + of any string operation performed on or with a *StrEnum* member is not part + of the enumeration. + + >>> from enum import StrEnum + >>> class Name(StrEnum): + ... IDENTIFIER = 'id' + ... PROPERTY = 'pro' + ... ATTRIBUTE = 'attr' + ... + >>> Name.IDENTIFIER + + >>> str(Name.IDENTIFIER) + 'id' .. note:: There are places in the stdlib that check for an exact :class:`str` instead of a :class:`str` subclass (i.e. ``type(unknown) == str`` instead of ``isinstance(unknown, str)``), and in those locations you - will need to use ``str(StrEnum.member)``. + will need to use ``str(MyStrEnum.MY_MEMBER)``. .. note:: From ef67fc6a75f2d0f5da22e1c319fb0ee765482e5e Mon Sep 17 00:00:00 2001 From: Nacho Caballero Date: Sun, 20 Jul 2025 14:43:14 +0200 Subject: [PATCH 2/3] improve the example Co-authored-by: Antonio Spadaro --- Doc/library/enum.rst | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index ed5f2bb9590474..43a0516bd95712 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -510,15 +510,20 @@ Data Types of the enumeration. >>> from enum import StrEnum - >>> class Name(StrEnum): - ... IDENTIFIER = 'id' - ... PROPERTY = 'pro' - ... ATTRIBUTE = 'attr' - ... - >>> Name.IDENTIFIER - - >>> str(Name.IDENTIFIER) - 'id' + >>> class Color(StrEnum): + ... RED = 'r' + ... GREEN = 'g' + ... BLUE = 'b' + ... UNKNOWN = auto() + ... + >>> Color.RED + + >>> str(Color.RED) + 'r' + >>> Color.UNKNOWN + + >>> str(Color.UNKNOWN) + 'unknown' .. note:: From 81d2831aa4cffa2cce8195efc4ba0f0286f51d5c Mon Sep 17 00:00:00 2001 From: Nacho Caballero Date: Sun, 20 Jul 2025 14:55:59 +0200 Subject: [PATCH 3/3] fix the example --- Doc/library/enum.rst | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index 43a0516bd95712..2cfc2f4962979f 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -509,21 +509,19 @@ Data Types of any string operation performed on or with a *StrEnum* member is not part of the enumeration. - >>> from enum import StrEnum - >>> class Color(StrEnum): - ... RED = 'r' - ... GREEN = 'g' - ... BLUE = 'b' - ... UNKNOWN = auto() - ... - >>> Color.RED - - >>> str(Color.RED) - 'r' - >>> Color.UNKNOWN - - >>> str(Color.UNKNOWN) - 'unknown' + >>> from enum import StrEnum, auto + >>> class Color(StrEnum): + ... RED = 'r' + ... GREEN = 'g' + ... BLUE = 'b' + ... UNKNOWN = auto() + ... + >>> Color.RED + + >>> Color.UNKNOWN + + >>> str(Color.UNKNOWN) + 'unknown' .. note::