New submission from Richard Neumann <[email protected]>:
When sublassing the built-in list, the invocation of super().__new__ will
unexpectedly expand the passed arguments:
class MyTuple(tuple):
def __new__(cls, *items):
print(cls, items)
return super().__new__(cls, items)
class MyList(list):
def __new__(cls, *items):
print(cls, items)
return super().__new__(cls, items)
def main():
my_tuple = MyTuple(1, 2, 3, 'foo', 'bar')
print('My tuple:', my_tuple)
my_list = MyList(1, 2, 3, 'foo', 'bar')
print('My list:', my_list)
if __name__ == '__main__':
main()
Actual result:
<class '__main__.MyTuple'> (1, 2, 3, 'foo', 'bar')
My tuple: (1, 2, 3, 'foo', 'bar')
<class '__main__.MyList'> (1, 2, 3, 'foo', 'bar')
Traceback (most recent call last):
File "/home/neumann/listbug.py", line 24, in <module>
main()
File "/home/neumann/listbug.py", line 19, in main
my_list = MyList(1, 2, 3, 'foo', 'bar')
TypeError: list expected at most 1 argument, got 5
Expected:
<class '__main__.MyTuple'> (1, 2, 3, 'foo', 'bar')
My tuple: (1, 2, 3, 'foo', 'bar')
<class '__main__.MyList'> (1, 2, 3, 'foo', 'bar')
My list: [1, 2, 3, 'foo', 'bar']
----------
components: ctypes
messages: 383902
nosy: conqp
priority: normal
severity: normal
status: open
title: super().__new__() of list expands arguments
type: behavior
versions: Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue42768>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com