jazzband / tablib

Python Module for Tabular Datasets in XLS, CSV, JSON, YAML, &c.
https://tablib.readthedocs.io/
MIT License
4.59k stars 590 forks source link

Mutable object as a parameter default #492

Closed 0xsirsaif closed 3 years ago

0xsirsaif commented 3 years ago

Hi, some methods of the Dataset class use list() as a default value for its parameters. This practice may cause some unexpected behaviors in some contexts. This problem arises because of the way python variables (names) work. When modifying these parameters (e.g appending items to it) the method still uses the same default value:

def append_item(item, data=[]):
    data.append(item)
    return data
>> append_item(1)
[1]
>> append_item(2) # [2] is the expected value 
[1, 2]

So that, the workaround is always using the None type as the default of the parameters

methods having mutable types as parameters defaults:

Note: This is my first issue

claudep commented 3 years ago

Hi @sirSaif, I didn't study that hard, but could you maybe write a patch to fix that and see how it goes in the test suite?

claudep commented 3 years ago

Fixed by eba2f6f