Skip to content

Commit db43d9c

Browse files
committed
📇 Add type hints to all the code
Note that, as of the time of writing, straightforward type checks are fine, but strict checking still fails due to python/mypy#7316
1 parent 0dbe40a commit db43d9c

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

journeys/commands.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
"""Module that provides a method of playing robot commands."""
22

3+
##############################################################################
4+
# Python imports.
5+
from typing import List
6+
7+
##############################################################################
8+
# Local imports.
9+
from .state import State
10+
311
##############################################################################
412
# Exception used to convey problems with the commands.
513
class CommandException( Exception ):
@@ -15,7 +23,7 @@ class CommandException( Exception ):
1523

1624
##############################################################################
1725
# Perform a list of commands starting with an initial state.
18-
def perform( commands, state ):
26+
def perform( commands: List[ str ], state: State ) -> State:
1927
"""Perform a list of commands for a robot.
2028
2129
:param list commands: The list of commands to perform.

journeys/journey.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
##############################################################################
44
# General imports.
55
from dataclasses import dataclass
6+
from typing import List
67

78
##############################################################################
89
# Local imports.
@@ -27,10 +28,10 @@ class Journey:
2728
end_state: State
2829

2930
#: The commands for the robot.
30-
commands: list
31+
commands: List[ str ]
3132

3233
@property
33-
def is_valid( self ):
34+
def is_valid( self ) -> bool:
3435
"""Does the journey data look valid?
3536
3637
:type: bool

journeys/reader.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
##############################################################################
44
# General imports.
55
import re
6+
from typing import List, Iterator, TextIO
67

78
##############################################################################
89
# Local imports.
@@ -26,7 +27,7 @@ class InvalidCommands( Exception ):
2627

2728
##############################################################################
2829
# Parse the given line as state.
29-
def parse_state( text ):
30+
def parse_state( text: str ) -> State:
3031
"""Parse a given line of text as a robot state.
3132
3233
:param str text: The text to parse.
@@ -53,12 +54,12 @@ def parse_state( text ):
5354

5455
##############################################################################
5556
# Parse a command line.
56-
def parse_commands( text ):
57+
def parse_commands( text: str ) -> List[ str ]:
5758
"""Parse a line of text as robot commands.
5859
5960
:param str text: The line of text to parse.
6061
:returns: A list of commands to perform.
61-
:rtype: list
62+
:rtype: list[ str ]
6263
:raises InvalidCommands:
6364
"""
6465

@@ -71,11 +72,11 @@ def parse_commands( text ):
7172

7273
##############################################################################
7374
# Class for reading in the journey input.
74-
def journeys( source ):
75+
def journeys( source: TextIO ) -> Iterator[ Journey ]:
7576
"""Read the journeys found in the given source.
7677
77-
:param TextIOBase source: A stream that is a source of journeys.
78-
:returns: A journey generator.
78+
:param TextIO source: A stream that is a source of journeys.
79+
:returns: A journey iterator.
7980
:rtype: Iterator[Journey]
8081
"""
8182

journeys/state.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
##############################################################################
44
# General imports.
55
from dataclasses import dataclass, replace
6+
from typing import List
67

78
##############################################################################
89
# Exception used to convey problems with the State class.
@@ -49,7 +50,7 @@ class State:
4950
#: Direction the robot is facing.
5051
facing: str = EAST
5152

52-
def _turn( self, compass ):
53+
def _turn( self, compass: List[ str ] ) -> State:
5354
"""Make a 90 degree turn on the given compass.
5455
5556
:param list compass: List of compass directions to turn through.
@@ -58,23 +59,23 @@ def _turn( self, compass ):
5859
"""
5960
return replace( self, facing=compass[ compass.index( self.facing ) - 1 ] )
6061

61-
def left( self ):
62+
def left( self ) -> State:
6263
"""Make a 90 degree turn to the left.
6364
6465
:returns: A new state.
6566
:rtype: State
6667
"""
6768
return self._turn( COMPASS )
6869

69-
def right( self ):
70+
def right( self ) -> State:
7071
"""Make a 90 degree turn to the right.
7172
7273
:returns: A new state.
7374
:rtype: State
7475
"""
7576
return self._turn( list( reversed( COMPASS ) ) )
7677

77-
def forward( self ):
78+
def forward( self ) -> State:
7879
"""Move forward one position.
7980
8081
:returns: self
@@ -96,7 +97,7 @@ def forward( self ):
9697
# We appear to have being facing in a direction we don't know about.
9798
raise StateException( f"Facing in unknown direction '{self.facing}'" )
9899

99-
def __str__( self ):
100+
def __str__( self ) -> str:
100101
"""Return a string representation of the state."""
101102
return f"{self.x_pos} {self.y_pos} {self.facing}"
102103

0 commit comments

Comments
 (0)