This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: str.removeprefix(): add strict: bool
Type: enhancement Stage:
Components: Interpreter Core, Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, JelleZijlstra, jack__d, lemburg, socketpair, terry.reedy
Priority: normal Keywords:

Created on 2021-06-28 09:58 by socketpair, last changed 2022-04-11 14:59 by admin.

Messages (7)
msg396627 - (view) Author: Марк Коренберг (socketpair) * Date: 2021-06-28 09:58
#39939 introduced .removeprefix for several classes. I propose adding kwargs-only parameter `, *, strict: bool = False`. It should raise some exception (I'm unuse what exactly) if the string DOES NOT start from specified prefix. False by default only for compatibility purposes. Personally I think it should be True, but I understand why it's impossible to change API.

The same about `removesuffix()`.
msg396651 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2021-06-28 16:45
ValueError would seem to be the right exception to use.

If this parameter were to be added (I'm not convinced that it should be), I'd prefer to call it something more specific than "strict", since "strict" can mean lots of things.
msg396652 - (view) Author: Jack DeVries (jack__d) * Date: 2021-06-28 16:53
@Jelle Zijlstra, they did think about this in the pep; see here https://www.python.org/dev/peps/pep-0616/#id26

The PEP authors suggested a kwarg of ``required`` which is certainly better. Maybe ``raise_exception`` would be even more explicit albeit verbose.

It might be a useful feature; consider the following example::

    dogs = ['mydog_spot', 'mydog_sally', 'jill']
    new_dogs = [d.removeprefix('mydog_', required=True) for d in dogs]
    
If there is support for this feature, I would like to work on it. Let me know what you think, and whether you think that ``required`` or ``raise_exception`` is a better name for the keyword argument.
msg396656 - (view) Author: Марк Коренберг (socketpair) * Date: 2021-06-28 17:48
Any kwarg is OK for me, so please do.
msg396668 - (view) Author: Jack DeVries (jack__d) * Date: 2021-06-28 23:03
@lemburg, hopefully I'm asking the right person... you're down as the devguide expert on unicode and these changes would (I think) need to be applied here::

Objects/unicodeobject.c::12814 unicode_removeprefix_impl

Anyway, do you think this would be a useful feature that you'd like to see added?
msg396687 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-06-29 00:19
Start by presenting this idea on python-ideas list.
msg396700 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2021-06-29 04:51
I'm +-0 on this. I would write something like this instead:

    assert whatever.startswith(prefix)
    result = whatever.removeprefix(prefix)

Note that if this were to change, the corresponding methods would also have to change on bytes, bytearray, and collections.UserString.
History
Date User Action Args
2022-04-11 14:59:47adminsetgithub: 88687
2021-06-29 04:51:58Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg396700
2021-06-29 00:19:46terry.reedysetnosy: + terry.reedy
messages: + msg396687
2021-06-28 23:03:57jack__dsetnosy: + lemburg
2021-06-28 23:03:32jack__dsetmessages: + msg396668
2021-06-28 17:48:07socketpairsetmessages: + msg396656
2021-06-28 16:53:53jack__dsetnosy: + jack__d
messages: + msg396652
2021-06-28 16:45:55JelleZijlstrasetnosy: + JelleZijlstra

messages: + msg396651
versions: - Python 3.9, Python 3.10
2021-06-28 09:58:50socketpaircreate