Skip to content

Commit effd970

Browse files
authored
Adds ability to have subclasses for NodeVisitor and TraverserVisitor (#10125)
Currently a very important tool - which `NodeVisitor` definitely is - is not available to be used in a 3rd party code. Because currently inheriting from `NodeVisitor` raises an exception: `TypeError: interpreted classes cannot inherit from compiled traits` A developer has a single choice to replicate the same visitor pattern by themselves, which is not really convenient. So, I propose to make this consistent with `TypeVisitor`, which already allows having interpreted subclasses. Refs a9fa9ab Refs #9001 Refs #9602
1 parent 15bd486 commit effd970

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

mypy/traverser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Generic node traverser visitor"""
22

33
from typing import List
4+
from mypy_extensions import mypyc_attr
45

56
from mypy.visitor import NodeVisitor
67
from mypy.nodes import (
@@ -16,6 +17,7 @@
1617
)
1718

1819

20+
@mypyc_attr(allow_interpreted_subclasses=True)
1921
class TraverserVisitor(NodeVisitor[None]):
2022
"""A parse tree visitor that traverses the parse tree during visiting.
2123

mypy/visitor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from abc import abstractmethod
44
from typing import TypeVar, Generic
55
from typing_extensions import TYPE_CHECKING
6-
from mypy_extensions import trait
6+
from mypy_extensions import trait, mypyc_attr
77

88
if TYPE_CHECKING:
99
# break import cycle only needed for mypy
@@ -14,6 +14,7 @@
1414

1515

1616
@trait
17+
@mypyc_attr(allow_interpreted_subclasses=True)
1718
class ExpressionVisitor(Generic[T]):
1819
@abstractmethod
1920
def visit_int_expr(self, o: 'mypy.nodes.IntExpr') -> T:
@@ -193,6 +194,7 @@ def visit_temp_node(self, o: 'mypy.nodes.TempNode') -> T:
193194

194195

195196
@trait
197+
@mypyc_attr(allow_interpreted_subclasses=True)
196198
class StatementVisitor(Generic[T]):
197199
# Definitions
198200

@@ -310,6 +312,7 @@ def visit_exec_stmt(self, o: 'mypy.nodes.ExecStmt') -> T:
310312

311313

312314
@trait
315+
@mypyc_attr(allow_interpreted_subclasses=True)
313316
class NodeVisitor(Generic[T], ExpressionVisitor[T], StatementVisitor[T]):
314317
"""Empty base class for parse tree node visitors.
315318

0 commit comments

Comments
 (0)