0

I am currently trying to get my python code to be compatible to PEP8. For the following code pylint tells me that the type_list variable in filter_not is problematic because it is never used. While this is of course true I am not sure how to fix this. Is there a better way to do this or is this just a false positive?

Note that the code below is just an extract. There are actually far more functions in there which the switcher_filter handles. So an regular if statement would get far to big. Also I am not using an enum for the switcher option as the main function is called from outside and I do not want to import the enum into every file calling the module.

def filter_by_x(df_input, type_list):
    return df_input[lambda df: df["x"].isin(type_list)]

def filter_not(df_input, type_list):
    return df_input

switcher_filter = {
    'not':  filter_not(removed_duplicates_df, type_list),
    'x': filter_by_x(removed_duplicates_df, type_list)

filtered_df = switcher_filter.get(filterby)
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Manuel
  • 649
  • 5
  • 22
  • See: https://stackoverflow.com/questions/35469253/silence-pylint-warning-about-unused-variables-for-string-interpolation. One way is to rename to `_type_list`: the leading underscore should stop pylint marking it as unused – slothrop Jul 28 '22 at 07:51
  • I'm not sure what's your problem to remove the argument... Just call it as `'not': filter_not(removed_duplicates_df)`... – Tomerikoo Jul 28 '22 at 07:52
  • @Tomerikoo consistent interface might be an important factor here. – matszwecja Jul 28 '22 at 07:53
  • 2
    Why are you calling your functions inside the dict anyway? If you have so many then you are literally calling all of them. Did you mean to do `'not': filter_not` and only call it after the `get`? – Tomerikoo Jul 28 '22 at 07:54
  • @matszwecja Of course. Then it should be reflected in a [mre]... – Tomerikoo Jul 28 '22 at 07:54
  • Also the `get` call is inside the actual dict... – Tomerikoo Jul 28 '22 at 07:54

1 Answers1

1

For your example why not do this:

def filter_by_x(df_input, type_list):
    return df_input[lambda df: df["x"].isin(type_list)]

def filter_not(df_input):
    return df_input

switcher_filter = {
    'not':  filter_not(removed_duplicates_df),
    'x': filter_by_x(removed_duplicates_df, type_list)
    filtered_df = switcher_filter.get(filterby)
}

In the general case there's a setting in pylint to ignore some variable that you know are unused. Namely a leading underscore, a leading unused or a leading ignored (by default).

This is configurable with:

# Argument names that match this expression will be ignored.
ignored-argument-names=_.*|^ignored_|^unused_
Pierre.Sassoulas
  • 3,733
  • 3
  • 33
  • 48