Skip to content

Commit a07ae32

Browse files
committed
use a Protocol for ast_to_offset
1 parent a33aa2d commit a07ae32

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

add_trailing_comma/_ast_helpers.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ast
44
import warnings
5+
from typing import Protocol
56

67
from tokenize_rt import Offset
78

@@ -12,5 +13,12 @@ def ast_parse(contents_text: str) -> ast.Module:
1213
return ast.parse(contents_text.encode())
1314

1415

15-
def ast_to_offset(node: ast.AST) -> Offset:
16+
class _HasOffsetInfo(Protocol):
17+
@property
18+
def lineno(self) -> int: ...
19+
@property
20+
def col_offset(self) -> int: ...
21+
22+
23+
def ast_to_offset(node: _HasOffsetInfo) -> Offset:
1624
return Offset(node.lineno, node.col_offset)

add_trailing_comma/_plugins/calls.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def visit_Call(
3434
state: State,
3535
node: ast.Call,
3636
) -> Iterable[tuple[Offset, TokenFunc]]:
37-
argnodes = [*node.args, *node.keywords]
37+
argnodes: list[ast.expr | ast.keyword] = [*node.args, *node.keywords]
3838
arg_offsets = set()
3939
for argnode in argnodes:
4040
offset = ast_to_offset(argnode)

add_trailing_comma/_plugins/classes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def visit_ClassDef(
3737
# starargs are allowed in py3 class definitions, py35+ allows trailing
3838
# commas. py34 does not, but adding an option for this very obscure
3939
# case seems not worth it.
40-
args = [*node.bases, *node.keywords]
40+
args: list[ast.expr | ast.keyword] = [*node.bases, *node.keywords]
4141
arg_offsets = {ast_to_offset(arg) for arg in args}
4242

4343
if arg_offsets:

0 commit comments

Comments
 (0)