Skip to content

Commit

Permalink
Support builtin str removeprefix/removesuffix (#1033)
Browse files Browse the repository at this point in the history
These are new in Python 3.9: https://docs.python.org/3/library/stdtypes.html#str.removeprefix

Resolves #1033

PiperOrigin-RevId: 404695837
  • Loading branch information
jdimatteo authored and rchen152 committed Oct 21, 2021
1 parent 1769e66 commit 1b5174b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pytype/stubs/builtins/__builtin__.pytd
Expand Up @@ -304,6 +304,9 @@ class str(Sequence[str], Hashable):
def lower(self) -> str: ...
def lstrip(self, chars: Optional[str] = ...) -> str: ...
def partition(self, sep: str) -> Tuple[str, str, str]: ...
if sys.version_info >= (3, 9):
def removeprefix(self, prefix: str) -> str: ...
def removesuffix(self, suffix: str) -> str: ...
def replace(self, old: str, new: str, *args, **kwargs) -> str: ...
def rfind(self, sub: str, *args, **kwargs) -> int: ...
def rindex(self, sub: str, *args, **kwargs) -> int: ...
Expand Down
3 changes: 3 additions & 0 deletions pytype/stubs/builtins/builtins.pytd
Expand Up @@ -304,6 +304,9 @@ class str(Sequence[str], Hashable):
def lower(self) -> str: ...
def lstrip(self, chars: Optional[str] = ...) -> str: ...
def partition(self, sep: str) -> Tuple[str, str, str]: ...
if sys.version_info >= (3, 9):
def removeprefix(self, prefix: str) -> str: ...
def removesuffix(self, suffix: str) -> str: ...
def replace(self, old: str, new: str, *args, **kwargs) -> str: ...
def rfind(self, sub: str, *args, **kwargs) -> int: ...
def rindex(self, sub: str, *args, **kwargs) -> int: ...
Expand Down
13 changes: 13 additions & 0 deletions pytype/tests/test_builtins4.py
Expand Up @@ -286,6 +286,19 @@ def test_str_join(self):
h = ... # type: str
""")

@test_utils.skipBeforePy((3, 9), "removeprefix and removesuffix new in 3.9")
def test_str_remove_prefix_suffix(self):
ty = self.Infer("""
a = "prefix_suffix"
b = a.removeprefix("prefix_")
c = a.removesuffix("_suffix")
""", deep=False)
self.assertTypesMatchPytd(ty, """
a = ... # type: str
b = ... # type: str
c = ... # type: str
""")

def test_str_is_hashable(self):
self.Check("""
from typing import Any, Dict, Hashable
Expand Down

0 comments on commit 1b5174b

Please sign in to comment.