Skip to content

Mutable object as a parameter default #41

@0xsirsaif

Description

@0xsirsaif

Hi, two methods of the FrappeClient class use {} as a default value for its parameters params. 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
These methods are:

  • get_api
  • post_api

Solution:

def get_api(self, method, params=None):
    if params is None:
        params = {}
    else:
        params = dict(params)
    res = self.session.get(self.url + '/api/method/' + method + '/', params=params)
    return self.post_process(res)

def post_api(self, method, params=None):
    if params is None:
        params = {}
    else:
        params = dict(params)
    res = self.session.post(self.url + '/api/method/' + method + '/', params=params)
    return self.post_process(res)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions