3

I had this code:

while True:
    cmd = input('> ')
    if cmd == 'exit':
        break

But I wanted to implement advanced text input features like command history so I imported the readline module. Importing the readline module (and not even using it) will unlock these features. This code works perfectly:

import readline

while True:
    cmd = input('> ')
    if cmd == 'exit':
        break

My problem (or maybe just annoyance) is that PyCharm gives me a non-fatal warning that I have an unused import statement. I assume this is just a simple fault in PyCharm for not realizing that the readline import WILL be used if you use the builtin input function.

What is the cleanest way for me to get rid of this warning? Also, is this a bug that PyCharm ought to fix?

YulkyTulky
  • 886
  • 1
  • 6
  • 20

1 Answers1

1

PyCharm linting only goes so far, but you can at least supress the warning by writing:

# noinspection PyUnresolvedReferences
import readline

You can access general warning configurations in Settings > Editor > Inspections.

Also if you ever need to suppress a warning type, but just for a single line/function instead of the whole file, do this:

  • place the cursor where the warning is
  • press Alt+Enter
  • press right arrow key on the "light bulb icon"
  • press Enter on the "suppress for class/function/statement"

PyCharm automatically adds the appropriate comment that ignores the warning type caused by that statement.

enter image description here

jfaccioni
  • 7,099
  • 1
  • 9
  • 25
  • 2
    Thanks for the answer! I wish that PyCharm would not give this warning and I'm not too fond of adding a comment to my code just to fix its problem. Instead, I think I might add the line ```readline.set_auto_history(True)``` which doesn't actually change anything but does explicitly use the readline module. – YulkyTulky Apr 17 '20 at 19:53
  • 1
    I know what you mean. I tend to either a) use comments like in my answer, or b) leave the warnings there as they are. You could also do something like `myvalue = False; if myvalue: readline.somemethod()`: the code will never execute but you trick PyCharm into believeving that you've used the module. – jfaccioni Apr 17 '20 at 20:01