Skip to content

Commit e5e9b29

Browse files
committed
improve type annotations in 'docutils.statemachine'
1 parent 1e7e174 commit e5e9b29

File tree

2 files changed

+201
-2
lines changed

2 files changed

+201
-2
lines changed

stubs/docutils/@tests/stubtest_allowlist.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ docutils.languages.LanguageImporter.__getattr__
99
docutils.nodes.Element.__getattr__
1010
docutils.nodes.GenericNodeVisitor.__getattr__
1111
docutils.nodes.Element.__iter__ # doesn't exist at runtime, but the class is iterable due to __getitem__
12+
docutils.statemachine.ViewList.__iter__ # doesn't exist at runtime, but the class is iterable due to __getitem__
1213
docutils.parsers.rst.Directive.__getattr__
1314
docutils.transforms.Transform.__getattr__
1415
docutils.transforms.Transformer.__getattr__
+200-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,201 @@
1-
from _typeshed import Incomplete
1+
from collections.abc import Callable, Generator, Iterator, Sequence
2+
from re import Match, Pattern
3+
from typing import Any, Generic, TypeAlias, TypeVar, overload
4+
from typing_extensions import Self
25

3-
def __getattr__(name: str) -> Incomplete: ...
6+
__docformat__: str
7+
8+
_Context = TypeVar("_Context")
9+
_TransitionResult: TypeAlias = tuple[_Context, str | None, list[str]]
10+
_TransitionMethod: TypeAlias = Callable[[Match[str], _Context, str], _TransitionResult[_Context]]
11+
12+
class StateMachine(Generic[_Context]):
13+
input_lines: StringList | None
14+
input_offset: int
15+
line: str | None
16+
line_offset: int
17+
debug: bool
18+
initial_state: str
19+
current_state: list[State[_Context]]
20+
states: dict[str, State[_Context]]
21+
observers: list[Callable[[Self], None]]
22+
def __init__(self, state_classes: Sequence[type[State[_Context]]], initial_state: str, debug: bool = False) -> None: ...
23+
def unlink(self) -> None: ...
24+
def run(
25+
self,
26+
input_lines: list[str] | StringList,
27+
input_offset: int = 0,
28+
context: Any | None = None,
29+
input_source: str | None = None,
30+
initial_state: str | None = None,
31+
) -> list[Any]: ...
32+
def get_state(self, next_state: str | None = None) -> State[_Context]: ...
33+
def next_line(self, n: int = 1) -> str: ...
34+
def is_next_line_blank(self) -> bool: ...
35+
def at_eof(self) -> bool: ...
36+
def at_bof(self) -> bool: ...
37+
def previous_line(self, n: int = 1) -> str: ...
38+
def goto_line(self, line_offset: int) -> None: ...
39+
def get_source(self, line_offset: int) -> str: ...
40+
def abs_line_offset(self) -> int: ...
41+
def abs_line_number(self) -> int: ...
42+
def get_source_and_line(self, lineno: int | None = None) -> tuple[str, int]: ...
43+
def insert_input(self, input_lines: list[str] | StringList, source: str) -> None: ...
44+
def get_text_block(self, flush_left: bool = False) -> StringList: ...
45+
def check_line(
46+
self, context: _Context, state: State[_Context], transitions: list[str] | None = ...
47+
) -> _TransitionResult[_Context]: ...
48+
def add_state(self, state_class: type[State[_Context]]) -> None: ...
49+
def add_states(self, state_classes: Sequence[type[State[_Context]]]) -> None: ...
50+
def runtime_init(self) -> None: ...
51+
def error(self) -> None: ...
52+
def attach_observer(self, observer) -> None: ...
53+
def detach_observer(self, observer) -> None: ...
54+
def notify_observers(self) -> None: ...
55+
56+
class State(Generic[_Context]):
57+
patterns: dict[str, str | Pattern[str]] | None
58+
initial_transitions: Sequence[str] | Sequence[tuple[str, str]] | None
59+
nested_sm: type[StateMachine[_Context]] | None
60+
nested_sm_kwargs: dict[str, Any] | None
61+
transition_order: list[str]
62+
transitions: dict[str, tuple[Pattern[str], Callable[[], None], str]]
63+
state_machine: StateMachine[_Context]
64+
debug: bool
65+
def __init__(self, state_machine: StateMachine[_Context], debug: bool = False) -> None: ...
66+
def runtime_init(self) -> None: ...
67+
def unlink(self) -> None: ...
68+
def add_initial_transitions(self) -> None: ...
69+
def add_transitions(self, names: list[str], transitions) -> None: ...
70+
def add_transition(self, name: str, transition: tuple[Pattern[str], str, str]) -> None: ...
71+
def remove_transition(self, name: str) -> None: ...
72+
def make_transition(
73+
self, name: str, next_state: str | None = None
74+
) -> tuple[Pattern[str], _TransitionMethod[_Context], str]: ...
75+
def make_transitions(
76+
self, name_list: list[str | tuple[str] | tuple[str, str]]
77+
) -> tuple[list[str], dict[str, tuple[Pattern[str], _TransitionMethod[_Context], str]]]: ...
78+
def no_match(
79+
self, context: Any, transitions: tuple[list[str], dict[str, tuple[Pattern[str], _TransitionMethod[_Context], str]]]
80+
) -> _TransitionResult[_Context]: ...
81+
def bof(self, context: _Context) -> tuple[_Context, list[str]]: ...
82+
def eof(self, context: _Context) -> list[str]: ...
83+
def nop(self, match: Match[str], context: _Context, next_state: str) -> _TransitionResult[_Context]: ...
84+
85+
class StateMachineWS(StateMachine[_Context]):
86+
def get_indented(self, until_blank: bool = False, strip_indent: bool = True) -> tuple[StringList, int, int, bool]: ...
87+
def get_known_indented(
88+
self, indent: int, until_blank: bool = False, strip_indent: bool = True
89+
) -> tuple[list[str], int, bool]: ...
90+
def get_first_known_indented(
91+
self, indent: int, until_blank: bool = False, strip_indent: bool = True, strip_top: bool = True
92+
) -> tuple[list[str], int, int, bool]: ...
93+
94+
class StateWS(State[_Context]):
95+
indent_sm: type[StateMachine[_Context]] | None
96+
indent_sm_kwargs: dict[str, Any] | None
97+
known_indent_sm: type[StateMachine[_Context]] | None
98+
known_indent_sm_kwargs: dict[str, Any] | None
99+
ws_patterns: dict[str, str]
100+
ws_initial_transitions: Sequence[str]
101+
def __init__(self, state_machine: StateMachine[_Context], debug: bool = False) -> None: ...
102+
def add_initial_transitions(self) -> None: ...
103+
def blank(self, match: Match[str], context: _Context, next_state: str) -> _TransitionResult[_Context]: ...
104+
def indent(self, match: Match[str], context: _Context, next_state: str) -> _TransitionResult[_Context]: ...
105+
def known_indent(self, match: Match[str], context: _Context, next_state: str) -> _TransitionResult[_Context]: ...
106+
def first_known_indent(self, match: Match[str], context: _Context, next_state: str) -> _TransitionResult[_Context]: ...
107+
108+
class _SearchOverride:
109+
def match(self, pattern: Pattern[str]) -> Match[str]: ...
110+
111+
class SearchStateMachine(_SearchOverride, StateMachine[_Context]): ...
112+
class SearchStateMachineWS(_SearchOverride, StateMachineWS[_Context]): ...
113+
114+
_Data = TypeVar("_Data")
115+
116+
class ViewList(Generic[_Data]):
117+
data: list[_Data]
118+
items: list[tuple[str, int]]
119+
parent: ViewList[_Data]
120+
parent_offset: int
121+
def __init__(
122+
self,
123+
initlist: ViewList[_Data] | list[_Data] | None = None,
124+
source: str | None = None,
125+
items: list[tuple[str, int]] | None = None,
126+
parent: ViewList[_Data] | None = None,
127+
parent_offset: int | None = None,
128+
) -> None: ...
129+
def __lt__(self, other: Any) -> bool: ...
130+
def __le__(self, other: Any) -> bool: ...
131+
def __eq__(self, other: object) -> bool: ...
132+
def __ne__(self, other: object) -> bool: ...
133+
def __gt__(self, other: Any) -> bool: ...
134+
def __ge__(self, other: Any) -> bool: ...
135+
def __contains__(self, item: ViewList[_Data]) -> bool: ...
136+
def __len__(self) -> int: ...
137+
@overload
138+
def __getitem__(self, i: slice) -> ViewList[_Data]: ...
139+
@overload
140+
def __getitem__(self, i: int) -> _Data: ...
141+
@overload
142+
def __setitem__(self, i: slice, item: ViewList[_Data]) -> None: ...
143+
@overload
144+
def __setitem__(self, i: int, item: _Data) -> None: ...
145+
def __delitem__(self, i: int) -> None: ...
146+
def __add__(self, other: ViewList[_Data]) -> None: ...
147+
def __radd__(self, other: ViewList[_Data]) -> None: ...
148+
def __iadd__(self, other: Self) -> Self: ...
149+
def __mul__(self, n: int) -> ViewList[_Data]: ...
150+
__rmul__: Any = ...
151+
def __imul__(self, n: int) -> Self: ...
152+
def extend(self, other: ViewList[_Data]) -> None: ...
153+
def append(self, item: _Data, source: str | None = None, offset: int = 0) -> None: ...
154+
def insert(self, i: int, item: _Data, source: str | None = None, offset: int = 0) -> None: ...
155+
def pop(self, i: int = -1) -> _Data: ...
156+
def trim_start(self, n: int = 1) -> None: ...
157+
def trim_end(self, n: int = 1) -> None: ...
158+
def remove(self, item: _Data) -> None: ...
159+
def count(self, item: _Data) -> int: ...
160+
def index(self, item: _Data) -> int: ...
161+
def reverse(self) -> None: ...
162+
def sort(self, *args: tuple[_Data, tuple[str, int]]) -> None: ...
163+
def info(self, i: int) -> tuple[str, int]: ...
164+
def source(self, i: int) -> str: ...
165+
def offset(self, i: int) -> int: ...
166+
def disconnect(self) -> None: ...
167+
def xitems(self) -> Generator[tuple[str, int, str], None, None]: ...
168+
def pprint(self) -> None: ...
169+
170+
# dummy atribute to indicate to mypy that ViewList is Iterable[str]
171+
def __iter__(self) -> Iterator[str]: ...
172+
173+
class StringList(ViewList[str]):
174+
def trim_left(self, length: int, start: int = 0, end: int = ...) -> None: ...
175+
def get_text_block(self, start: int, flush_left: bool = False) -> StringList: ...
176+
def get_indented(
177+
self,
178+
start: int = 0,
179+
until_blank: bool = False,
180+
strip_indent: bool = True,
181+
block_indent: int | None = None,
182+
first_indent: int | None = None,
183+
) -> tuple[StringList, int, bool]: ...
184+
def get_2D_block(self, top: int, left: int, bottom: int, right: int, strip_indent: bool = True) -> StringList: ...
185+
def pad_double_width(self, pad_char: str) -> None: ...
186+
def replace(self, old: str, new: str) -> None: ...
187+
188+
class StateMachineError(Exception): ...
189+
class UnknownStateError(StateMachineError): ...
190+
class DuplicateStateError(StateMachineError): ...
191+
class UnknownTransitionError(StateMachineError): ...
192+
class DuplicateTransitionError(StateMachineError): ...
193+
class TransitionPatternNotFound(StateMachineError): ...
194+
class TransitionMethodNotFound(StateMachineError): ...
195+
class UnexpectedIndentationError(StateMachineError): ...
196+
class TransitionCorrection(Exception): ...
197+
class StateCorrection(Exception): ...
198+
199+
def string2lines(
200+
astring: str, tab_width: int = 8, convert_whitespace: bool = False, whitespace: Pattern[str] = ...
201+
) -> list[str]: ...

0 commit comments

Comments
 (0)