Repository navigation
Closed
Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = None
created_at = <Date 2015-10-18.12:15:36.240>
labels = ['easy', '3.8', 'type-feature', '3.7', 'docs']
title = 'whitespace in strip()/lstrip()/rstrip()'
updated_at = <Date 2019-07-14.11:58:14.923>
user = 'https://bugs.python.org/DimitriPapadopoulosOrfanos'bugs.python.org fields:
activity = <Date 2019-07-14.11:58:14.923>
actor = 'rompe'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2015-10-18.12:15:36.240>
creator = 'Dimitri Papadopoulos Orfanos'
dependencies = []
files = ['47769']
hgrepos = []
issue_num = 25433
keywords = ['patch', 'easy']
message_count = 8.0
messages = ['253152', '257449', '257450', '314668', '314681', '315193', '324272', '347853']
nosy_count = 9.0
nosy_names = ['jwilk', 'ezio.melotti', 'docs@python', 'Dimitri Papadopoulos Orfanos', 'nitishch', 'joel.johnson', 'oulenz', '[email protected]', 'rompe']
pr_nums = ['14753', '14771', '14775']
priority = 'normal'
resolution = None
stage = 'patch review'
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue25433'
versions = ['Python 2.7', 'Python 3.7', 'Python 3.8']Linked PRs
- gh-69619: Add whitespace term to glossary and reference in
stdtypes.rst#132568 - gh-69619: Clarify whitespace definition in str.strip docs #150021
- [3.13] gh-69619: Clarify whitespace definition in str.strip docs (GH-150021) #150047
- [3.14] gh-69619: Clarify whitespace definition in str.strip docs (GH-150021) #150048
- [3.15] gh-69619: Clarify whitespace definition in str.strip docs (GH-150021) #150049
- gh-69619: Clarify
str.{l,r}strip()documentation #150901 - gh-69619: Link to isspace() in the strip() and split() documentation #155444
- [3.15] gh-69619: Link to isspace() in the strip() and split() documentation (GH-155444) #155667
- [3.14] gh-69619: Link to isspace() in the strip() and split() documentation (GH-155444) #155668
- [3.13] gh-69619: Link to isspace() in the strip() and split() documentation (GH-155444) #155669
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsTodo
Activity
DimitriPapadopoulosOrfanos commentedon Oct 18, 2015
The documentation of strip() / lstrip() / rstrip() should define "whitespace" more precisely.
The Python 3 documentation refers to "ASCII whitespace" for bytes.strip() / bytes.lstrip() / bytes.rstrip() and "whitespace" for str.strip() / str.lstrip() / str.rstrip(). I suggest the following improvements:
The Python 2 documentation refers to plain "whitespace". As far as I know strip() removes ASCII whitespaces only. If so, please:
DimitriPapadopoulosOrfanos commentedon Jan 4, 2016
In Python 2, as far as I can understand, string.whitespace and str.isspace() are different:
Functions strip()/lstrip()/rstrip() use str.isspace() and have nothing to do with string.whitespace:
Therefore I suggest the documentation of Python 2.7 points to str.isspace() wherever the term "whitespace" is used in the documentation - including this specific case of strip()/lstrip()/rstrip().
DimitriPapadopoulosOrfanos commentedon Jan 4, 2016
In Python 3 the situation is similar:
Therefore I suggest the documentation of Python 3 points to str.isspace() wherever the term "whitespace" is used in any documentation related to strings - including this specific case of strip()/lstrip()/rstrip().
joelsmithjohnson commentedon Mar 29, 2018
I have started working on this and will have a pull request submitted by the end of the week.
The term "whitespace" appears in several contextual situations throughout the documentation. While all situations would benefit from the definition of "whitespace" contained in the str.isspace() documentation, not all of the situations would benefit from a link to str.isspace() whose primary goal is to document the str.isspace() function and not to provide a global definition of what a whitespace character is.
Therefore I suggest the documentation of Python 3 create a new glossary definition of "whitespace" (which contains the definition currently in the str.isspace() documentation) and is pointed to wherever the term "whitespace" is used in any documentation related to strings - including this specific case of strip()/lstrip()/rstrip().
DimitriPapadopoulosOrfanos commentedon Mar 30, 2018
I agree on avoiding a link to str.isspace() and defining "whitespace" instead.
However please note there are many de facto definitions of "whitespace". All of them must be documented - or at least the conceptual classes of "whitespace" and clarify which class each of the following belongs to:
Unicode whitespaces are by very far the most common: str.isspace(), strip()/lstrip()/rstrip(), Py_UNICODE_ISSPACE.
Py_ISSPACE targets byte/bytearray but is never used!
bytearray.strip() does not use Py_ISSPACE but a hardcoded list of ASCII whitespaces instead.
finally string.whitespace is probably equivalent to the list used by bytearray.strip().
Beyond the docs, I think Python 3 should rationalize bytearray.strip() / Py_ISSPACE / string.whitespace, probably having bytearray.strip() rely on Py_ISSPACE, and Py_ISSPACE rely on string.whitespace unless string.whitespace is obsoleted.
oulenz commentedon Apr 11, 2018
Slightly tangential, but it would be great if the documentation of lstrip() and rstrip() could include an equivalent definition in terms of re.sub(), e.g.:
lstrip(foo) == re.sub(r'(?u)\A\s*', '', foo)
rstrip(foo) == re.sub(r'(?u)\s*\Z', '', foo)
(Or whatever else is correct.)
eedmunds commentedon Aug 28, 2018
Not sure how helpful it would be to have the re.sub expressions for lstrip and rstrip, but I think it would look like:
rompe commentedon Jul 13, 2019
Using a re.sub() call as documentation:
I have created a pull request that aligns the strip methods of bytearray to those of bytes objects. The implementation found there is cleaner and even a little bit faster.
Current master:
./python -m timeit -s 'bla = bytearray(b" foo ")' 'bla.lstrip()'
1000000 loops, best of 5: 245 nsec per loop
./python -m timeit -s 'bla = bytearray(b" foo ")' 'bla.rstrip()'
1000000 loops, best of 5: 245 nsec per loop
./python -m timeit -s 'bla = bytearray(b" foo ")' 'bla.strip()'
1000000 loops, best of 5: 260 nsec per loop
Using my patch:
./python -m timeit -s 'bla = bytearray(b" foo ")' 'bla.lstrip()'
1000000 loops, best of 5: 235 nsec per loop
./python -m timeit -s 'bla = bytearray(b" foo ")' 'bla.rstrip()'
1000000 loops, best of 5: 235 nsec per loop
./python -m timeit -s 'bla = bytearray(b" foo ")' 'bla.strip()'
1000000 loops, best of 5: 239 nsec per loop
I have also updated the documentation, adding "whitespace" to the glossary and linking to it from many places in the documentation of standard types.
10 remaining items
[3.13] gh-69619: Clarify whitespace definition in str.strip docs (GH-…
pythongh-69619: Define whitespace for str.lstrip() and str.rstrip()
pythongh-69619: also inline the str.isspace note in str.strip
gh-69619: Clarify "whitespace" in the `str.{l,r}strip()` documentation (
pythongh-69619: Clarify "whitespace" in the `str.{l,r}strip()` docume…
gh-69619: Link to isspace() in the strip() and split() documentation (G…
[3.13] gh-69619: Link to isspace() in the strip() and split() documen…
[3.14] gh-69619: Link to isspace() in the strip() and split() documen…
pythongh-69619: Link to isspace() in the strip() and split() document…
[3.15] gh-69619: Link to isspace() in the strip() and split() documen…