@@ -216,3 +216,66 @@ to be annotated with a starred type:
216
216
p, q, * rs = 1 , 2 # type: int , int , List [ int ]
217
217
218
218
Here, the type of ``rs `` is set to ``List[int] ``.
219
+
220
+ Silencing type errors
221
+ *********************
222
+
223
+ You might want to disable type checking on specific lines, or within specific
224
+ files in your codebase. To do that, you can use a ``# type: ignore `` comment.
225
+
226
+ For example, say that the web framework that you use now takes an integer
227
+ argument to ``run() ``, which starts it on localhost on that port. Liks so:
228
+
229
+ .. code-block :: python
230
+
231
+ # Starting app on http://localhost:8000
232
+ app.run(8000 )
233
+
234
+ However, the type stubs that the package uses is not up-to-date, and it still
235
+ expects only `str ` types for `run() `. This would give you the following error:
236
+
237
+ .. code-block :: text
238
+
239
+ error: Argument 1 to "run" of "A" has incompatible type "int"; expected "str"
240
+
241
+ If you cannot directly fix the type stubs yourself, you can temporarily
242
+ disable type checking on that line, by adding a ``# type: ignore ``:
243
+
244
+ .. code-block :: python
245
+
246
+ # Starting app on http://localhost:8000
247
+ app.run(8000 ) # type: ignore
248
+
249
+ This will suppress any mypy errors that would have raised on that specific line.
250
+
251
+ You should probably add some more information on the ``# type: ignore `` comment,
252
+ to explain why the ignore was added in the first place. This could be a link to
253
+ an issue on the repository responsible for the type stubs, or it could be a
254
+ short explanation of the bug. To do that, use this format:
255
+
256
+ .. code-block :: python
257
+
258
+ # Starting app on http://localhost:8000
259
+ app.run(8000 ) # type: ignore # `run()` now accepts an `int`, as a port
260
+
261
+
262
+ If your error displays an error code, like so:
263
+
264
+ .. code-block :: text
265
+
266
+ error: "str" has no attribute "trim" [attr-defined]
267
+
268
+
269
+ It is possible to add a specific error-code in your message, like
270
+ ``# type: ignore[attr-defined] ``, to clarify what's being silenced. You can
271
+ find more information about error codes here: :ref: `silence-error-codes `.
272
+
273
+ Similarly, you can also ignore all mypy checks in a file, by adding a
274
+ ``# type: ignore `` on the top of the file:
275
+
276
+ .. code-block :: python
277
+
278
+ # type: ignore
279
+ # This is a test file, skipping type checking in it.
280
+ import unittest
281
+ ...
0 commit comments