Skip to content

Commit 6bccbe7

Browse files
gnpricevstinner
authored andcommitted
bpo-36502: Correct documentation of str.isspace() (GH-15019)
The documented definition was much broader than the real one: there are tons of characters with general category "Other", and we don't (and shouldn't) treat most of them as whitespace. Rewrite the definition to agree with the comment on _PyUnicode_IsWhitespace, and with the logic in makeunicodedata.py, which is what generates that function and so ultimately governs. Add suitable breadcrumbs so that a reader who wants to pin down exactly what this definition means (what's a "bidirectional class" of "B"?) can do so. The `unicodedata` module documentation is an appropriate central place for our references to Unicode's own copious documentation, so point there. Also add to the isspace() test a thorough check that the implementation agrees with the intended definition.
1 parent 077af8c commit 6bccbe7

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

Doc/library/stdtypes.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,9 +1763,13 @@ expression support in the :mod:`re` module).
17631763
.. method:: str.isspace()
17641764

17651765
Return true if there are only whitespace characters in the string and there is
1766-
at least one character, false otherwise. Whitespace characters are those
1767-
characters defined in the Unicode character database as "Other" or "Separator"
1768-
and those with bidirectional property being one of "WS", "B", or "S".
1766+
at least one character, false otherwise.
1767+
1768+
A character is *whitespace* if in the Unicode character database
1769+
(see :mod:`unicodedata`), either its general category is ``Zs``
1770+
("Separator, space"), or its bidirectional class is one of ``WS``,
1771+
``B``, or ``S``.
1772+
17691773

17701774
.. method:: str.istitle()
17711775

Lib/test/test_unicode.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import struct
1313
import sys
1414
import textwrap
15+
import unicodedata
1516
import unittest
1617
import warnings
1718
from test import support, string_tests
@@ -617,11 +618,21 @@ def test_isspace(self):
617618
self.checkequalnofix(True, '\u2000', 'isspace')
618619
self.checkequalnofix(True, '\u200a', 'isspace')
619620
self.checkequalnofix(False, '\u2014', 'isspace')
620-
# apparently there are no non-BMP spaces chars in Unicode 6
621+
# There are no non-BMP whitespace chars as of Unicode 12.
621622
for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E',
622623
'\U0001F40D', '\U0001F46F']:
623624
self.assertFalse(ch.isspace(), '{!a} is not space.'.format(ch))
624625

626+
@support.requires_resource('cpu')
627+
def test_isspace_invariant(self):
628+
for codepoint in range(sys.maxunicode + 1):
629+
char = chr(codepoint)
630+
bidirectional = unicodedata.bidirectional(char)
631+
category = unicodedata.category(char)
632+
self.assertEqual(char.isspace(),
633+
(bidirectional in ('WS', 'B', 'S')
634+
or category == 'Zs'))
635+
625636
def test_isalnum(self):
626637
super().test_isalnum()
627638
for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E',

0 commit comments

Comments
 (0)