Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
768 views
in Technique[技术] by (71.8m points)

python - How do I disable pylint unused import error messages in vs code

How do I disable these error messages from popping up in the problems box in vs code

error messages

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As others have said, you can provide a disable argument to disable a specific message. I wanted to elaborate on that.

  1. Here is the syntax for disabling multiple messages and for providing multiple arguments, which was not immediately obvious to me from googling it:

    "python.linting.pylintArgs": [
        "--max-line-length=80",
        "--disable=W0142,W0403,W0613,W0232,R0903,R0913,C0103,R0914,C0304,F0401,W0402,E1101,W0614,C0111,C0301"
    ]
    
  2. You stated that you started seeing way more errors once you disabled that one message. That actually might make sense according to the documentation:

    Python in Visual Studio code is configured by default to use a set of linting rules that are friendly to the largest number of Python developers:

    • Enable all Error (E) and Fatal (F) messages.
    • Disable all Convention (C) and Refactor (R) messages.
    • Disable all Warning (W) messages except the following:
      • unreachable (W0101): Unreachable code
      • duplicate-key (W0109): Duplicate key %r in dictionary
      • unnecessary-semicolon (W0301): Unnecessary semicolon
      • global-variable-not-assigned (W0602): Using global for %r but no assignment is done
      • unused-variable (W0612): Unused variable %r
      • binary-op-exception (W0711): Exception to catch is the result of a binary "%s" operation
      • bad-format-string (W1302): Invalid format string
      • anomalous-backslash-in-string (W1401): Anomalous backslash in string
      • bad-open-mode (W1501): "%s" is not a valid mode for open

    These rules are applied through the following default arguments passed to Pylint:

    --disable=all
    --enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode
    

    These arguments are passed whenever the python.linting.pylintUseMinimalCheckers is set to true (the default). If you specify a value in pylintArgs or use a Pylint configuration file (see the next section), then pylintUseMinimalCheckers is implicitly set to false.

    In other words, PyLint is supposedly pretty lax by default in VS Code, only showing you messages for errors and a few hand-picked warnings. But when you manually set pylintArgs to something, pylintUseMinimalCheckers is ignored, opening the floodgates to all messages. That might be why disabling one message resulted in way more messages being shown. Then again, I'm not sure why you were seeing unused-import messages in the first place since it should have been suppressed by default according to the documentation.

  3. Actually, this currently doesn't work: python.linting.pylintUseMinimalCheckers": true (for me, at this particular moment in time, but hopefully it works fine for you, future reader). To get the same effect, I had to manually set pylintArgs to the value it was supposed to be setting automatically:

    "python.linting.pylintArgs": [
        "--disable=all",
        "--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode"
    ]
    
  4. BONUS: Here's an explanation of the list of disabled messages I use, as shown above in point 1. It's mostly taken from here:

    # Disabled messages
    #    Pointless
    #       W0142 = *args and **kwargs support
    #       W0403 = Relative imports
    #       W0613 = Unused argument
    #       W0232 = Class has no __init__ method
    #       R0903 = Too few public methods
    #       R0913 = Too many arguments
    #       C0103 = Invalid name
    #       R0914 = Too many local variables
    #       C0304 = Final newline missing
    #
    #    PyLint's module importation is unreliable
    #       F0401 = Unable to import module
    #       W0402 = Uses of a deprecated module
    #       E1101 = Module x has no y member
    #
    #    Already an error when wildcard imports are used
    #       W0614 = Unused import from wildcard
    #
    #    Stricter messages that can be disabled until everything else has been fixed
    #       C0111 = Missing docstring
    #       C0301 = Line too long
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...