Skip to content
This repository was archived by the owner on Jan 13, 2026. It is now read-only.

Commit b421019

Browse files
committed
Use ABC rather than Protocol
`Protocol` is meant to be used for structural typing, whereas `ABC` is meant to be used for nominal typing. As our models are based on the Java models, `ABC` thus makes more sense.
1 parent bf22809 commit b421019

File tree

11 files changed

+74
-58
lines changed

11 files changed

+74
-58
lines changed

rewrite/rewrite/execution.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
from __future__ import annotations
22

3-
from abc import ABC
3+
from abc import ABC, abstractmethod
44
from collections.abc import Callable
55
from dataclasses import dataclass
6-
from typing import Protocol, Any, ClassVar, TYPE_CHECKING, List, Optional
6+
from typing import Any, ClassVar, TYPE_CHECKING, List, Optional
77

88
from .tree import SourceFile
99

1010
if TYPE_CHECKING:
1111
from .visitor import TreeVisitor, Cursor
1212

1313

14-
class ExecutionContext(Protocol):
14+
class ExecutionContext(ABC):
1515
REQUIRE_PRINT_EQUALS_INPUT: ClassVar[str] = "org.openrewrite.requirePrintEqualsInput"
1616
CHARSET: ClassVar[str] = "org.openrewrite.parser.charset"
1717

18+
@abstractmethod
1819
def get_message(self, key: str, default_value=None) -> Any:
1920
...
2021

22+
@abstractmethod
2123
def put_message(self, key: str, value: Any):
2224
...
2325

rewrite/rewrite/java/support_types.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
from __future__ import annotations
22

33
import weakref
4+
from abc import abstractmethod, ABC
45
from dataclasses import dataclass, replace
56
from enum import Enum, auto
6-
from functools import cached_property
7-
from typing import List, Optional, Protocol, TypeVar, Generic, ClassVar, Dict, runtime_checkable, Any, cast, \
8-
TYPE_CHECKING, Iterable
7+
from typing import List, Optional, TypeVar, Generic, ClassVar, Dict, Any, TYPE_CHECKING, Iterable
98
from uuid import UUID
109

11-
from rewrite import Tree, SourceFile, TreeVisitor
1210
from rewrite import Markers
11+
from rewrite import Tree, SourceFile, TreeVisitor
1312

1413
if TYPE_CHECKING:
1514
from .visitor import JavaVisitor
1615

1716
P = TypeVar('P')
1817

1918

20-
@runtime_checkable
21-
class J(Tree, Protocol):
19+
class J(Tree):
2220
@property
21+
@abstractmethod
2322
def prefix(self) -> Space:
2423
...
2524

25+
@abstractmethod
2626
def with_prefix(self, prefix: Space) -> 'J':
2727
...
2828

@@ -39,8 +39,9 @@ def accept_java(self, v: 'JavaVisitor[P]', p: P) -> Optional['J']:
3939

4040

4141
@dataclass(frozen=True)
42-
class Comment(Protocol):
42+
class Comment(ABC):
4343
@property
44+
@abstractmethod
4445
def multiline(self) -> bool:
4546
...
4647

@@ -288,47 +289,39 @@ class Location(Enum):
288289
Space.SINGLE_SPACE = Space([], ' ')
289290

290291

291-
@runtime_checkable
292-
class JavaSourceFile(SourceFile, Protocol):
292+
class JavaSourceFile(SourceFile):
293293
pass
294294

295295

296-
@runtime_checkable
297-
class Expression(J, Protocol):
296+
class Expression(J):
298297
pass
299298

300299

301-
@runtime_checkable
302-
class Statement(J, Protocol):
300+
class Statement(J):
303301
pass
304302

305303

306-
@runtime_checkable
307-
class TypedTree(J, Protocol):
304+
class TypedTree(J):
308305
pass
309306

310307

311-
@runtime_checkable
312-
class NameTree(TypedTree, Protocol):
308+
class NameTree(TypedTree):
313309
pass
314310

315311

316-
@runtime_checkable
317-
class TypeTree(NameTree, Protocol):
312+
class TypeTree(NameTree):
318313
pass
319314

320315

321-
@runtime_checkable
322-
class Loop(Statement, Protocol):
316+
class Loop(Statement):
323317
pass
324318

325319

326-
@runtime_checkable
327-
class MethodCall(Expression, Protocol):
320+
class MethodCall(Expression):
328321
pass
329322

330323

331-
class JavaType(Protocol):
324+
class JavaType(ABC):
332325
class FullyQualified:
333326
pass
334327

rewrite/rewrite/java/tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import weakref
44
from dataclasses import dataclass, replace
55
from pathlib import Path
6-
from typing import List, Optional, Protocol, runtime_checkable, TYPE_CHECKING
6+
from typing import List, Optional, TYPE_CHECKING
77
from uuid import UUID
88
from enum import Enum
99

rewrite/rewrite/markers.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
from __future__ import annotations
2-
import traceback
32

3+
import traceback
4+
from abc import ABC, abstractmethod
45
from dataclasses import dataclass, replace
5-
from typing import List, Protocol, ClassVar, cast, runtime_checkable, TYPE_CHECKING
6+
from typing import List, ClassVar, cast, TYPE_CHECKING
67
from uuid import UUID
78

89
if TYPE_CHECKING:
910
from .parser import Parser
1011
from .utils import random_id
1112

1213

13-
@runtime_checkable
14-
class Marker(Protocol):
14+
class Marker(ABC):
1515
@property
16+
@abstractmethod
1617
def id(self) -> UUID:
1718
...
1819

20+
@abstractmethod
1921
def with_id(self, id: UUID) -> Marker:
2022
...
2123

rewrite/rewrite/python/_parser_visitor.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ def visit_withitem(self, node):
505505
else:
506506
var = expr
507507

508-
if not is_of_type(var, TypedTree):
508+
if not isinstance(var, TypedTree):
509509
var = py.ExpressionTypeTree(
510510
random_id(),
511511
Space.EMPTY,
@@ -1860,7 +1860,7 @@ def visit_UnaryOp(self, node):
18601860
def __convert_type(self, node) -> Optional[TypeTree]:
18611861
prefix = self.__whitespace()
18621862
converted_type = self.__convert_internal(node, self.__convert_type, self.__convert_type_mapper)
1863-
if is_of_type(converted_type, TypeTree):
1863+
if isinstance(converted_type, TypeTree):
18641864
return converted_type.with_prefix(prefix)
18651865
else:
18661866
return py.ExpressionTypeTree(
@@ -1940,7 +1940,7 @@ def __convert(self, node) -> Optional[J]:
19401940

19411941
def __convert_statement(self, node) -> Optional[J]:
19421942
converted = self.__convert_internal(node, self.__convert_statement)
1943-
if is_of_type(converted, Statement):
1943+
if isinstance(converted, Statement):
19441944
return converted
19451945
return py.ExpressionStatement(
19461946
random_id(),
@@ -2390,7 +2390,3 @@ def __map_fstring(self, node: ast.JoinedStr, prefix: Space, tok: TokenInfo, toke
23902390
def __cursor_at(self, s: str):
23912391
return self._cursor < len(self._source) and (
23922392
len(s) == 1 and self._source[self._cursor] == s or self._source.startswith(s, self._cursor))
2393-
2394-
2395-
def is_of_type(obj, type):
2396-
return type in obj.__class__.__mro__

rewrite/rewrite/python/style.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from __future__ import annotations
22

3+
from abc import ABC
34
from dataclasses import dataclass, replace
4-
from typing import Protocol
55

66
from ..style import Style, NamedStyles
77

88

9-
class PythonStyle(Style, Protocol):
9+
class PythonStyle(Style):
1010
pass
1111

1212

rewrite/rewrite/python/support_types.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from dataclasses import replace, dataclass
44
from enum import Enum, auto
5-
from typing import Protocol, TypeVar, runtime_checkable, Any, Optional, TYPE_CHECKING
5+
from typing import TypeVar, Any, Optional, TYPE_CHECKING
66

77
from rewrite import TreeVisitor, Markers
88
from rewrite.java.tree import J
@@ -14,8 +14,7 @@
1414
P = TypeVar('P')
1515

1616

17-
@runtime_checkable
18-
class Py(J, Protocol):
17+
class Py(J):
1918
def accept(self, v: TreeVisitor[Any, P], p: P) -> Optional[Any]:
2019
from .visitor import PythonVisitor
2120
return self.accept_python(v.adapt(Py, PythonVisitor), p)

rewrite/rewrite/python/tree.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import weakref
44
from dataclasses import dataclass, replace
55
from pathlib import Path
6-
from typing import List, Optional, Protocol, runtime_checkable, TYPE_CHECKING
6+
from typing import List, Optional, TYPE_CHECKING
77
from uuid import UUID
88
from enum import Enum
99

@@ -726,6 +726,13 @@ def prefix(self) -> Space:
726726
def with_prefix(self, prefix: Space) -> ExpressionStatement:
727727
return self.with_expression(self._expression.with_prefix(prefix))
728728

729+
@property
730+
def markers(self) -> Markers:
731+
return self._expression.markers
732+
733+
def with_markers(self, markers: Markers) -> ExpressionStatement:
734+
return self.with_expression(self._expression.with_markers(markers))
735+
729736
_expression: Expression
730737

731738
@property
@@ -799,6 +806,13 @@ def prefix(self) -> Space:
799806
def with_prefix(self, prefix: Space) -> StatementExpression:
800807
return self.with_statement(self._statement.with_prefix(prefix))
801808

809+
@property
810+
def markers(self) -> Markers:
811+
return self._statement.markers
812+
813+
def with_markers(self, markers: Markers) -> StatementExpression:
814+
return self.with_statement(self._statement.with_markers(markers))
815+
802816
_statement: Statement
803817

804818
@property

rewrite/rewrite/style.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from __future__ import annotations
22

3+
from abc import ABC
34
from dataclasses import dataclass, replace
4-
from typing import Protocol, TypeVar, Type, Iterable, Optional, Set, runtime_checkable
5+
from typing import TypeVar, Type, Iterable, Optional, Set
56
from uuid import UUID
67

78
from .markers import Marker
89
from .utils import random_id
910

1011

11-
@runtime_checkable
12-
class Style(Protocol):
12+
class Style(ABC):
1313
def merge(self, lower_precedence: Style) -> Style:
1414
return self
1515

rewrite/rewrite/tree.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import os
44
import threading
5+
from abc import ABC, abstractmethod
56
from dataclasses import dataclass
67
from datetime import datetime
78
from pathlib import Path
8-
from typing import Protocol, Optional, Any, TypeVar, runtime_checkable, cast, TYPE_CHECKING, Generic, ClassVar, \
9-
Callable, Type
9+
from typing import Optional, Any, TypeVar, cast, TYPE_CHECKING, Generic, ClassVar, Callable, Type
1010
from uuid import UUID
1111

1212
from .markers import Markers
@@ -19,22 +19,26 @@
1919
P = TypeVar('P')
2020

2121

22-
@runtime_checkable
23-
class Tree(Protocol):
22+
class Tree(ABC):
2423
@property
24+
@abstractmethod
2525
def id(self) -> UUID:
2626
...
2727

28+
@abstractmethod
2829
def with_id(self, id: UUID) -> Tree:
2930
...
3031

3132
@property
33+
@abstractmethod
3234
def markers(self) -> Markers:
3335
...
3436

37+
@abstractmethod
3538
def with_markers(self, markers: Markers) -> Tree:
3639
...
3740

41+
@abstractmethod
3842
def is_acceptable(self, v: TreeVisitor[Any, P], p: P) -> bool:
3943
...
4044

@@ -57,7 +61,7 @@ def __hash__(self) -> int:
5761
return hash(self.id)
5862

5963

60-
class PrinterFactory(Protocol):
64+
class PrinterFactory(ABC):
6165
_thread_local = threading.local()
6266

6367
@classmethod
@@ -70,29 +74,34 @@ def current(cls) -> PrinterFactory:
7074
def set_current(self):
7175
PrinterFactory._thread_local.context = self
7276

77+
@abstractmethod
7378
def create_printer(self, cursor: Cursor) -> TreeVisitor[Any, PrintOutputCapture[P]]:
7479
...
7580

7681

7782
S = TypeVar('S', bound=Style)
7883

79-
@runtime_checkable
80-
class SourceFile(Tree, Protocol):
84+
class SourceFile(Tree):
8185
@property
86+
@abstractmethod
8287
def charset_name(self) -> Optional[str]:
8388
...
8489

8590
@property
91+
@abstractmethod
8692
def source_path(self) -> Path:
8793
...
8894

95+
@abstractmethod
8996
def with_source_path(self, source_path: Path) -> SourceFile:
9097
...
9198

9299
@property
100+
@abstractmethod
93101
def file_attributes(self) -> Optional[FileAttributes]:
94102
...
95103

104+
@abstractmethod
96105
def with_file_attributes(self, file_attributes: Optional[FileAttributes]) -> SourceFile:
97106
...
98107

@@ -148,7 +157,7 @@ class Checksum:
148157

149158
class PrintOutputCapture(Generic[P]):
150159
@dataclass
151-
class MarkerPrinter(Protocol):
160+
class MarkerPrinter(ABC):
152161
DEFAULT: ClassVar['PrintOutputCapture.MarkerPrinter'] = None
153162

154163
def before_syntax(self, marker: 'Marker', cursor: 'Cursor', comment_wrapper: Callable[[str], str]) -> str:

0 commit comments

Comments
 (0)