2

For some reason PyCharm is telling me that 3 of my 5 list are not used in my function but they in fact are being used in the function and the code completes with the expected results.

This is odd behavior. Is this a bug?

I have seen some issues for f-strings reporting false positives but I do not think this is the same issue here.

   def filter_for_vzb_vzt(self, query_results):
        vzb_list = []
        vzt_list = []
        vzt_analyst_tkr = []
        vzb_analyst_tkr = []
        vzb_lpc_analyst_tkr = []

        with open('./Files/{}.json'.format('VZT_ACNA_LIST'), 'r') as df:
            vzt_analyst_tkr = json.load(df)

        with open('./Files/{}.json'.format('VZB_TAX_ACNA_LIST'), 'r') as df:
            vzb_analyst_tkr = json.load(df)

        with open('./Files/{}.json'.format('VZB_LPC_ACNA_LIST'), 'r') as df:
            vzb_lpc_analyst_tkr = json.load(df)

        self.process_vzb_mass(vzb_list, vzb_analyst_tkr, vzb_lpc_analyst_tkr)
        self.process_vzt_mass(vzt_list, vzt_analyst_tkr)
        self.active_time = False

enter image description here

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • 4
    What happens if you replace `vzt_analyst_tkr = json.load(df)` with `vzt_analyst_tkr.append(0)`? My guess is that you never use it before reassigning it, so PyCharm considers it to be unused. You may as well not declare it at the top, as `with` statements don't create their own scope in Python. – Michael Kolber Aug 05 '19 at 20:46
  • @MichaelKolber that makes sense. That is probably the reason for the notice. Good to know with statements do not create their own scope as well. Ty. It is hard to break the habit of always declaring all my variables at the top of a function. But I dislike the annoying notice more so I will remove those 3 lines before `with`. – Mike - SMT Aug 05 '19 at 20:49
  • No problem. As a general rule, Python does not have [block-level scope](https://stackoverflow.com/a/6167952/2555108). – Michael Kolber Aug 05 '19 at 20:50
  • For my personal elucidation, those 3 variables don't need to be created as `lists`, correct? – Trenton McKinney Aug 05 '19 at 20:55
  • @Trenton_M no they dont but for readability it helps the reader to know those variables are list without having to see what the with open statements are generating. – Mike - SMT Aug 05 '19 at 21:05
  • @Mike-SMT I absolutely disagree regarding readability. At the very least, it is *not idiomatic python*, and this would look strange anyone who writes a lot of python. Just like the linter, they would say "why are they creating completely unnecessary lists they never use?". Important to note, Python *doesn't have variable declarations*. – juanpa.arrivillaga Aug 05 '19 at 21:10
  • How about just adding them to the docstring? – Trenton McKinney Aug 05 '19 at 21:15
  • @Trenton_M even better would be to use [*type annotations*](https://www.python.org/dev/peps/pep-0484/) and then PyCharm can even run static-time type checking! (or you can use `mypy` for static-time type checking) – juanpa.arrivillaga Aug 05 '19 at 21:15
  • Aren't type annotations just for the parameters `def filter_for_vzb_vzt(self, query_results: str) -> str:` for example, not variables inside the method? – Trenton McKinney Aug 05 '19 at 21:17
  • @Trenton_M no, you can type annotate variables. so here, instead of `vzb_list = []` you would just do `vzb_list: list`, or using the `typing` module:, `vzb_list: typing.List[str]` if you wanted to indicate a list of strings... – juanpa.arrivillaga Aug 05 '19 at 21:30
  • `vzb_list: list` is what I did, but inside the docstring, didn't realize it could be outside the docstring – Trenton McKinney Aug 05 '19 at 21:33

1 Answers1

1

You are indeed not using those values. Read carefully the message:

local variable <...> value is not used

PyCharm is being very specific - you are assigning these three variables a new list with

<...> = json...

The original empty lists that these variables pointed to are then discarded.

These assignments are not conditional, so you will never use those empty lists - PyCharm is warning you you are not using the empty lists, rather than the variables themselves. You should probably just delete those lines - common practice is to first define variables with values that will actually be used.

kabanus
  • 24,623
  • 6
  • 41
  • 74