Skip to content

Commit 244e219

Browse files
type hints
1 parent 01c80fc commit 244e219

5 files changed

Lines changed: 34 additions & 14 deletions

File tree

sifter/grammar/actions.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33

44
class Actions(list):
55

6-
def __init__(self, implicit_keep=False):
6+
def __init__(self, implicit_keep: bool = False) -> None:
77
super(Actions, self).__init__()
88
self.implicit_keep = implicit_keep
99

10-
def append(self, action, action_args=None):
10+
def append(self, action, action_args=None) -> None:
1111
super(Actions, self).append((action, action_args))
12-
return self
1312

14-
def cancel_implicit_keep(self):
13+
def cancel_implicit_keep(self) -> 'Actions':
1514
self.implicit_keep = False
1615
return self

sifter/grammar/command.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
from typing import (
2-
Text
2+
TYPE_CHECKING,
3+
Text,
4+
Optional,
5+
List
36
)
47

58
import sifter.grammar
69
from sifter.grammar.command_list import CommandList
7-
from sifter.grammar.rule import RuleSyntaxError
8-
from . import rule
10+
from sifter.grammar.rule import Rule, RuleSyntaxError
911
import sifter.utils
1012

13+
if TYPE_CHECKING:
14+
from sifter.grammar.tag import Tag
15+
1116
__all__ = ('Command',)
1217

1318

14-
class Command(rule.Rule):
19+
class Command(Rule):
1520

1621
RULE_TYPE: Text = 'command'
1722

18-
def __init__(self, arguments=None, tests=None, block=None) -> None:
23+
def __init__(self, arguments: Optional[List['Tag']] = None, tests=None, block=None) -> None:
1924
super(Command, self).__init__(arguments, tests)
2025
if block is None:
2126
self.block = CommandList()

sifter/grammar/command_list.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1+
from email.message import Message
2+
from typing import (
3+
Text
4+
)
5+
16
from sifter.grammar.state import EvaluationState
7+
from sifter.grammar.actions import Actions
28

39
__all__ = ('CommandList',)
410

511

612
class CommandList(object):
713

8-
def __init__(self, command_list=None):
14+
def __init__(self, command_list=None) -> None:
915
if command_list is None:
1016
self.commands = []
1117
else:
1218
self.commands = command_list
1319

14-
def __str__(self):
20+
def __str__(self) -> Text:
1521
return ''.join(cmd.__str__() for cmd in self.commands)
1622

17-
def evaluate(self, message, state=None):
23+
def evaluate(self, message: Message, state: EvaluationState = None) -> Actions:
1824
if state is None:
1925
state = EvaluationState()
2026
for command in self.commands:

sifter/grammar/rule.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
)
1010

1111
from sifter.grammar.tag import Tag
12+
from sifter.grammar.string import String
1213
from sifter.grammar.state import EvaluationState
1314
from sifter.grammar.validator import Validator
1415
import sifter.grammar
@@ -36,7 +37,10 @@ def register(cls) -> None:
3637
# only on subclasses that implement specific rules
3738
raise NotImplementedError
3839

39-
def __init__(self, arguments: Optional[List[Any]] = None, tests: Optional[List[Any]] = None) -> None:
40+
def __init__(self, arguments: Optional[List[Tag]] = None, tests: Optional[List[Any]] = None) -> None:
41+
if arguments:
42+
print(arguments)
43+
print([type(x) for x in arguments])
4044
if arguments is None:
4145
self.arguments = []
4246
else:

sifter/tests/size.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import operator
22
from email.message import Message
3+
from typing import (
4+
Any,
5+
Callable,
6+
Dict,
7+
Text
8+
)
39

410
from sifter.grammar.test import Test
511
from sifter.validators.tag import Tag
@@ -14,7 +20,7 @@ class TestSize(Test):
1420

1521
RULE_IDENTIFIER = 'SIZE'
1622

17-
COMPARISON_FNS = {
23+
COMPARISON_FNS: Dict[Text, Callable[[Any, Any], bool]] = {
1824
'OVER': operator.gt,
1925
'UNDER': operator.lt,
2026
}

0 commit comments

Comments
 (0)