Skip to content

Commit e5ed57e

Browse files
committed
gh-146406: add clear() cross-language hint for immutable types
Follow-up to PR #146407. Calling .clear() on a tuple, frozenset, or frozendict now suggests the mutable counterpart (list / set / dict), mirroring the existing append/extend/insert/remove/discard/update entries for the same immutable types. Triggered by @henryiii noticing that .clear() was the first method they tried on these types and got no useful suggestion, and @vstinner agreeing it was worth adding. Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
1 parent 5f8d9d3 commit e5ed57e

3 files changed

Lines changed: 10 additions & 0 deletions

File tree

Lib/test/test_traceback.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4637,6 +4637,9 @@ def test_cross_language_mutable_on_immutable(self):
46374637
(frozenset, 'remove', "Did you mean to use a 'set' object?"),
46384638
(frozenset, 'update', "Did you mean to use a 'set' object?"),
46394639
(frozendict, 'update', "Did you mean to use a 'dict' object?"),
4640+
(tuple, 'clear', "Did you mean to use a 'list' object?"),
4641+
(frozenset, 'clear', "Did you mean to use a 'set' object?"),
4642+
(frozendict, 'clear', "Did you mean to use a 'dict' object?"),
46404643
]
46414644
for test_type, attr, expected in cases:
46424645
with self.subTest(type=test_type.__name__, attr=attr):

Lib/traceback.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,10 @@ def print(self, *, file=None, chain=True, **kwargs):
17751775
# frozendict -- mutable method on immutable type (user expected a dict)
17761776
"update": ((frozenset, "Did you mean to use a 'set' object?", True),
17771777
(frozendict, "Did you mean to use a 'dict' object?", True)),
1778+
# clear() -- shared across immutable container types (user expected the mutable counterpart)
1779+
"clear": ((tuple, "Did you mean to use a 'list' object?", True),
1780+
(frozenset, "Did you mean to use a 'set' object?", True),
1781+
(frozendict, "Did you mean to use a 'dict' object?", True)),
17781782
# float -- bitwise operators belong to int
17791783
"__or__": ((float, "Did you mean to use an 'int' object? Bitwise operators are not supported by 'float'.", True),),
17801784
"__and__": ((float, "Did you mean to use an 'int' object? Bitwise operators are not supported by 'float'.", True),),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add cross-language hints for ``.clear()`` on :class:`tuple`,
2+
:class:`frozenset`, and :class:`frozendict`, suggesting the mutable
3+
counterpart. Follow-up to :gh:`146406`.

0 commit comments

Comments
 (0)