Skip to content

Commit 6b0b7e1

Browse files
committed
Stricter interface for Lint._parse
1 parent 68b8df5 commit 6b0b7e1

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

src/ethereum_spec_tools/lint/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from abc import ABCMeta, abstractmethod
1313
from dataclasses import dataclass
1414
from itertools import zip_longest
15-
from typing import Generator, List, Optional, Sequence, Tuple
15+
from pathlib import Path
16+
from typing import Generator, List, Optional, Sequence, Tuple, TypeVar
1617

1718
from ..forks import Hardfork
1819

@@ -78,6 +79,9 @@ class Diagnostic:
7879
message: str
7980

8081

82+
V = TypeVar("V", bound=ast.NodeVisitor)
83+
84+
8185
class Lint(metaclass=ABCMeta):
8286
"""
8387
A single check which may be performed against the specifications.
@@ -98,15 +102,13 @@ def lint(
98102
The particular hardfork to lint.
99103
"""
100104

101-
def _parse(
102-
self, source: str, visitor: ast.NodeVisitor, attr: str
103-
) -> Sequence[str]:
105+
def _parse(self, source: str, visitor: V) -> V:
104106
"""
105-
Walks the source string and extracts a sequence of identifiers.
107+
Walks the source string.
106108
"""
107109
parsed = ast.parse(source)
108110
visitor.visit(parsed)
109-
return getattr(visitor, attr)
111+
return visitor
110112

111113

112114
class Linter:

src/ethereum_spec_tools/lint/lints/glacier_forks_hygiene.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,11 @@ def lint(
9595
)
9696
continue
9797

98-
current_node = self._parse(all_current[file], _Visitor(), "items")
99-
previous_node = self._parse(
100-
all_previous[file], _Visitor(), "items"
101-
)
98+
current_node = self._parse(all_current[file], _Visitor()).items
99+
previous_node = self._parse(all_previous[file], _Visitor()).items
102100

103101
diagnostics += self.compare(
104-
fork_name, file, current_node, previous_node # type: ignore
102+
fork_name, file, current_node, previous_node
105103
)
106104

107105
return diagnostics

src/ethereum_spec_tools/lint/lints/import_hygiene.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def check_import(
5555
else tuple()
5656
)
5757

58-
current_imports = self._parse(source, _Visitor(), "item_imports")
58+
current_imports = self._parse(source, _Visitor()).item_imports
5959

6060
for item in current_imports:
6161
if item is None:

src/ethereum_spec_tools/lint/lints/patch_hygiene.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ def compare(
5050
# Entire file is new, so nothing to compare!
5151
return []
5252

53-
current_nodes = self._parse(current_source, _Visitor(), "items")
53+
current_nodes = self._parse(current_source, _Visitor()).items
5454
previous_nodes = {
5555
item: idx
5656
for (idx, item) in enumerate(
57-
self._parse(previous_source, _Visitor(), "items")
57+
self._parse(previous_source, _Visitor()).items
5858
)
5959
}
6060

0 commit comments

Comments
 (0)