hatching / triage

Hatching Triage public command-line utility and API library.
BSD 3-Clause "New" or "Revised" License
65 stars 22 forks source link

Replaces use of default mutable arguments in a function #18

Closed krnick closed 2 years ago

krnick commented 2 years ago

Hi all,

I would like to start to get familiar with the Triage codebase by trying to help deal with some problems 😄 .

In this pull request, I found that there is some usage of default mutable arguments in a function, which will lead to problems like the below code:

def func(key, value, a={}):
    a[key] = value
    return a

>>> print(func('a', 10))  # that's expected
{'a': 10}
>>> print(func('b', 20))  # that could be unexpected
{'b': 20, 'a': 10}

To deal with that, we can put None in arguments and then check this argument within the function.

Reference: [1] https://docs.sourcery.ai/suggestions/default-mutable-arg/ [2] https://docs.python-guide.org/writing/gotchas/ [3] https://stackoverflow.com/questions/41686829/warning-about-mutable-default-argument-in-pycharm

jbremer commented 2 years ago

Good catch - thanks! :-)