dfm / corner.py

Make some beautiful corner plots

Home Page:http://corner.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Do not mute mutable arguments

sebastian-schindler opened this issue · comments

The current implementation of corner_impl is muting its mutable arguments, like here:

if hist_kwargs is None:
    hist_kwargs = dict()
hist_kwargs["color"] = hist_kwargs.get("color", color)

The dictionary hist_kwargs as seen from the scope calling corner is changed after the call.

Minimal example:

import numpy as np
import corner
mydict = {}
corner.corner(np.array([[1,2], [3,4]]), hist_kwargs=mydict)
print(mydict)

The object mydict was changed by the call to corner, which is unexpected.

This could be resolved by simply adding hist_kwargs = dict(hist_kwargs), because then the rest of the code is working on a copy of the dictionary. Of course the user could do this as well, but it would be nicer if the user wouldn't need to bother. Also, this becomes more annoying when passing this argument as a kwarg itself, because then a copy.deepcopy becomes necessary to ensure that what is passed in stays the same.

(If this will be implemented: If the default for hist_kwargs would not be None, but [], the if checking for None wouldn't also be needed anymore, because dict([]) gives an empty dictionary.)

Thanks for the report! I'd be happy to review a PR that adds the appropriate copy, but please keep the same default value since some downstream dependencies rely on that behavior.