Skip to content

Commit e0dc2ce

Browse files
authored
Merge pull request #837 from Axelrod-Python/typing
Fix Type Annotations and add type test shell script Merged under the 'bugfix' convention!
2 parents f4629b2 + f46a5e4 commit e0dc2ce

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

axelrod/actions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from typing import NewType
21

3-
Action = NewType('Action', str)
2+
# Type alias for actions.
3+
Action = str
44

55

66
class Actions(object):

axelrod/deterministic_cache.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
from collections import UserDict
22
import pickle
3-
from typing import Tuple
3+
from typing import List, Tuple
44

5-
from axelrod import Player
5+
from .actions import Action
6+
from .player import Player
7+
8+
9+
CachePlayerKey = Tuple[Player, Player, int]
10+
CacheKey = Tuple[str, str, int]
611

712

813
class DeterministicCache(UserDict):
@@ -30,7 +35,7 @@ class DeterministicCache(UserDict):
3035
methods to save/load the cache to/from a file.
3136
"""
3237

33-
def __init__(self, file_name: str=None):
38+
def __init__(self, file_name: str=None) -> None:
3439
"""
3540
Parameters
3641
----------
@@ -43,7 +48,7 @@ def __init__(self, file_name: str=None):
4348
self.load(file_name)
4449

4550
@staticmethod
46-
def _key_transform(key: Tuple[str, str, int]):
51+
def _key_transform(key: CachePlayerKey) -> CacheKey:
4752
"""
4853
Parameters
4954
----------
@@ -52,16 +57,16 @@ def _key_transform(key: Tuple[str, str, int]):
5257
"""
5358
return key[0].name, key[1].name, key[2]
5459

55-
def __delitem__(self, key: Tuple[str, str, int]):
60+
def __delitem__(self, key: CachePlayerKey):
5661
return super().__delitem__(self._key_transform(key))
5762

58-
def __getitem__(self, key: Tuple[str, str, int]):
63+
def __getitem__(self, key: CachePlayerKey) -> List[Tuple[Action, Action]]:
5964
return super().__getitem__(self._key_transform(key))
6065

61-
def __contains__(self, key: Tuple[str, str, int]):
66+
def __contains__(self, key):
6267
return super().__contains__(self._key_transform(key))
6368

64-
def __setitem__(self, key: Tuple[str, str, int], value):
69+
def __setitem__(self, key: CachePlayerKey, value):
6570
"""Overrides the UserDict.__setitem__ method in order to validate
6671
the key/value and also to set the turns attribute"""
6772
if not self.mutable:
@@ -79,7 +84,7 @@ def __setitem__(self, key: Tuple[str, str, int], value):
7984
super().__setitem__(self._key_transform(key), value)
8085

8186
@staticmethod
82-
def _is_valid_key(key: Tuple[str, str, int]) -> bool:
87+
def _is_valid_key(key: CachePlayerKey) -> bool:
8388
"""Validate a proposed dictionary key.
8489
8590
Parameters
@@ -117,7 +122,7 @@ def _is_valid_key(key: Tuple[str, str, int]) -> bool:
117122
return True
118123

119124
@staticmethod
120-
def _is_valid_value(value) -> bool:
125+
def _is_valid_value(value: List) -> bool:
121126
"""Validate a proposed dictionary value.
122127
123128
Parameters
@@ -134,7 +139,7 @@ def _is_valid_value(value) -> bool:
134139

135140
return True
136141

137-
def save(self, file_name: str):
142+
def save(self, file_name: str) -> bool:
138143
"""Serialise the cache dictionary to a file.
139144
140145
Parameters

axelrod/game.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
from axelrod import Actions
2-
from typing import Tuple
1+
from .actions import Action, Actions
2+
from typing import Tuple, Union
33

44
C, D = Actions.C, Actions.D
55

6+
Score = Union[int, float]
7+
68

79
class Game(object):
810
"""A class to hold the game matrix and to score a game accordingly."""
911

10-
def __init__(self, r: int =3, s: int=0, t: int=5, p:int=1):
12+
def __init__(self, r: Score=3, s: Score=0, t: Score=5, p: Score=1) -> None:
1113
self.scores = {
1214
(C, C): (r, r),
1315
(D, D): (p, p),
1416
(C, D): (s, t),
1517
(D, C): (t, s),
1618
}
1719

18-
def RPST(self):
20+
def RPST(self) -> Tuple[Score, Score, Score, Score]:
1921
"""Return the values in the game matrix in the Press and Dyson
2022
notation."""
2123
R = self.scores[(C, C)][0]
@@ -24,7 +26,7 @@ def RPST(self):
2426
T = self.scores[(D, C)][0]
2527
return (R, P, S, T)
2628

27-
def score(self, pair) -> Tuple[int, int]:
29+
def score(self, pair: Tuple[Action, Action]) -> Tuple[Score, Score]:
2830
"""Return the appropriate score for decision pair.
2931
3032
Returns the appropriate score (as a tuple) from the scores dictionary
@@ -33,7 +35,8 @@ def score(self, pair) -> Tuple[int, int]:
3335
"""
3436
return self.scores[pair]
3537

36-
def __repr__(self):
38+
def __repr__(self) -> str:
3739
return "Axelrod game: (R,P,S,T) = {}".format(self.RPST())
3840

41+
3942
DefaultGame = Game()

type_tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mypy --ignore-missing-imports --follow-imports skip axelrod/actions.py
2+
mypy --ignore-missing-imports --follow-imports skip axelrod/deterministic_cache.py
3+
mypy --ignore-missing-imports --follow-imports skip axelrod/game.py
4+

0 commit comments

Comments
 (0)