Skip to content
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

Support builtin str removeprefix/removesuffix #1033

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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