From 05ecd24bb8e70405a607b6ae09732a8ae4d39e23 Mon Sep 17 00:00:00 2001 From: Adam Chhina Date: Mon, 24 Apr 2023 12:00:30 -0600 Subject: [PATCH 1/5] GH-95088: Clarified rules for parsing an item key for format strings. --- Doc/library/string.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Doc/library/string.rst b/Doc/library/string.rst index 3b96813e683864..de41df2c8cacc2 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -231,7 +231,9 @@ it refers to a named keyword argument. If the numerical arg_names in a format s are 0, 1, 2, ... in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be automatically inserted in that order. Because *arg_name* is not quote-delimited, it is not possible to specify arbitrary -dictionary keys (e.g., the strings ``'10'`` or ``':-]'``) within a format string. +dictionary keys (e.g., the strings ``'10'`` or ``':-]'``) within a format string. +The rules for parsing an item key is if it starts with a digit then it is treated as a +number, otherwise it is used as a string. The *arg_name* can be followed by any number of index or attribute expressions. An expression of the form ``'.name'`` selects the named attribute using :func:`getattr`, while an expression of the form ``'[index]'`` From de72624b8085b4360f5ee8a00e06d64155b88580 Mon Sep 17 00:00:00 2001 From: Adam Chhina Date: Mon, 24 Apr 2023 12:14:58 -0600 Subject: [PATCH 2/5] GH-95088: Fix trailing whitespace. --- Doc/library/string.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/string.rst b/Doc/library/string.rst index de41df2c8cacc2..ef8b3b444d3d05 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -231,9 +231,9 @@ it refers to a named keyword argument. If the numerical arg_names in a format s are 0, 1, 2, ... in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be automatically inserted in that order. Because *arg_name* is not quote-delimited, it is not possible to specify arbitrary -dictionary keys (e.g., the strings ``'10'`` or ``':-]'``) within a format string. -The rules for parsing an item key is if it starts with a digit then it is treated as a -number, otherwise it is used as a string. +dictionary keys (e.g., the strings ``'10'`` or ``':-]'``) within a format string. +The rules for parsing an item key is if it starts with a digit then it is treated as a +number, otherwise it is used as a string. The *arg_name* can be followed by any number of index or attribute expressions. An expression of the form ``'.name'`` selects the named attribute using :func:`getattr`, while an expression of the form ``'[index]'`` From bf4b5c0fcca8530ab258e42f08e35c39de512bd5 Mon Sep 17 00:00:00 2001 From: Adam Chhina Date: Sat, 29 Apr 2023 01:23:08 -0400 Subject: [PATCH 3/5] GH-95088: Address review comment. --- Doc/library/string.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Doc/library/string.rst b/Doc/library/string.rst index ef8b3b444d3d05..de4761138fb890 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -227,13 +227,12 @@ See also the :ref:`formatspec` section. The *field_name* itself begins with an *arg_name* that is either a number or a keyword. If it's a number, it refers to a positional argument, and if it's a keyword, -it refers to a named keyword argument. If the numerical arg_names in a format string +it refers to a named keyword argument. A key is treated as a number if it starts with a digit. +If the numerical arg_names in a format string are 0, 1, 2, ... in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be automatically inserted in that order. Because *arg_name* is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings ``'10'`` or ``':-]'``) within a format string. -The rules for parsing an item key is if it starts with a digit then it is treated as a -number, otherwise it is used as a string. The *arg_name* can be followed by any number of index or attribute expressions. An expression of the form ``'.name'`` selects the named attribute using :func:`getattr`, while an expression of the form ``'[index]'`` From d85fd11b691c9bc8064c6458a3413f4fd1f4ccdc Mon Sep 17 00:00:00 2001 From: Adam Chhina Date: Sat, 6 May 2023 19:10:10 -0400 Subject: [PATCH 4/5] GH-95088: Update definition based on implementation. --- Doc/library/string.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/library/string.rst b/Doc/library/string.rst index 7cc38909a0e404..24f7e747834742 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -227,7 +227,8 @@ See also the :ref:`formatspec` section. The *field_name* itself begins with an *arg_name* that is either a number or a keyword. If it's a number, it refers to a positional argument, and if it's a keyword, -it refers to a named keyword argument. A key is treated as a number if it starts with a digit. +it refers to a named keyword argument. A key is treated as a number if +``str(key).isdecimal()`` would evaluate to true. If the numerical arg_names in a format string are 0, 1, 2, ... in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be automatically inserted in that order. From 7639de37f74c3ef8c04f3ee6455ec9290d6c0bc7 Mon Sep 17 00:00:00 2001 From: achhina Date: Tue, 6 Jun 2023 20:41:52 -0400 Subject: [PATCH 5/5] Update Doc/library/string.rst Co-authored-by: Jelle Zijlstra --- Doc/library/string.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/string.rst b/Doc/library/string.rst index 24f7e747834742..9b28f99536a3ae 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -227,8 +227,8 @@ See also the :ref:`formatspec` section. The *field_name* itself begins with an *arg_name* that is either a number or a keyword. If it's a number, it refers to a positional argument, and if it's a keyword, -it refers to a named keyword argument. A key is treated as a number if -``str(key).isdecimal()`` would evaluate to true. +it refers to a named keyword argument. An *arg_name* is treated as a number if +a call to :meth:`str.isdecimal` on the string would return true. If the numerical arg_names in a format string are 0, 1, 2, ... in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be automatically inserted in that order.