Skip to content

Commit 3da4fa2

Browse files
committed
Fix typing issues detected by mypy
1 parent 82fd449 commit 3da4fa2

File tree

9 files changed

+25
-19
lines changed

9 files changed

+25
-19
lines changed

contextshell/Action.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
class Action(ABC):
8-
def __init__(self, name: NodePath):
8+
def __init__(self, name: NodePath) -> None:
99
assert isinstance(name, NodePath)
1010
assert name.is_relative
1111
self.name: NodePath = name

contextshell/CallableAction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
class CallableAction(Action):
9-
def __init__(self, implementation: Callable, name: NodePath):
9+
def __init__(self, implementation: Callable, name: NodePath) -> None:
1010
super().__init__(name)
1111
self.implementation = implementation
1212

contextshell/CommandInterpreter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
class CommandInterpreter:
8-
def __init__(self, tree: TreeRoot):
8+
def __init__(self, tree: TreeRoot) -> None:
99
self.tree = tree
1010

1111
def execute(self, command: Command):
@@ -17,8 +17,8 @@ def execute(self, command: Command):
1717
target_path = NodePath.cast(target_path)
1818
action_path = NodePath.cast(self._evaluate(command.name))
1919
arguments = list(map(self._evaluate, command.arguments))
20-
arguments = parse_argument_tree(arguments)
21-
return self.tree.execute(target_path, action_path, arguments)
20+
packed_arguments = parse_argument_tree(arguments)
21+
return self.tree.execute(target_path, action_path, packed_arguments)
2222

2323
def _evaluate(self, part):
2424
if isinstance(part, Command):

contextshell/CommandParser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from contextshell.Command import Command
2-
from typing import List
2+
from typing import List, Optional
33

44

55
def convert_token_type(token):
@@ -55,7 +55,7 @@ class CommandParser:
5555
def __init__(self):
5656
self._root_scope = None
5757

58-
def parse(self, command_line: str) -> Command:
58+
def parse(self, command_line: str) -> Optional[Command]:
5959
tokens = tokenize(command_line)
6060
if len(tokens) == 0:
6161
return None # ignore empty lines

contextshell/Node.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from typing import Optional
2+
3+
14
class Node:
25
def __init__(self, value=None):
36
self._value = value
@@ -41,7 +44,7 @@ def append(self, node, name: str=None):
4144
node._parent = self
4245
self._subnodes.append((name, node))
4346

44-
def get_node(self, name: str=None, index: int=None) -> 'Node':
47+
def get_node(self, name: str=None, index: int=None) -> Optional['Node']:
4548
"""Return subnode with provided name or index"""
4649
if name is not None:
4750
for p in self._subnodes:

contextshell/NodePath.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ def _parse_path(self, text):
9292
new_path = map(NodePath._to_path_part, [part for part in text.split(NodePath.separator) if len(part) > 0])
9393
self.extend(new_path)
9494

95-
def __eq__(self, other: 'NodePath'):
95+
def __eq__(self, other):
9696
other = NodePath.cast(other)
9797
return self.is_absolute == other.is_absolute and self[:] == other[:]
9898

99-
def __ne__(self, other: 'NodePath'):
99+
def __ne__(self, other):
100100
return not (self == other)
101101

102102
def __str__(self):

contextshell/NodeTreeRoot.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _is_action_implementation(self, node_value) -> bool:
8989
# def install_type(self, type: NodeType):
9090
# raise NotImplementedError()
9191

92-
def is_attribute(self, path: NodePath):
92+
def is_attribute(self, path: str): # FIXME: is used?
9393
return path.startswith('@')
9494

9595
def list_actions(self, path: NodePath) -> List[NodePath]:
@@ -149,11 +149,12 @@ def _resolve_path(self, path: NodePath, root: Node = None) -> Node:
149149
raise NameError("'{}' doesn't exists".format(path))
150150
return node
151151

152-
def _resolve_optional_path(self, path: NodePath, root: Node=None) -> Node:
152+
def _resolve_optional_path(self, path: NodePath, root: Node=None) -> Optional[Node]:
153153
if root is None:
154154
if path.is_relative:
155155
raise ValueError("Could not resolve relative paths")
156156
root = self.root
157+
assert root is not None
157158
if len(path) == 0:
158159
return root
159160

@@ -167,6 +168,7 @@ def _create_path(self, path: NodePath, root: Node = None) -> Node:
167168
if path.is_relative:
168169
raise ValueError("Could not resolve relative paths")
169170
root = self.root
171+
assert root is not None
170172
if len(path) == 0:
171173
return root
172174

contextshell/Shell.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from contextshell.CommandParser import CommandParser
22
from contextshell.NodePath import *
33
from contextshell.CommandInterpreter import CommandInterpreter
4+
from typing import Optional
45

56

67
class Shell:
@@ -9,7 +10,7 @@ def __init__(self, interpreter):
910
self.interpreter = interpreter
1011
self.parser = CommandParser()
1112

12-
def execute(self, command_line: str) -> str:
13+
def execute(self, command_line: str) -> Optional[str]:
1314
command = self.parser.parse(command_line)
1415
if command is None:
1516
return None
@@ -22,7 +23,7 @@ def execute(self, command_line: str) -> str:
2223
except Exception as error:
2324
return self.format_error(error)
2425

25-
def format_result(self, result) -> str:
26+
def format_result(self, result) -> Optional[str]:
2627
if result is None:
2728
return None
2829
if isinstance(result, list):

contextshell/TreeRoot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ def execute(self, target: NodePath, action: NodePath, args: ActionArgsPack):
1414

1515
def unpack_argument_tree(action_args: ActionArgsPack) -> Tuple[List[Any], Dict[str, Any]]:
1616
args = dict()
17-
kwargs = OrderedDict()
17+
kwargs: Dict[str, Any] = OrderedDict()
1818
for key, value in action_args.items():
1919
if isinstance(key, int):
2020
args[key] = value
2121
else:
2222
kwargs[key.to_python_name()] = value
2323
assert len(args) == 0 or max(args.keys()) < len(args)+len(kwargs)
24-
args = [a[1] for a in sorted(args.items())]
25-
return args, kwargs
24+
positional_args = [a[1] for a in sorted(args.items())]
25+
return positional_args, kwargs
2626

2727

2828
def pack_argument_tree(args: List[Any], kwargs: Dict[str, Any]) -> ActionArgsPack:
29-
pack_list = []
29+
pack_list: List[Tuple[Union[NodePath, int], Any]] = []
3030
for i, arg in enumerate(args):
3131
pack_list.append((i, arg))
3232
for key, value in kwargs.items():
@@ -36,7 +36,7 @@ def pack_argument_tree(args: List[Any], kwargs: Dict[str, Any]) -> ActionArgsPac
3636

3737
def parse_argument_tree(raw_arguments: List[str]) -> ActionArgsPack:
3838
from contextshell.CommandParser import convert_token_type
39-
pack_list = []
39+
pack_list: List[Tuple[Union[NodePath, int], Any]] = []
4040
for i, arg in enumerate(raw_arguments):
4141
if isinstance(arg, str) and '=' in arg:
4242
key, value = arg.split('=')

0 commit comments

Comments
 (0)