I work with library that has function with signature f(*args, **kwargs)
.
I need to pass python dict in kwargs argument, but dict contains not strings in keywords
f(**{1: 2, 3: 4})
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: f() keywords must be strings
How can I get around this without editing the function?
I think the best you can do is filter out the non-string arguments in your dict:
The reason is because keyword arguments must be strings. Otherwise, what would they unpack to on the other side?
Alternatively, you could convert your non-string keys to strings, but you run the risk of overwriting keys:
-- Consider what would happen if you started with:
Non-string keyword arguments are simply not allowed, so there is no general solution to this problem. Your specific example can be fixed by converting the keys of your
dict
to strings: