diff --git a/Lib/collections.py b/Lib/collections.py index 98c43257534..c77b08972ca 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -536,8 +536,8 @@ def subtract(self, iterable=None, **kwds): self.subtract(kwds) def copy(self): - 'Like dict.copy() but returns a Counter instance instead of a dict.' - return Counter(self) + 'Return a shallow copy.' + return self.__class__(self) def __reduce__(self): return self.__class__, (dict(self),) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index b3e0907b333..b2cad7082f1 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -812,6 +812,15 @@ def test_copying(self): self.assertEqual(len(dup), len(words)) self.assertEqual(type(dup), type(words)) + def test_copy_subclass(self): + class MyCounter(Counter): + pass + c = MyCounter('slartibartfast') + d = c.copy() + self.assertEqual(d, c) + self.assertEqual(len(d), len(c)) + self.assertEqual(type(d), type(c)) + def test_conversions(self): # Convert to: set, list, dict s = 'she sells sea shells by the sea shore' diff --git a/Misc/NEWS b/Misc/NEWS index 2c3f86de001..fbb7411bdd2 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -63,6 +63,8 @@ Library - Issue #11467: Fix urlparse behavior when handling urls which contains scheme specific part only digits. Patch by Santoso Wijaya. +- collections.Counter().copy() now works correctly for subclasses. + - Issue #11474: Fix the bug with url2pathname() handling of '/C|/' on Windows. Patch by Santoso Wijaya.