Python 3.8+astterm
ast.TypeIgnore
root and base nodes
Node for a # type: ignore comment: lineno is the line, tag is the text after ignore. It sits in the type_ignores list of the Module root.
Syntax
ast.TypeIgnore(lineno, tag)
ast drops comments, and # type: ignore is an exception only with ast.parse(…, type_comments=True); without the flag type_ignores is always empty. The node is not attached to the statement it covers; the only link is the line number. The tag keeps its brackets: for # type: ignore[attr] tag is '[attr]'.
Examples
import ast
src = 'x = 1\ny = f() # type: ignore[attr]'
print(ast.parse(src).type_ignores) # → []
ti = ast.parse(src, type_comments=True).type_ignores[0]
print(type(ti).__name__, ti.lineno, ti.tag) # → TypeIgnore 2 [attr]
See also
Open in the full glossarydocs.python.orgReport an inaccuracy