Skip to content
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

Add type hints to geller, hunter, memorytwo #860

Merged
merged 5 commits into from
Feb 19, 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/strategies/geller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import inspect

from axelrod.actions import Actions
from axelrod.actions import Actions, Action
from axelrod.player import Player
from axelrod.random_ import random_choice

Expand Down Expand Up @@ -42,7 +42,7 @@ class Geller(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
"""
Look at what the opponent will play in the next round and choose a strategy
that gives the least jail time, which is is equivalent to playing the same
Expand Down
28 changes: 15 additions & 13 deletions axelrod/strategies/hunter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from axelrod.actions import Actions
from axelrod.actions import Actions, Action
from axelrod.player import Player
from axelrod._strategy_utils import detect_cycle

from typing import List, Tuple

C, D = Actions.C, Actions.D


Expand All @@ -19,7 +21,7 @@ class DefectorHunter(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
if len(self.history) >= 4 and len(opponent.history) == opponent.defections:
return D
return C
Expand All @@ -39,13 +41,13 @@ class CooperatorHunter(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
if len(self.history) >= 4 and len(opponent.history) == opponent.cooperations:
return D
return C


def is_alternator(history):
def is_alternator(history: List[Action]) -> bool:
for i in range(len(history) - 1):
if history[i] == history[i + 1]:
return False
Expand All @@ -66,11 +68,11 @@ class AlternatorHunter(Player):
'manipulates_state': False
}

def __init__(self):
def __init__(self) -> None:
super().__init__()
self.is_alt = False

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
if len(opponent.history) < 6:
return C
if len(self.history) == 6:
Expand Down Expand Up @@ -100,11 +102,11 @@ class CycleHunter(Player):
'manipulates_state': False
}

def __init__(self):
def __init__(self) -> None:
super().__init__()
self.cycle = None
self.cycle = None # type: Tuple[Action]

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
if self.cycle:
return D
cycle = detect_cycle(opponent.history, min_size=3)
Expand All @@ -124,7 +126,7 @@ class EventualCycleHunter(CycleHunter):

name = 'Eventual Cycle Hunter'

def strategy(self, opponent):
def strategy(self, opponent: Player) -> None:
if len(opponent.history) < 10:
return C
if len(opponent.history) == opponent.cooperations:
Expand Down Expand Up @@ -153,7 +155,7 @@ class MathConstantHunter(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
"""
Check whether the number of cooperations in the first and second halves
of the history are close. The variance of the uniform distribution (1/4)
Expand Down Expand Up @@ -191,12 +193,12 @@ class RandomHunter(Player):
'manipulates_state': False
}

def __init__(self):
def __init__(self) -> None:
self.countCC = 0
self.countDD = 0
super().__init__()

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
"""
A random player is unpredictable, which means the conditional frequency
of cooperation after cooperation, and defection after defections, should
Expand Down
6 changes: 3 additions & 3 deletions axelrod/strategies/memorytwo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from axelrod.actions import Actions
from axelrod.actions import Actions, Action
from axelrod.player import Player
from .titfortat import TitForTat, TitFor2Tats
from .defector import Defector
Expand Down Expand Up @@ -30,7 +30,7 @@ class MEM2(Player):
'manipulates_state': False
}

def __init__(self):
def __init__(self) -> None:
super().__init__()
self.players = {
"TFT" : TitForTat(),
Expand All @@ -41,7 +41,7 @@ def __init__(self):
self.shift_counter = 3
self.alld_counter = 0

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
# Update Histories
# Note that this assumes that TFT and TFTT do not use internal counters,
# Rather that they examine the actual history of play
Expand Down
3 changes: 3 additions & 0 deletions type_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/grumpy.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/handshake.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/inverse.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/mathematicalconstants.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/hunter.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/geller.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/memorytwo.py