I am trying to fix the complaints filed by codacy on this PR https://github.com/ESMValGroup/ESMValTool/pull/1186
A first (beginner) question that I have would be if it is possible to replicate the codacy checks on my local machine? I noted that prospector (locally) does not complain about certain aspects, whereas codacy (online) does. This means that trying to fix stuff is a bit cumbersome, with pushing and waiting for codacy to run. In my current case, codacy seems stricter than prospector.
The current aspect that I am dealing with is unused variables. When dealing with a function where e.g. only one of the returned arguments is used, it is easy to get around this. As an example:
This fails in the case weekday is not used.
weekday, ndays_in_month = calendar.monthrange(2010, 1)
But prospector and codacy accept usage of 'dummy'. E.g. the following works fine.
dummy_weekday, ndays_in_month = calendar.monthrange(2010, 1)
However in the PR referenced above, the problem comes up in my custom callback function that is passed to Iris when reading the dataset (@bjlittle , maybe this is also something to consider in how the function is designed?). In this function, I do not use the argument filename (which is required by Iris) and I do not want to use it. Prospector accepts this, but I do not get rid of the issues by Codacy. I tried a few:
def duveiller2018_callback_function(cube, field, filename):
"""Dataset specific callback function.
This is a dataset specific callback function that deals with correct
handling of the time axis and time_bnds
"""
# First deal with unused filename variable (this is needed by Iris,
# but codacy will complain if it is not used, so simply put it here,
# but do nothing.)
filename
# Rename 'Month' to 'time'
cube.coord('Month').rename('time')
def duveiller2018_callback_function(cube, field, filename):
"""Dataset specific callback function.
This is a dataset specific callback function that deals with correct
handling of the time axis and time_bnds
"""
# First deal with unused filename variable (this is needed by Iris,
# but codacy will complain if it is not used, so simply put it here,
# but do nothing.)
dummy = filename
# Rename 'Month' to 'time'
cube.coord('Month').rename('time')
def duveiller2018_callback_function(cube, field, filename):
"""Dataset specific callback function.
This is a dataset specific callback function that deals with correct
handling of the time axis and time_bnds
"""
# First deal with unused filename variable (this is needed by Iris,
# but codacy will complain if it is not used, so simply put it here,
# but do nothing.)
if filename:
pass
# Rename 'Month' to 'time'
cube.coord('Month').rename('time')
Any hints on how to get around this? Or should we go with one of these solutions and neglect the codacy complaints?
A first (beginner) question that I have would be if it is possible to replicate the codacy checks on my local machine? I noted that prospector (locally) does not complain about certain aspects, whereas codacy (online) does.
Codacy also runs prospector, so I would expect the results to be very similar. Please make sure that you run prospector from the root of the repository, so it picks up the .prospector.yml configuration file. If this doesn't help, can you give more specific examples?
This fails in the case weekday is not used.
weekday, ndays_in_month = calendar.monthrange(2010, 1)
But prospector and codacy accept usage of 'dummy'. E.g. the following works fine.
dummy_weekday, ndays_in_month = calendar.monthrange(2010, 1)
I would recommend simply writing
ndays_in_month = calendar.monthrange(2010, 1)[1]
instead of the above
However in the PR referenced above, the problem comes up in my custom callback function ...
The convention is to use a single underscore _ for variables that have to be there, but are never used, e.g.
def duveiller2018_callback_function(cube, _, _):
and I would also recommend giving that function a more descriptive name, e.g. fix_time_coordinate
Everything clear. Thanks @bouweandela