From 17100dadafc5d6ad7255e13d469412b2071bda05 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Sun, 21 Feb 2021 13:33:53 +0300 Subject: [PATCH 1/2] Adds ability to have subclasses for NodeVisitor 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 patter by theyself, which is not really convenient. So, I propose to make this consistent with `TypeVisitor`, which already allows having interpreted subclasses. Refs https://github.com/python/mypy/commit/a9fa9ab43a2655cf81c66c065d51db4618839d05 Refs https://github.com/python/mypy/issues/9001 Refs https://github.com/python/mypy/pull/9602 --- mypy/visitor.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mypy/visitor.py b/mypy/visitor.py index 09a6cea9106a..b98ec773bbe3 100644 --- a/mypy/visitor.py +++ b/mypy/visitor.py @@ -3,7 +3,7 @@ from abc import abstractmethod from typing import TypeVar, Generic from typing_extensions import TYPE_CHECKING -from mypy_extensions import trait +from mypy_extensions import trait, mypyc_attr if TYPE_CHECKING: # break import cycle only needed for mypy @@ -14,6 +14,7 @@ @trait +@mypyc_attr(allow_interpreted_subclasses=True) class ExpressionVisitor(Generic[T]): @abstractmethod def visit_int_expr(self, o: 'mypy.nodes.IntExpr') -> T: @@ -193,6 +194,7 @@ def visit_temp_node(self, o: 'mypy.nodes.TempNode') -> T: @trait +@mypyc_attr(allow_interpreted_subclasses=True) class StatementVisitor(Generic[T]): # Definitions @@ -310,6 +312,7 @@ def visit_exec_stmt(self, o: 'mypy.nodes.ExecStmt') -> T: @trait +@mypyc_attr(allow_interpreted_subclasses=True) class NodeVisitor(Generic[T], ExpressionVisitor[T], StatementVisitor[T]): """Empty base class for parse tree node visitors. From 1dcfde8e09df3a60426fb0bb802da0c3283233f1 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Sun, 21 Feb 2021 19:50:29 +0300 Subject: [PATCH 2/2] TraverserVisitor is also a highly related tool to inherit from --- mypy/traverser.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mypy/traverser.py b/mypy/traverser.py index 4ce8332fed86..c4834c9acb6b 100644 --- a/mypy/traverser.py +++ b/mypy/traverser.py @@ -1,6 +1,7 @@ """Generic node traverser visitor""" from typing import List +from mypy_extensions import mypyc_attr from mypy.visitor import NodeVisitor from mypy.nodes import ( @@ -16,6 +17,7 @@ ) +@mypyc_attr(allow_interpreted_subclasses=True) class TraverserVisitor(NodeVisitor[None]): """A parse tree visitor that traverses the parse tree during visiting.