| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from mypy.nodes import Expression, Node |
| 4 |
from mypy.traverser import ExtendedTraverserVisitor |
| 5 |
from mypy.types import AnyType, Type, TypeOfAny |
| 6 |
|
| 7 |
|
| 8 |
class MissingTypesVisitor(ExtendedTraverserVisitor): |
| 9 |
"""AST visitor that can be used to add any missing types as a generic AnyType.""" |
| 10 |
|
| 11 |
def __init__(self, types: dict[Expression, Type]) -> None: |
| 12 |
super().__init__() |
| 13 |
self.types: dict[Expression, Type] = types |
| 14 |
|
| 15 |
def visit(self, o: Node) -> bool: |
| 16 |
if isinstance(o, Expression) and o not in self.types: |
| 17 |
self.types[o] = AnyType(TypeOfAny.special_form) |
| 18 |
|
| 19 |
# If returns True, will continue to nested nodes. |
| 20 |
return True |