How to remove unused function parameters in shutil.rmtree file

This question answers the question of how to delete read-only files. This is super efficient, but requires unused parameters. In this other question , the question was asked how to tell pylint that several non-contiguous parameters are not used without adding a specific comment (e.g. using _ ). Many of the answers were roughly β€œZOMG, YOU DESIGN WRONG”, so I promised that I would give an example where it is needed and out of control. Here is an example.

 shutil.rmtree(self._temp_dir, onerror=del_rw) def del_rw(action, name, exc): os.chmod(name, stat.S_IWRITE) os.remove(name) 

"Answer" so that pylint does not complain about action and exc on

 shutil.rmtree(self._temp_dir, onerror=del_rw) def del_rw(_action, name, _exc): os.chmod(name, stat.S_IWRITE) os.remove(name) 

but a new question: how to do this without having _action or _exc as parameters?

+1
python pylint
source share
1 answer

As discussed in the comments, you cannot just ignore action and exc because rmtree will pass these arguments to the callback. From python docs :

If onerror provided, it must be a callable that takes three parameters: function , path and excinfo .

However, you have several options:

  • You can prefix the callback with cb_ (see pylint docs about this), turning your function into

     shutil.rmtree(self._temp_dir, onerror=cb_del_rw) def cb_del_rw(action, name, exc): os.chmod(name, stat.S_IWRITE) os.remove(name) 
  • You can use keyword arguments (you can also use *args , but I find this approach more readable):

     shutil.rmtree(self._temp_dir, onerror=del_rw) def del_rw(**kwargs): name = kwargs['name'] os.chmod(name, stat.S_IWRITE) os.remove(name) 
+1
source share

All Articles