Skip to content

Instantly share code, notes, and snippets.

@metatoaster
Last active June 22, 2022 01:49
Show Gist options
  • Save metatoaster/c2d6757963df13f7e5584f36a4e9f49f to your computer and use it in GitHub Desktop.
Save metatoaster/c2d6757963df13f7e5584f36a4e9f49f to your computer and use it in GitHub Desktop.
class SomeClassToMock(object):
def __init__(self, key=None):
self.key = key
old_new, SomeClassToMock.__new__ = SomeClassToMock.__new__, lambda: None
SomeClassToMock.__new__ = old_new
SomeClassToMock(key='value')
$ python2.7 python_dunder_new_assignmment_bug.py && echo $?
0
$ python3.2 python_dunder_new_assignmment_bug.py && echo $?
0
$ python3.3 python_dunder_new_assignmment_bug.py && echo $?
Traceback (most recent call last):
File "python_dunder_new_assignmment_bug.py", line 7, in <module>
SomeClassToMock(key='value')
TypeError: object() takes no parameters
$ python3.6 python_dunder_new_assignmment_bug.py && echo $?
Traceback (most recent call last):
File "python_dunder_new_assignmment_bug.py", line 7, in <module>
SomeClassToMock(key='value')
TypeError: object() takes no parameters
$ python3.7 python_dunder_new_assignmment_bug.py && echo $?
Traceback (most recent call last):
File "python_dunder_new_assignmment_bug.py", line 7, in <module>
SomeClassToMock(key='value')
TypeError: object.__new__() takes exactly one argument (the type to instantiate)
$ python3.10 python_dunder_new_assignmment_bug.py && echo $?
Traceback (most recent call last):
File "python_dunder_new_assignmment_bug.py", line 7, in <module>
SomeClassToMock(key='value')
TypeError: object.__new__() takes exactly one argument (the type to instantiate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment