Skip to content

Fix Type Annotations and add type test shell script #837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions axelrod/actions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import NewType

Action = NewType('Action', str)
# Type alias for actions.
Action = str


class Actions(object):
Expand Down
27 changes: 16 additions & 11 deletions axelrod/deterministic_cache.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from collections import UserDict
import pickle
from typing import Tuple
from typing import List, Tuple

from axelrod import Player
from .actions import Action
from .player import Player


CachePlayerKey = Tuple[Player, Player, int]
CacheKey = Tuple[str, str, int]


class DeterministicCache(UserDict):
Expand Down Expand Up @@ -30,7 +35,7 @@ class DeterministicCache(UserDict):
methods to save/load the cache to/from a file.
"""

def __init__(self, file_name: str=None):
def __init__(self, file_name: str=None) -> None:
"""
Parameters
----------
Expand All @@ -43,7 +48,7 @@ def __init__(self, file_name: str=None):
self.load(file_name)

@staticmethod
def _key_transform(key: Tuple[str, str, int]):
def _key_transform(key: CachePlayerKey) -> CacheKey:
"""
Parameters
----------
Expand All @@ -52,16 +57,16 @@ def _key_transform(key: Tuple[str, str, int]):
"""
return key[0].name, key[1].name, key[2]

def __delitem__(self, key: Tuple[str, str, int]):
def __delitem__(self, key: CachePlayerKey):
return super().__delitem__(self._key_transform(key))

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

def __contains__(self, key: Tuple[str, str, int]):
def __contains__(self, key):
return super().__contains__(self._key_transform(key))

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

@staticmethod
def _is_valid_key(key: Tuple[str, str, int]) -> bool:
def _is_valid_key(key: CachePlayerKey) -> bool:
"""Validate a proposed dictionary key.

Parameters
Expand Down Expand Up @@ -117,7 +122,7 @@ def _is_valid_key(key: Tuple[str, str, int]) -> bool:
return True

@staticmethod
def _is_valid_value(value) -> bool:
def _is_valid_value(value: List) -> bool:
"""Validate a proposed dictionary value.

Parameters
Expand All @@ -134,7 +139,7 @@ def _is_valid_value(value) -> bool:

return True

def save(self, file_name: str):
def save(self, file_name: str) -> bool:
"""Serialise the cache dictionary to a file.

Parameters
Expand Down
15 changes: 9 additions & 6 deletions axelrod/game.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
from axelrod import Actions
from typing import Tuple
from .actions import Action, Actions
from typing import Tuple, Union

C, D = Actions.C, Actions.D

Score = Union[int, float]


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

def __init__(self, r: int =3, s: int=0, t: int=5, p:int=1):
def __init__(self, r: Score=3, s: Score=0, t: Score=5, p: Score=1) -> None:
self.scores = {
(C, C): (r, r),
(D, D): (p, p),
(C, D): (s, t),
(D, C): (t, s),
}

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

def score(self, pair) -> Tuple[int, int]:
def score(self, pair: Tuple[Action, Action]) -> Tuple[Score, Score]:
"""Return the appropriate score for decision pair.

Returns the appropriate score (as a tuple) from the scores dictionary
Expand All @@ -33,7 +35,8 @@ def score(self, pair) -> Tuple[int, int]:
"""
return self.scores[pair]

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


DefaultGame = Game()
4 changes: 4 additions & 0 deletions type_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mypy --ignore-missing-imports --follow-imports skip axelrod/actions.py
mypy --ignore-missing-imports --follow-imports skip axelrod/deterministic_cache.py
mypy --ignore-missing-imports --follow-imports skip axelrod/game.py