_str_methods.py 732 B

12345678910111213141516171819202122232425262728
  1. """
  2. Python3.9 introduces removesuffix and remove prefix.
  3. They're reimplemented here for use in Python3.8.
  4. NOTE: when pyupgrade --py39-plus removes nearly everything in this file,
  5. this file and the associated tests should be removed.
  6. """
  7. from __future__ import annotations
  8. import sys
  9. if sys.version_info < (3, 9):
  10. def removesuffix(string: str, suffix: str) -> str:
  11. if string.endswith(suffix):
  12. return string[: -len(suffix)]
  13. return string
  14. def removeprefix(string: str, prefix: str) -> str:
  15. if string.startswith(prefix):
  16. return string[len(prefix) :]
  17. return string
  18. else:
  19. # NOTE: remove this file when pyupgrade --py39-plus removes
  20. # the above block
  21. pass