|
| 1 | +Title: Disabling Pylint Messages |
| 2 | +Date: 2018-08-12 15:20 |
| 3 | +Modified: 2018-08-12 15:20 |
| 4 | +Category: Posts |
| 5 | +tags: python,pylint,vscode |
| 6 | +cover: static/imgs/default_page_imagev2.jpg |
| 7 | +summary: Showing how to disable specific Pylint warnings. |
| 8 | + |
| 9 | +Small tip of the day since I keep having to look this up. If you use |
| 10 | +[Pylint](https://pylint.org/) for static analysis of your code, you might find |
| 11 | +that you'll want to disable a particular rule for a particular line or file. |
| 12 | +That is, you don't want to permanently disable this rule, but just for this one |
| 13 | +special spot where the rule doesn't make sense. |
| 14 | + |
| 15 | +I find this particularly common in unit test code as my test method names tend |
| 16 | +to be long, violating rule C0103 "Name doesn't conform to naming rules". For |
| 17 | +example, a test name might look like: |
| 18 | + |
| 19 | +```python |
| 20 | +def test_message_builder_generates_correct_bytestring_when_no_argument_supplied(): |
| 21 | +``` |
| 22 | + |
| 23 | +Which is very self-descriptive, and when that test fails, it makes it much |
| 24 | +easier to figure out what might've gone wrong. The problem is that Pylint will |
| 25 | +flag that line since it's greater than 30 characters long, violating the style |
| 26 | +guidelines. We could disable this rule across the entire codebase, but outside |
| 27 | +of tests, the rule makes sense. |
| 28 | + |
| 29 | +This is where local disables come in, which take the form of comments: |
| 30 | + |
| 31 | +```python |
| 32 | +# pylint disable=C0103 |
| 33 | +def test_message_builder_generates_correct_bytestring_when_no_argument_supplied(): |
| 34 | +``` |
| 35 | + |
| 36 | +This will suppress code C0103 for the remainder of the scope (module or block), |
| 37 | +or until it's re-enabled: |
| 38 | + |
| 39 | +```python |
| 40 | +# pylint disable=C0103 |
| 41 | +def test_message_builder_generates_correct_bytestring_when_no_argument_supplied(): |
| 42 | + |
| 43 | + |
| 44 | +# still disabled here... |
| 45 | + |
| 46 | +# pylint enable=C0103 |
| 47 | +def but_not_disabled_here_so_this_name_will_get_flagged_by_pylint(): |
| 48 | +``` |
| 49 | + |
| 50 | +You can also (and it's generally better practice) use the "verbose name" for a |
| 51 | +particular code rather than the shorthand code. For example, this is |
| 52 | +equivalent: |
| 53 | + |
| 54 | +```python |
| 55 | +# pylint disable=invalid-name |
| 56 | +def test_message_builder_generates_correct_bytestring_when_no_argument_supplied(): |
| 57 | +``` |
| 58 | + |
| 59 | +A question then becomes "how do I know what the verbose name for a code is?" |
| 60 | +And the answer is to use the `--list-msgs` argument to Pylint on the command |
| 61 | +line, and it'll spit them all out: |
| 62 | + |
| 63 | +```shell |
| 64 | +$ pylint --list-msgs |
| 65 | +:blacklisted-name (C0102): *Black listed name "%s"* |
| 66 | + Used when the name is listed in the black list (unauthorized names). |
| 67 | +:invalid-name (C0103): *%s name "%s" doesn't conform to %s* |
| 68 | + Used when the name doesn't conform to naming rules associated to its type |
| 69 | + (constant, variable, class...). |
| 70 | +:missing-docstring (C0111): *Missing %s docstring* |
| 71 | + Used when a module, function, class or method has no docstring.Some special |
| 72 | + methods like __init__ doesn't necessary require a docstring. |
| 73 | +:empty-docstring (C0112): *Empty %s docstring* |
| 74 | + Used when a module, function, class or method has an empty docstring (it would |
| 75 | + be too easy ;). |
| 76 | +
|
| 77 | +... more lines ... |
| 78 | +``` |
| 79 | +
|
| 80 | +One extra tip about Pylint: if you use Visual Studio Code you can turn on Pylint as your linter, and |
| 81 | +warnings will get put into the problems view. To turn it on set the following in your VS Code settings |
| 82 | +(assuming you've installed the |
| 83 | +[Python extension from Microsoft](https://marketplace.visualstudio.com/items?itemName=ms-python.python)): |
| 84 | + |
| 85 | +```javscript |
| 86 | +"python.linting.enabled": true, |
| 87 | +"python.linting.pylintEnabled": true, |
| 88 | +``` |
| 89 | + |
| 90 | +Note that these are both on by default (at least in the current version). Once you save |
| 91 | +a Python file, Pylint warnings will show up in the problems view: |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +Note that that screenshot also illustrates how you can filter the problems view down to show just Pylint |
| 96 | +warnings by entering "pylint" into the search box. Also note that clicking any of those will open up |
| 97 | +that file in VS Code and navigate to the line in question. Really handy. |
0 commit comments