Skip to content

py/objstr: Add str.removesuffix() and str.removeprefix() methods. #16821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

glenn20
Copy link
Contributor

@glenn20 glenn20 commented Feb 26, 2025

Summary

Add str.removesuffix() and str.removeprefix() methods to the str type.

These methods are used to remove a suffix or prefix from a string if it exists. If the suffix or prefix does not exist in the string, the original string is returned.

This provides compatibility with Python 3.9+ and supports ruff rule FURB188 (thanks @cclauss).

The methods are conditionally compiled based on the MICROPY_PY_BUILTINS_STR_REMOVESUFFIX C macro (which is enabled by default in MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES builds).

Testing

Tests are provided in tests/basics/string_remove{pre,suf}fix.py. These have been tested and passing in the unix port.

Trade-offs and Alternatives

This PR provides improved compatibility with cpython (and ruff code rules) at the expense of increased code size. Both methods share a common implementation to reduce the impact on code size.

Copy link

codecov bot commented Feb 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.53%. Comparing base (3f0dd13) to head (b58e487).
Report is 53 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #16821   +/-   ##
=======================================
  Coverage   98.53%   98.53%           
=======================================
  Files         169      169           
  Lines       21822    21836   +14     
=======================================
+ Hits        21502    21516   +14     
  Misses        320      320           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Add `str.removesuffix()` and `str.removeprefix()` methods to the `str`
type. These methods are used to remove a suffix or prefix from a string
if it exists. If the suffix or prefix does not exist in the string, the
original string is returned.

This provides compatibility with Python 3.9+.

Signed-off-by: Glenn Moloney <[email protected]>
@glenn20 glenn20 force-pushed the feature-str-removesuffix branch from e8f5f89 to a517a6b Compare February 26, 2025 00:52
Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:  +712 +0.083% standard[incl +64(data)]
      stm32:  +224 +0.057% PYBV10
     mimxrt:  +224 +0.061% TEENSY40
        rp2:  +224 +0.024% RPI_PICO_W
       samd:  +224 +0.084% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:  +258 +0.057% VIRT_RV32

Add `tests/basics/{bytes,bytearray}_remove{prefix,suffix}.py` to test
the new `removeprefix()` and `removesuffix()` methods for bytes and
bytearray objects.

Signed-off-by: Glenn Moloney <[email protected]>
@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Mar 13, 2025
@dpgeorge
Copy link
Member

Thanks for making this PR.

It's a lot of code size for a relatively obscure (?) or at least new feature. Since these methods were added in Python 3.9 there's not that much code out there using them.

Are these necessary for MicroPython to have? Apart from ruff having a rule that suggests to use these methods, is there any other motivation for adding them?

I guess it doesn't hurt to add them but under a high feature level like FULL_FEATURES. Although that means users can't really rely on them to write portable code (portable to many MicroPython platforms).

Relevant PEP: https://peps.python.org/pep-0616/

@glenn20
Copy link
Contributor Author

glenn20 commented Jun 26, 2025

Thanks for making this PR.

It's a lot of code size for a relatively obscure (?) or at least new feature. Since these methods were added in Python 3.9 there's not that much code out there using them.

I'm happy to not merge this PR as I agree that the pros/cons weighs against inclusion. I built it, as it was an easy follow-on from #16812 (Support tuples as pre/suffix in startswith() and endswith()) and was suggested by @cclauss. I couldn't find a more compact implementation in the end and thought I'd just defer the decision to you ;).

I agree that its probably not worth including if it is not going to be available on the great majority of ports.

I'm happy for you to close this PR if you prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants