Skip to content

Commit 531417f

Browse files
auscompgeekcursorless-bot
authored andcommitted
Tighten Talon marks type hints (cursorless-dev#1848)
This removes a bunch of `Any`s from the Talon code related to marks. Ref: cursorless-dev#725 ## Checklist - [ ] ~I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/)~ - [ ] ~I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet)~ - [x] I have not broken the cheatsheet
1 parent b2ca68b commit 531417f

File tree

6 files changed

+58
-24
lines changed

6 files changed

+58
-24
lines changed

src/marks/decorated_mark.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from talon import Module, actions, cron, fs
55

66
from ..csv_overrides import init_csv_and_watch_changes
7+
from .mark_types import DecoratedSymbol
78

89
mod = Module()
910

@@ -28,9 +29,9 @@ def cursorless_grapheme(m) -> str:
2829
@mod.capture(
2930
rule="[{user.cursorless_hat_color}] [{user.cursorless_hat_shape}] <user.cursorless_grapheme>"
3031
)
31-
def cursorless_decorated_symbol(m) -> dict[str, Any]:
32+
def cursorless_decorated_symbol(m) -> DecoratedSymbol:
3233
"""A decorated symbol"""
33-
hat_color = getattr(m, "cursorless_hat_color", "default")
34+
hat_color: str = getattr(m, "cursorless_hat_color", "default")
3435
try:
3536
hat_style_name = f"{hat_color}-{m.cursorless_hat_shape}"
3637
except AttributeError:
@@ -82,10 +83,10 @@ def cursorless_decorated_symbol(m) -> dict[str, Any]:
8283
}
8384
FALLBACK_COLOR_ENABLEMENT = DEFAULT_COLOR_ENABLEMENT
8485

85-
unsubscribe_hat_styles = None
86+
unsubscribe_hat_styles: Any = None
8687

8788

88-
def setup_hat_styles_csv(hat_colors: dict, hat_shapes: dict):
89+
def setup_hat_styles_csv(hat_colors: dict[str, str], hat_shapes: dict[str, str]):
8990
global unsubscribe_hat_styles
9091

9192
(
@@ -149,7 +150,7 @@ def setup_hat_styles_csv(hat_colors: dict, hat_shapes: dict):
149150
slow_reload_job = None
150151

151152

152-
def init_hats(hat_colors: dict, hat_shapes: dict):
153+
def init_hats(hat_colors: dict[str, str], hat_shapes: dict[str, str]):
153154
setup_hat_styles_csv(hat_colors, hat_shapes)
154155

155156
vscode_settings_path: Path = actions.user.vscode_settings_path().resolve()

src/marks/lines_number.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from collections.abc import Callable
22
from dataclasses import dataclass
3-
from typing import Any
43

54
from talon import Module
65

76
from ..targets.range_target import RangeConnective
7+
from .mark_types import LineNumber, LineNumberMark, LineNumberType
88

99
mod = Module()
1010

@@ -14,8 +14,8 @@
1414
@dataclass
1515
class CustomizableTerm:
1616
cursorlessIdentifier: str
17-
type: str
18-
formatter: Callable
17+
type: LineNumberType
18+
formatter: Callable[[int], int]
1919

2020

2121
# NOTE: Please do not change these dicts. Use the CSVs for customization.
@@ -35,15 +35,13 @@ class CustomizableTerm:
3535
"[<user.cursorless_range_connective> <user.private_cursorless_number_small>]"
3636
)
3737
)
38-
def cursorless_line_number(m) -> dict[str, Any]:
38+
def cursorless_line_number(m) -> LineNumber:
3939
direction = directions_map[m.cursorless_line_direction]
40-
anchor = create_line_number_mark(
41-
direction.type, direction.formatter(m.private_cursorless_number_small_list[0])
42-
)
43-
if len(m.private_cursorless_number_small_list) > 1:
40+
numbers: list[int] = m.private_cursorless_number_small_list
41+
anchor = create_line_number_mark(direction.type, direction.formatter(numbers[0]))
42+
if len(numbers) > 1:
4443
active = create_line_number_mark(
45-
direction.type,
46-
direction.formatter(m.private_cursorless_number_small_list[1]),
44+
direction.type, direction.formatter(numbers[1])
4745
)
4846
range_connective: RangeConnective = m.cursorless_range_connective
4947
return {
@@ -56,9 +54,9 @@ def cursorless_line_number(m) -> dict[str, Any]:
5654
return anchor
5755

5856

59-
def create_line_number_mark(line_number_type: str, line_number: int) -> dict[str, Any]:
57+
def create_line_number_mark(type: LineNumberType, line_number: int) -> LineNumberMark:
6058
return {
6159
"type": "lineNumber",
62-
"lineNumberType": line_number_type,
60+
"lineNumberType": type,
6361
"lineNumber": line_number,
6462
}

src/marks/mark.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from typing import Any
2-
31
from talon import Module
42

3+
from .mark_types import Mark
4+
55
mod = Module()
66

77

@@ -12,5 +12,5 @@
1212
"<user.cursorless_line_number>" # row (ie absolute mod 100), up, down
1313
)
1414
)
15-
def cursorless_mark(m) -> dict[str, Any]:
15+
def cursorless_mark(m) -> Mark:
1616
return m[0]

src/marks/mark_types.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import Literal, TypedDict, Union
2+
3+
4+
class DecoratedSymbol(TypedDict):
5+
type: Literal["decoratedSymbol"]
6+
symbolColor: str
7+
character: str
8+
9+
10+
SimpleMark = dict[Literal["type"], str]
11+
12+
LineNumberType = Literal["modulo100", "relative"]
13+
14+
15+
class LineNumberMark(TypedDict):
16+
type: Literal["lineNumber"]
17+
lineNumberType: LineNumberType
18+
lineNumber: int
19+
20+
21+
class LineNumberRange(TypedDict):
22+
type: Literal["range"]
23+
anchor: LineNumberMark
24+
active: LineNumberMark
25+
excludeAnchor: bool
26+
excludeActive: bool
27+
28+
29+
LineNumber = Union[LineNumberMark, LineNumberRange]
30+
31+
Mark = Union[DecoratedSymbol, SimpleMark, LineNumber]

src/marks/simple_mark.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from talon import Module
22

3+
from .mark_types import SimpleMark
4+
35
mod = Module()
46

57
mod.list("cursorless_simple_mark", desc="Cursorless simple marks")
@@ -15,7 +17,7 @@
1517

1618

1719
@mod.capture(rule="{user.cursorless_simple_mark}")
18-
def cursorless_simple_mark(m) -> dict[str, str]:
20+
def cursorless_simple_mark(m) -> SimpleMark:
1921
return {
2022
"type": simple_marks[m.cursorless_simple_mark],
2123
}

src/targets/target_types.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
from dataclasses import dataclass
2-
from typing import Literal, Optional, Union
2+
from typing import Any, Literal, Optional, Union
3+
4+
from ..marks.mark_types import Mark
35

46
RangeTargetType = Literal["vertical"]
57

68

79
@dataclass
810
class PrimitiveTarget:
911
type = "primitive"
10-
mark: Optional[dict]
11-
modifiers: Optional[list[dict]]
12+
mark: Optional[Mark]
13+
modifiers: Optional[list[dict[str, Any]]]
1214

1315

1416
@dataclass

0 commit comments

Comments
 (0)