Skip to content

Commit 3acedec

Browse files
authored
refactor: normalize typing (#22)
1 parent 8b18b84 commit 3acedec

File tree

12 files changed

+62
-88
lines changed

12 files changed

+62
-88
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "superstate"
7-
version = "1.6.2a2"
7+
version = "1.6.2a3"
88
description = "Robust statechart for configurable automation rules."
99
readme = "README.md"
1010
license = {file = "LICENSE"}

src/superstate/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
__author_email__ = 'jpj6652@gmail.com'
6565
__title__ = 'superstate'
6666
__description__ = 'Compact statechart that can be vendored.'
67-
__version__ = '1.6.2a2'
67+
__version__ = '1.6.2a3'
6868
__license__ = 'MIT'
6969
__copyright__ = 'Copyright 2022 Jesse Johnson.'
7070
__all__ = (

src/superstate/context.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
import os
6-
from typing import TYPE_CHECKING, List, Tuple
6+
from typing import TYPE_CHECKING
77
from uuid import UUID
88

99
from superstate.state import ParallelState
@@ -56,7 +56,7 @@ def parent(self) -> State:
5656
return self.current_state.parent or self.root
5757

5858
@property
59-
def children(self) -> Tuple[State, ...]:
59+
def children(self) -> tuple[State, ...]:
6060
"""Return list of states."""
6161
return (
6262
tuple(self.__current_state.states.values())
@@ -65,19 +65,19 @@ def children(self) -> Tuple[State, ...]:
6565
)
6666

6767
@property
68-
def states(self) -> Tuple[State, ...]:
68+
def states(self) -> tuple[State, ...]:
6969
"""Return list of states."""
7070
return tuple(self.parent.states.values())
7171

7272
@property
73-
def siblings(self) -> Tuple[State, ...]:
73+
def siblings(self) -> tuple[State, ...]:
7474
"""Return list of states."""
7575
return tuple(self.parent.states.values())
7676

7777
@property
78-
def active(self) -> Tuple[State, ...]:
78+
def active(self) -> tuple[State, ...]:
7979
"""Return active states."""
80-
states: List[State] = []
80+
states: list[State] = []
8181
parents = list(reversed(self.current_state))
8282
for i, x in enumerate(parents):
8383
n = i + 1

src/superstate/machine.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,7 @@
77
import os
88
from copy import deepcopy
99
from itertools import zip_longest
10-
from typing import (
11-
TYPE_CHECKING,
12-
Any,
13-
Dict,
14-
Iterator,
15-
List,
16-
Optional,
17-
# Sequence,
18-
Tuple,
19-
cast,
20-
)
10+
from typing import TYPE_CHECKING, Any, Iterator, Optional, cast
2111
from uuid import UUID
2212

2313
from superstate.config import DEFAULT_BINDING, DEFAULT_PROVIDER
@@ -60,8 +50,8 @@ class MetaStateChart(type):
6050
def __new__(
6151
mcs,
6252
name: str,
63-
bases: Tuple[type, ...],
64-
attrs: Dict[str, Any],
53+
bases: tuple[type, ...],
54+
attrs: dict[str, Any],
6555
) -> 'MetaStateChart':
6656
if '__name__' not in attrs:
6757
name = name.lower()
@@ -218,7 +208,7 @@ def parent(self) -> SubstateMixin:
218208
return self.current_state.parent or self.root
219209

220210
@property
221-
def children(self) -> Tuple[State, ...]:
211+
def children(self) -> tuple[State, ...]:
222212
"""Return list of states."""
223213
return (
224214
tuple(self.__current_state.states.values())
@@ -227,19 +217,19 @@ def children(self) -> Tuple[State, ...]:
227217
)
228218

229219
@property
230-
def states(self) -> Tuple[State, ...]:
220+
def states(self) -> tuple[State, ...]:
231221
"""Return list of states."""
232222
return tuple(self.parent.states.values())
233223

234224
@property
235-
def siblings(self) -> Tuple[State, ...]:
225+
def siblings(self) -> tuple[State, ...]:
236226
"""Return list of states."""
237227
return tuple(self.parent.states.values())
238228

239229
@property
240-
def active(self) -> Tuple[State, ...]:
230+
def active(self) -> tuple[State, ...]:
241231
"""Return active states."""
242-
states: List[State] = []
232+
states: list[State] = []
243233
parents = list(reversed(self.current_state))
244234
for i, x in enumerate(parents):
245235
n = i + 1

src/superstate/model/action.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,9 @@
44

55
import logging
66
import logging.config
7+
from collections.abc import Callable
78
from dataclasses import InitVar, asdict, dataclass
8-
from typing import (
9-
TYPE_CHECKING,
10-
Any,
11-
Callable,
12-
List,
13-
Optional,
14-
Sequence,
15-
Union,
16-
)
9+
from typing import TYPE_CHECKING, Any, Optional, Sequence, Union
1710

1811
from superstate.config import LOGGING_CONFIG
1912
from superstate.model.base import Action, Conditional
@@ -57,12 +50,12 @@ def callback(self, provider: Provider, *args: Any, **kwargs: Any) -> None:
5750
class ForEach(Action):
5851
"""Data item providing state data."""
5952

60-
content: InitVar[List[str]]
53+
content: InitVar[list[str]]
6154
array: str
6255
item: str
6356
index: Optional[str] = None # expression
6457

65-
def __post_init__(self, content: List[str]) -> None:
58+
def __post_init__(self, content: list[str]) -> None:
6659
self.__content = [Action.create(x) for x in content] # type: ignore
6760

6861
def callback(self, provider: Provider, *args: Any, **kwargs: Any) -> None:

src/superstate/model/data.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
TYPE_CHECKING,
1111
Any,
1212
ClassVar,
13-
# Dict,
14-
List,
1513
Optional,
1614
# Sequence,
1715
Type,
@@ -87,7 +85,7 @@ def value(self) -> Optional[Any]:
8785
class DataModel(ChainMap):
8886
"""Instantiate state types from class metadata."""
8987

90-
data: List[Data]
88+
data: list[Data]
9189
binding: ClassVar[str] = 'early'
9290
provider: ClassVar[Type[Provider]] = Default
9391

@@ -132,5 +130,5 @@ def populate(self) -> None:
132130
class DoneData:
133131
"""Data model providing state data."""
134132

135-
param: List[Data]
133+
param: list[Data]
136134
content: Optional[Any] = None

src/superstate/provider/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Provide common types for statechart components."""
22

3-
from typing import Dict, Type
3+
from typing import Type
44

55
from superstate.provider.base import Provider
66
from superstate.provider.default import Default
@@ -18,7 +18,7 @@
1818
# 'XPath',
1919
)
2020

21-
PROVIDERS: Dict[str, Type[Provider]] = {
21+
PROVIDERS: dict[str, Type[Provider]] = {
2222
'default': Default,
2323
# 'ecmasscript': ECMASript,
2424
# 'null': Null,

src/superstate/provider/base.py

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

33
import re
44
from abc import ABC, abstractmethod # pylint: disable=no-name-in-module
5+
from collections.abc import Callable
56
from functools import singledispatchmethod
67
from typing import (
78
TYPE_CHECKING,
89
Any,
9-
Callable,
10-
Dict,
1110
Optional,
1211
Type,
1312
TypeVar,
@@ -64,7 +63,7 @@ def get_provider(cls, name: str) -> Type['Provider']:
6463
# )
6564

6665
@property
67-
def globals(self) -> Dict[str, Any]:
66+
def globals(self) -> dict[str, Any]:
6867
"""Get global attributes and methods available for eval and exec."""
6968
# pylint: disable=import-outside-toplevel
7069
from datetime import datetime
@@ -75,7 +74,7 @@ def globals(self) -> Dict[str, Any]:
7574
return glb
7675

7776
@property
78-
def locals(self) -> Dict[str, Any]:
77+
def locals(self) -> dict[str, Any]:
7978
"""Get local attributes and methods available for eval and exec."""
8079
lcl = dict(self.ctx.current_state.datamodel)
8180
lcl['In'] = self.In

src/superstate/state.py

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,7 @@
44

55
import logging
66
from itertools import chain # , zip_longest
7-
from typing import (
8-
TYPE_CHECKING,
9-
Any,
10-
Dict,
11-
Generator,
12-
List,
13-
Optional,
14-
Tuple,
15-
Union,
16-
cast,
17-
)
7+
from typing import TYPE_CHECKING, Any, Generator, Optional, Union, cast
188

199
from superstate.exception import (
2010
InvalidConfig,
@@ -39,16 +29,16 @@
3929
#
4030
# initial: Optional[Initial]
4131
# kind: Optional[str]
42-
# states: List[State]
43-
# transitions: List[State]
32+
# states: list[State]
33+
# transitions: list[State]
4434
# on_entry: Optional[ActionTypes]
4535
# on_exit: Optional[ActionTypes]
4636
#
4737
# def __new__(
4838
# cls,
4939
# name: str,
50-
# bases: Tuple[type, ...],
51-
# attrs: Dict[str, Any],
40+
# bases: tuple[type, ...],
41+
# attrs: dict[str, Any],
5242
# ) -> 'MetaState':
5343
# initial = attrs.pop('initial', None)
5444
# kind = attrs.pop('type', None)
@@ -70,23 +60,23 @@
7060
class TransitionMixin:
7161
"""Provide an atomic state for a statechart."""
7262

73-
__transitions: List[Transition]
63+
__transitions: list[Transition]
7464

7565
@property
76-
def transitions(self) -> Tuple[Transition, ...]:
66+
def transitions(self) -> tuple[Transition, ...]:
7767
"""Return transitions of this state."""
7868
return tuple(self.__transitions)
7969

8070
@transitions.setter
81-
def transitions(self, transitions: List[Transition]) -> None:
71+
def transitions(self, transitions: list[Transition]) -> None:
8272
"""Initialize atomic state."""
8373
self.__transitions = transitions
8474

8575
def add_transition(self, transition: Transition) -> None:
8676
"""Add transition to this state."""
8777
self.__transitions.append(transition)
8878

89-
def get_transition(self, event: str) -> Tuple[Transition, ...]:
79+
def get_transition(self, event: str) -> tuple[Transition, ...]:
9080
"""Get each transition maching event."""
9181
return tuple(
9282
filter(
@@ -161,15 +151,15 @@ class State:
161151
# '__type',
162152
# ]
163153

164-
__stack: List[State]
154+
__stack: list[State]
165155
datamodel: DataModel
166156
name: str = cast(str, Identifier())
167157
# history: Optional['HistoryState']
168158
# final: Optional[FinalState]
169-
# states: Dict[str, State]
170-
# transitions: Tuple[Transition, ...]
171-
# onentry: Tuple[ActionTypes, ...]
172-
# onexit: Tuple[ActionTypes, ...]
159+
states: dict[str, State]
160+
# transitions: tuple[Transition, ...]
161+
# onentry: tuple[ActionTypes, ...]
162+
# onexit: tuple[ActionTypes, ...]
173163

174164
# pylint: disable-next=unused-argument
175165
def __new__(cls, *args: Any, **kwargs: Any) -> State:
@@ -193,7 +183,7 @@ def __new__(cls, *args: Any, **kwargs: Any) -> State:
193183
def __init__(
194184
self, # pylint: disable=unused-argument
195185
name: str,
196-
# settings: Optional[Dict[str, Any]] = None,
186+
# settings: Optional[dict[str, Any]] = None,
197187
# /,
198188
**kwargs: Any,
199189
) -> None:
@@ -488,7 +478,7 @@ def run_on_entry(self, ctx: StateChart) -> Optional[Any]:
488478
class SubstateMixin(State):
489479
"""Provide composite abstract to define nested state types."""
490480

491-
__states: Dict[str, State] = {}
481+
__states: dict[str, State] = {}
492482

493483
def __getattr__(self, name: str) -> Any:
494484
if name.startswith('__'):
@@ -499,12 +489,12 @@ def __getattr__(self, name: str) -> Any:
499489
raise AttributeError
500490

501491
@property
502-
def states(self) -> Dict[str, State]:
492+
def states(self) -> dict[str, State]:
503493
"""Return states."""
504494
return self.__states
505495

506496
@states.setter
507-
def states(self, states: List[State]) -> None:
497+
def states(self, states: list[State]) -> None:
508498
"""Define states."""
509499
if not self.__states:
510500
self.__states = {}
@@ -529,7 +519,7 @@ def get_state(self, name: str) -> Optional[State]:
529519
# class SubstateMixin(State):
530520
# """Provide composite abstract to define nested state types."""
531521
#
532-
# __states: List[State] = []
522+
# __states: list[State] = []
533523
#
534524
# def __getattr__(self, name: str) -> Any:
535525
# if name.startswith('__'):
@@ -540,12 +530,12 @@ def get_state(self, name: str) -> Optional[State]:
540530
# raise AttributeError
541531
#
542532
# @property
543-
# def states(self) -> List[State]:
533+
# def states(self) -> list[State]:
544534
# """Return states."""
545535
# return self.__states
546536
#
547537
# @states.setter
548-
# def states(self, states: List[State]) -> None:
538+
# def states(self, states: list[State]) -> None:
549539
# """Define states."""
550540
# if not self.__states:
551541
# for state in states:
@@ -580,7 +570,7 @@ def __init__(self, name: str, **kwargs: Any) -> None:
580570
self.states = kwargs.pop('states', [])
581571
super().__init__(name, **kwargs)
582572

583-
def run_on_entry(self, ctx: StateChart) -> Optional[Tuple[Any, ...]]:
573+
def run_on_entry(self, ctx: StateChart) -> Optional[tuple[Any, ...]]:
584574
# if next(
585575
# (x for x in self.states if isinstance(x, HistoryState)), False
586576
# ):
@@ -596,7 +586,7 @@ def run_on_entry(self, ctx: StateChart) -> Optional[Tuple[Any, ...]]:
596586
)
597587
if initial and ctx.current_state != initial:
598588
ctx.change_state(initial)
599-
results: List[Any] = []
589+
results: list[Any] = []
600590
results += filter(None, [super().run_on_entry(ctx)])
601591
# XXX: self transitions should still be possible here
602592
if (

0 commit comments

Comments
 (0)