Skip to content

Commit 836df52

Browse files
committed
gh-161970: Reject an explicit count together with maxsplit in UserString.replace()
1 parent f0d071b commit 836df52

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

‎Lib/collections/__init__.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,9 +1615,9 @@ def partition(self, sep):
16151615
return self.data.partition(sep)
16161616

16171617
# The 'maxsplit' keyword parameter can be removed in Python 3.18.
1618-
def replace(self, old, new, count=-1, *, maxsplit=_MISSING):
1618+
def replace(self, old, new, count=_MISSING, *, maxsplit=_MISSING):
16191619
if maxsplit is not _MISSING:
1620-
if count != -1:
1620+
if count is not _MISSING:
16211621
raise TypeError("'count' and 'maxsplit' are mutually exclusive "
16221622
"and support for 'maxsplit' keyword parameter "
16231623
"is slated for removal in Python 3.18")
@@ -1630,6 +1630,8 @@ def replace(self, old, new, count=-1, *, maxsplit=_MISSING):
16301630
"as a positional argument instead",
16311631
remove=(3, 18))
16321632
count = maxsplit
1633+
elif count is _MISSING:
1634+
count = -1
16331635
if isinstance(old, UserString):
16341636
old = old.data
16351637
if isinstance(new, UserString):

‎Lib/test/test_userstring.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ def test_replace_maxsplit_deprecated(self):
218218
s.replace('a', 'b', 1, maxsplit=2)
219219
with self.assertRaisesRegex(TypeError, msg):
220220
s.replace('a', 'b', count=1, maxsplit=2)
221+
# An explicit count equal to the default still conflicts.
222+
with self.assertRaisesRegex(TypeError, msg):
223+
s.replace('a', 'b', -1, maxsplit=2)
224+
with self.assertRaisesRegex(TypeError, msg):
225+
s.replace('a', 'b', count=-1, maxsplit=2)
221226

222227

223228
if __name__ == "__main__":

0 commit comments

Comments
 (0)