From 5404ce3ffde0bff1df5399197d50cd208fc15a52 Mon Sep 17 00:00:00 2001 From: John DiMatteo Date: Mon, 18 Oct 2021 15:12:24 -0600 Subject: [PATCH] Support builtin str removeprefix/removesuffix These are new in Python 3.9: https://docs.python.org/3/library/stdtypes.html#str.removeprefix --- pytype/stubs/builtins/builtins.pytd | 2 ++ pytype/tests/test_builtins4.py | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/pytype/stubs/builtins/builtins.pytd b/pytype/stubs/builtins/builtins.pytd index 10ef53cd5..ccc71564a 100644 --- a/pytype/stubs/builtins/builtins.pytd +++ b/pytype/stubs/builtins/builtins.pytd @@ -304,6 +304,8 @@ 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]: ... + 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: ... diff --git a/pytype/tests/test_builtins4.py b/pytype/tests/test_builtins4.py index c55252e3c..9223137ac 100644 --- a/pytype/tests/test_builtins4.py +++ b/pytype/tests/test_builtins4.py @@ -286,6 +286,18 @@ def test_str_join(self): h = ... # type: str """) + 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