Skip to content

Commit 5d4cbb9

Browse files
davidhaltercarljm
andauthored
Allow type checkers to ignore specific error codes (#2153)
* Allow type: ignore error codes filtering * Try to improve the wording for the spec * Rephrase a bit of the spec * Avoid specifying how whitespace after # type: ignore works * Update conformance/tests/directives_type_ignore.py Co-authored-by: Carl Meyer <carl@oddbird.net> * Update docs/spec/directives.rst Co-authored-by: Carl Meyer <carl@oddbird.net> * Rerun the tests * Change the error code to a random error code and allow the error to happen * Update conformance/tests/directives_type_ignore.py Co-authored-by: Carl Meyer <carl@oddbird.net> --------- Co-authored-by: Carl Meyer <carl@oddbird.net>
1 parent cf84b52 commit 5d4cbb9

6 files changed

Lines changed: 22 additions & 22 deletions

File tree

conformance/results/mypy/directives_type_ignore.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ Does not honor "# type: ignore" comment if comment includes additional text.
55
output = """
66
directives_type_ignore.py:11: error: Invalid "type: ignore" comment [syntax]
77
directives_type_ignore.py:11: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
8-
directives_type_ignore.py:14: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
9-
directives_type_ignore.py:14: note: Error code "assignment" not covered by "type: ignore" comment
8+
directives_type_ignore.py:16: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
9+
directives_type_ignore.py:16: note: Error code "assignment" not covered by "type: ignore" comment
1010
"""
1111
conformance_automated = "Fail"
1212
errors_diff = """
1313
Line 11: Unexpected errors ['directives_type_ignore.py:11: error: Invalid "type: ignore" comment [syntax]', 'directives_type_ignore.py:11: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]']
14-
Line 14: Unexpected errors ['directives_type_ignore.py:14: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]']
1514
"""

conformance/results/results.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,9 +1198,9 @@ <h3>Python Type System Conformance Test Results</h3>
11981198
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;directives_type_ignore</th>
11991199
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not honor "# type: ignore" comment if comment includes additional text.</p></span></div></th>
12001200
<th class="column col2 conformant">Pass</th>
1201-
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not honor "# type: ignore" comment if comment includes additional text.</p></span></div></th>
12021201
<th class="column col2 conformant">Pass</th>
1203-
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Treats `# type: ignore[error-code]` as only ignoring errors that match the error code `error-code`.</p></span></div></th>
1202+
<th class="column col2 conformant">Pass</th>
1203+
<th class="column col2 conformant">Pass</th>
12041204
</tr>
12051205
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;directives_type_ignore_file1</th>
12061206
<th class="column col2 conformant">Pass</th>
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
conformance_automated = "Fail"
2-
conformant = "Partial"
3-
notes = """
4-
Treats `# type: ignore[error-code]` as only ignoring errors that match the error code `error-code`.
5-
"""
1+
conformance_automated = "Pass"
62
errors_diff = """
7-
Line 14: Unexpected errors ['directives_type_ignore.py:14:10: error[invalid-assignment] Object of type `Literal[""]` is not assignable to `int`']
83
"""
94
output = """
10-
directives_type_ignore.py:14:10: error[invalid-assignment] Object of type `Literal[""]` is not assignable to `int`
5+
directives_type_ignore.py:16:10: error[invalid-assignment] Object of type `Literal[""]` is not assignable to `int`
116
"""
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
conformant = "Partial"
2-
notes = """
3-
Does not honor "# type: ignore" comment if comment includes additional text.
4-
"""
5-
conformance_automated = "Fail"
1+
conformance_automated = "Pass"
62
errors_diff = """
7-
Line 14: Unexpected errors ['directives_type_ignore.py:14: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]']
83
"""
94
output = """
10-
directives_type_ignore.py:14: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
11-
directives_type_ignore.py:14: note: Error code "assignment" not covered by "type: ignore" comment
5+
directives_type_ignore.py:16: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
6+
directives_type_ignore.py:16: note: Error code "assignment" not covered by "type: ignore" comment
127
"""

conformance/tests/directives_type_ignore.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
# The following type violation should be suppressed.
1111
y: int = "" # type: ignore - additional stuff
1212

13-
# The following type violation should be suppressed.
14-
z: int = "" # type: ignore[additional_stuff]
13+
# The following type violation may be suppressed, if the type checker
14+
# actually uses the code `an-empty-str-is-not-an-int` (unlikely!), or if
15+
# it treats unknown codes as blanket ignores.
16+
z: int = "" # type: ignore[an-empty-str-is-not-an-int] # E?
1517

1618
# > In some cases, linting tools or other comments may be needed on the same
1719
# > line as a type comment. In these cases, the type comment should be before

docs/spec/directives.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ other comments and linting markers:
7373

7474
# type: ignore # <comment or other marker>
7575

76+
The form ``# type: ignore[...]`` may be used to suppress only type errors with a
77+
given error code, though support for this is optional and may vary by type checker:
78+
79+
- Given ``# type: ignore[my_code1]``, a type checker may ignore the error code
80+
``my_code1`` and choose to treat this form as equivalent to ``# type: ignore``.
81+
- Alternatively, given ``# type: ignore[my_code1]`` a type checker may suppress the
82+
error only if the error cause matches the error code ``my_code1``.
83+
- A bare ``# type: ignore`` must always suppress all type errors.
84+
7685
.. _`cast`:
7786

7887
``cast()``

0 commit comments

Comments
 (0)