Skip to content

WIP: bpo-39939: Use removeprefix() and removesuffix() - #19455

Closed
vstinner wants to merge 3 commits into
python:masterfrom
vstinner:WIP_use_pep616
Closed

WIP: bpo-39939: Use removeprefix() and removesuffix()#19455
vstinner wants to merge 3 commits into
python:masterfrom
vstinner:WIP_use_pep616

Conversation

@vstinner

@vstinner vstinner commented Apr 10, 2020

Copy link
Copy Markdown
Member

DON'T MERGE THIS PR! PR only written to see how the PEP 616 could be
used in the stdlib.

https://bugs.python.org/issue39939

DON'T MERGE THIS PR! PR only written to see how the PEP 616 could be
used in the stdlib.
@vstinner

Copy link
Copy Markdown
Member Author

I created this PR using my pyreplace.py tool: https://github.com/vstinner/misc/blob/master/bin/pyreplace.py

Replace if x.startswith(pattern): x = x[len(pattern):] and if x.endswith(pattern): x = x[:-len(pattern)]:

pyreplace.py \
  'if <expr>.startswith(<expr>): <expr> = <expr>[<regex:[^:]+>:]' \
  '<1> = <1>.removeprefix(<2>)' \
  $(find -name "*.py")

pyreplace.py \
  'if <expr>.endswith(<expr>): <expr> = <expr>[:-<regex:[^]]+>]' \
  '<1> = <1>.removesuffix(<2>)' \
  $(find -name "*.py")

Then I reverted changes which produced invalid code.

I also used an inaccurate regex to discover a few more cases:

pyreplace.py \
  'if <expr>.endswith(<expr>): <regex:.*><expr>[:-<regex:[^]]+>]' \
  '<1> = <1>.removesuffix(<2>)' \
  $(find -name "*.py")

pyreplace.py \
  'if <expr>.startswith(<expr>): <regex:.*><expr>[<regex:[^:]+>:]' \
  '<1> = <1>.removeprefix(<2>)' \
  $(find -name "*.py")

I picked the very few changes which are valid, and revert all other changes.

To finish, I used examples of the PEP 616.

@sweeneyde

Copy link
Copy Markdown
Member

I just made PR at vstinner#3 with a few more examples.

More examples of removeprefix() and removesuffix()
@vstinner vstinner closed this Apr 20, 2020
@vstinner
vstinner deleted the WIP_use_pep616 branch April 20, 2020 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants