Python Glossary Русский

Python 3.8+astterm

ast.type_ignore

root and base nodes

The abstract base class of '# type: ignore' markers; its only subclass is TypeIgnore. Found in the Module.type_ignores list. Added in 3.8.

Syntax

ast.parse(src, type_comments=True).type_ignores

The type_ignores list is filled only when parse is called with type_comments=True; otherwise it is always empty even if the markers are present. Other comments never reach the tree. The tag after ignore (such as '[attr]') is kept in the tag field.

Examples

import ast
src = 'x = 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, isinstance(ti, ast.type_ignore))
# → TypeIgnore 1 [attr] True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy