|
| 1 | +# Suppressing errors with `knot: ignore` |
| 2 | + |
| 3 | +Type check errors can be suppressed by a `knot: ignore` comment on the same line as the violation. |
| 4 | + |
| 5 | +## Simple `knot: ignore` |
| 6 | + |
| 7 | +```py |
| 8 | +a = 4 + test # knot: ignore |
| 9 | +``` |
| 10 | + |
| 11 | +## Suppressing a specific code |
| 12 | + |
| 13 | +```py |
| 14 | +a = 4 + test # knot: ignore[unresolved-reference] |
| 15 | +``` |
| 16 | + |
| 17 | +## Useless suppression |
| 18 | + |
| 19 | +TODO: Red Knot should emit an `unused-suppression` diagnostic for the |
| 20 | +`possibly-unresolved-reference` suppression. |
| 21 | + |
| 22 | +```py |
| 23 | +test = 10 |
| 24 | +a = test + 3 # knot: ignore[possibly-unresolved-reference] |
| 25 | +``` |
| 26 | + |
| 27 | +## Useless suppression if the error codes don't match |
| 28 | + |
| 29 | +TODO: Red Knot should emit a `unused-suppression` diagnostic for the `possibly-unresolved-reference` |
| 30 | +suppression because it doesn't match the actual `unresolved-reference` diagnostic. |
| 31 | + |
| 32 | +```py |
| 33 | +# error: [unresolved-reference] |
| 34 | +a = test + 3 # knot: ignore[possibly-unresolved-reference] |
| 35 | +``` |
| 36 | + |
| 37 | +## Multiple suppressions |
| 38 | + |
| 39 | +```py |
| 40 | +# fmt: off |
| 41 | +def test(a: f"f-string type annotation", b: b"byte-string-type-annotation"): ... # knot: ignore[fstring-type-annotation, byte-string-type-annotation] |
| 42 | +``` |
| 43 | + |
| 44 | +## Can't suppress syntax errors |
| 45 | + |
| 46 | +<!-- blacken-docs:off --> |
| 47 | + |
| 48 | +```py |
| 49 | +# error: [invalid-syntax] |
| 50 | +def test( # knot: ignore |
| 51 | +``` |
| 52 | + |
| 53 | +<!-- blacken-docs:on --> |
| 54 | + |
| 55 | +## Can't suppress `revealed-type` diagnostics |
| 56 | + |
| 57 | +```py |
| 58 | +a = 10 |
| 59 | +# revealed: Literal[10] |
| 60 | +reveal_type(a) # knot: ignore |
| 61 | +``` |
| 62 | + |
| 63 | +## Extra whitespace in type ignore comments is allowed |
| 64 | + |
| 65 | +```py |
| 66 | +a = 10 / 0 # knot : ignore |
| 67 | +a = 10 / 0 # knot: ignore [ division-by-zero ] |
| 68 | +``` |
| 69 | + |
| 70 | +## Whitespace is optional |
| 71 | + |
| 72 | +```py |
| 73 | +# fmt: off |
| 74 | +a = 10 / 0 #knot:ignore[division-by-zero] |
| 75 | +``` |
| 76 | + |
| 77 | +## Trailing codes comma |
| 78 | + |
| 79 | +Trailing commas in the codes section are allowed: |
| 80 | + |
| 81 | +```py |
| 82 | +a = 10 / 0 # knot: ignore[division-by-zero,] |
| 83 | +``` |
| 84 | + |
| 85 | +## Invalid characters in codes |
| 86 | + |
| 87 | +```py |
| 88 | +# error: [division-by-zero] |
| 89 | +a = 10 / 0 # knot: ignore[*-*] |
| 90 | +``` |
| 91 | + |
| 92 | +## Trailing whitespace |
| 93 | + |
| 94 | +<!-- blacken-docs:off --> |
| 95 | + |
| 96 | +```py |
| 97 | +a = 10 / 0 # knot: ignore[division-by-zero] |
| 98 | + # ^^^^^^ trailing whitespace |
| 99 | +``` |
| 100 | + |
| 101 | +<!-- blacken-docs:on --> |
| 102 | + |
| 103 | +## Missing comma |
| 104 | + |
| 105 | +A missing comma results in an invalid suppression comment. We may want to recover from this in the |
| 106 | +future. |
| 107 | + |
| 108 | +```py |
| 109 | +# error: [unresolved-reference] |
| 110 | +a = x / 0 # knot: ignore[division-by-zero unresolved-reference] |
| 111 | +``` |
| 112 | + |
| 113 | +## Empty codes |
| 114 | + |
| 115 | +An empty codes array suppresses no-diagnostics and is always useless |
| 116 | + |
| 117 | +```py |
| 118 | +# error: [division-by-zero] |
| 119 | +a = 4 / 0 # knot: ignore[] |
| 120 | +``` |
0 commit comments