PEP 616: Fix behavior of code snippets (#1336)
This commit is contained in:
parent
7a2c2168b6
commit
dcb29f4019
28
pep-0616.rst
28
pep-0616.rst
|
@ -52,7 +52,7 @@ following behavior::
|
|||
self_str = str(self)
|
||||
|
||||
if isinstance(prefix, tuple):
|
||||
for option in prefix:
|
||||
for option in tuple(prefix):
|
||||
if not isinstance(option, str):
|
||||
raise TypeError()
|
||||
option_str = str(option)
|
||||
|
@ -79,12 +79,14 @@ following behavior::
|
|||
self_str = str(self)
|
||||
|
||||
if isinstance(suffix, tuple):
|
||||
for option in suffix:
|
||||
for option in tuple(suffix):
|
||||
if not isinstance(option, str):
|
||||
raise TypeError()
|
||||
option_str = str(option)
|
||||
|
||||
if option_str and self_str.endswith(option_str):
|
||||
if not option_str:
|
||||
return self_str[:]
|
||||
if self_str.endswith(option_str):
|
||||
return self_str[:-len(option_str)]
|
||||
|
||||
return self_str[:]
|
||||
|
@ -98,6 +100,7 @@ following behavior::
|
|||
else:
|
||||
return self_str[:]
|
||||
|
||||
|
||||
Note that without the check for the truthyness of suffixes,
|
||||
``s.cutsuffix('')`` would be mishandled and always return the empty
|
||||
string due to the unintended evaluation of ``self[:-0]``.
|
||||
|
@ -142,6 +145,7 @@ methods for control flow instead of testing the lengths as above.
|
|||
The two methods will also be added to ``collections.UserString``, with
|
||||
similar behavior.
|
||||
|
||||
|
||||
Motivating examples from the Python standard library
|
||||
====================================================
|
||||
|
||||
|
@ -231,9 +235,6 @@ cookiejar.py
|
|||
test_concurrent_futures.py
|
||||
--------------------------
|
||||
|
||||
In the following example, the meaning of the code changes slightly,
|
||||
but in context, it behaves the same.
|
||||
|
||||
- Current::
|
||||
|
||||
if name.endswith(('Mixin', 'Tests')):
|
||||
|
@ -262,9 +263,10 @@ as taking an iterable of length-1 strings. The API could therefore be
|
|||
generalized to accept any iterable of strings, which would be
|
||||
successively removed as prefixes. While this behavior would be
|
||||
consistent, it would not be obvious for users to have to call
|
||||
``'foobar'.cutprefix(('foo,))`` for the common use case of a
|
||||
``'foobar'.lstrip(('foo',))`` for the common use case of a
|
||||
single prefix.
|
||||
|
||||
|
||||
Remove multiple copies of a prefix
|
||||
----------------------------------
|
||||
|
||||
|
@ -273,19 +275,19 @@ expansion of the ``lstrip``/``rstrip`` API -- repeatedly applying the
|
|||
function until the argument is unchanged. This behavior is attainable
|
||||
from the proposed behavior via by the following::
|
||||
|
||||
>>> s = 'foobar' * 100 + 'bar'
|
||||
>>> prefixes = ('bar', 'foo')
|
||||
>>> s = 'FooBar' * 100 + 'Baz'
|
||||
>>> prefixes = ('Bar', 'Foo')
|
||||
>>> while len(s) != len(s := s.cutprefix(prefixes)): pass
|
||||
>>> s
|
||||
'bar'
|
||||
'Baz'
|
||||
|
||||
or the more obvious and readable alternative::
|
||||
|
||||
>>> s = 'foo' * 100 + 'bar'
|
||||
>>> prefixes = ('bar', 'foo')
|
||||
>>> s = 'FooBar' * 100 + 'Baz'
|
||||
>>> prefixes = ('Bar', 'Foo')
|
||||
>>> while s.startswith(prefixes): s = s.cutprefix(prefixes)
|
||||
>>> s
|
||||
'bar'
|
||||
'Baz'
|
||||
|
||||
|
||||
Raising an exception when not found
|
||||
|
|
Loading…
Reference in New Issue