Is there a python syntax checker that can ignore ' assigned to but never used' when locals() is present?

StackOverflow https://stackoverflow.com/questions/14349239

  •  15-01-2022
  •  | 
  •  

Question

suppose I have the code

def foo():
    bar = 1
    wibble = 3
    return locals()

my current syntax checker (flake8 with syntastic.vim) will throw a 'assigned to but never used' error on both variables. However locals() implies something is they are in fact being used, if not explicitly.

def foo():
    bar = 1
    wibble = 3 # <-- I still want this to throw as it is definitely not being used
    return bar

Is there any python checker or custom setting that will be locals() aware and lenient?

EDIT:

This is a quick and dirty solution for vim/syntastic/flake8 that will suppress the warning in your .vimrc

"Ignore unused variable warnings whenever locals() is being used in a file                                                              
function! LocalsWarningSuppress()                                              
    if ( search("locals()",'nw') > 0)                                          
        let g:syntastic_python_checker='flake8 --ignore=W806'                  
    else                                                                       
        let g:syntastic_python_checker='flake8'                                
    endif                                                                      
endfunction 

au BufWritePre **/(filter_pattern)*py call LocalsWarningSuppress()                 
Was it helpful?

Solution

No. Even pylint, the most powerful and nitpicky Python linter I'm aware of, isn't clever enough to detect this case. But if it were, it would probably complain that you're using locals() in the first place. :)

On the other hand, and unlike pyflakes, pylint does support magic comments to ignore specific problems. But I must warn you that pylint is extremely picky (and thus slow) out of the box, so you'll want to spend a few minutes upfront to cut its list of checks down to just the things you actually care about.

There is a ticket closed as wontfix for improving this behavior in the specific case of string formatting. It appears that pylint developers don't want to implement this as a feature.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top