Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
align behaviour of __replace__
  • Loading branch information
eendebakpt committed Sep 16, 2023
commit c97105f0012f7e8380e469e81f25b56322bf50b4
4 changes: 2 additions & 2 deletions Lib/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def replace(obj, /, **changes):
frozen dataclasses.
"""
cls = obj.__class__
func = getattr(cls, '__replace__', None)
if func is None:
func = getattr(cls, '__replace__', _NoValue)
if func is _NoValue:
raise TypeError(f"replace() does not support {cls.__name__} objects")
return func(obj, **changes)
7 changes: 7 additions & 0 deletions Lib/test/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,13 @@ class C:
with self.assertRaisesRegex(TypeError, 'unexpected keyword argument'):
copy.replace(c, x=1, error=2)

def test_invalid_replace_method(self):
class A:
__replace__ = None
a = A()
with self.assertRaises(TypeError):
copy.replace(a)


def global_foo(x, y): return x+y

Expand Down