Skip to content

Commit ce40b93

Browse files
authored
Merge pull request #860 from janga1997/typeHint6
Add type hints to geller, hunter, memorytwo
2 parents 38c2763 + c49863d commit ce40b93

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

axelrod/strategies/geller.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import inspect
22

3-
from axelrod.actions import Actions
3+
from axelrod.actions import Actions, Action
44
from axelrod.player import Player
55
from axelrod.random_ import random_choice
66

@@ -42,7 +42,7 @@ class Geller(Player):
4242
'manipulates_state': False
4343
}
4444

45-
def strategy(self, opponent):
45+
def strategy(self, opponent: Player) -> Action:
4646
"""
4747
Look at what the opponent will play in the next round and choose a strategy
4848
that gives the least jail time, which is is equivalent to playing the same

axelrod/strategies/hunter.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from axelrod.actions import Actions
1+
from axelrod.actions import Actions, Action
22
from axelrod.player import Player
33
from axelrod._strategy_utils import detect_cycle
44

5+
from typing import List, Tuple
6+
57
C, D = Actions.C, Actions.D
68

79

@@ -19,7 +21,7 @@ class DefectorHunter(Player):
1921
'manipulates_state': False
2022
}
2123

22-
def strategy(self, opponent):
24+
def strategy(self, opponent: Player) -> Action:
2325
if len(self.history) >= 4 and len(opponent.history) == opponent.defections:
2426
return D
2527
return C
@@ -39,13 +41,13 @@ class CooperatorHunter(Player):
3941
'manipulates_state': False
4042
}
4143

42-
def strategy(self, opponent):
44+
def strategy(self, opponent: Player) -> Action:
4345
if len(self.history) >= 4 and len(opponent.history) == opponent.cooperations:
4446
return D
4547
return C
4648

4749

48-
def is_alternator(history):
50+
def is_alternator(history: List[Action]) -> bool:
4951
for i in range(len(history) - 1):
5052
if history[i] == history[i + 1]:
5153
return False
@@ -66,11 +68,11 @@ class AlternatorHunter(Player):
6668
'manipulates_state': False
6769
}
6870

69-
def __init__(self):
71+
def __init__(self) -> None:
7072
super().__init__()
7173
self.is_alt = False
7274

73-
def strategy(self, opponent):
75+
def strategy(self, opponent: Player) -> Action:
7476
if len(opponent.history) < 6:
7577
return C
7678
if len(self.history) == 6:
@@ -100,11 +102,11 @@ class CycleHunter(Player):
100102
'manipulates_state': False
101103
}
102104

103-
def __init__(self):
105+
def __init__(self) -> None:
104106
super().__init__()
105-
self.cycle = None
107+
self.cycle = None # type: Tuple[Action]
106108

107-
def strategy(self, opponent):
109+
def strategy(self, opponent: Player) -> Action:
108110
if self.cycle:
109111
return D
110112
cycle = detect_cycle(opponent.history, min_size=3)
@@ -124,7 +126,7 @@ class EventualCycleHunter(CycleHunter):
124126

125127
name = 'Eventual Cycle Hunter'
126128

127-
def strategy(self, opponent):
129+
def strategy(self, opponent: Player) -> None:
128130
if len(opponent.history) < 10:
129131
return C
130132
if len(opponent.history) == opponent.cooperations:
@@ -153,7 +155,7 @@ class MathConstantHunter(Player):
153155
'manipulates_state': False
154156
}
155157

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

194-
def __init__(self):
196+
def __init__(self) -> None:
195197
self.countCC = 0
196198
self.countDD = 0
197199
super().__init__()
198200

199-
def strategy(self, opponent):
201+
def strategy(self, opponent: Player) -> Action:
200202
"""
201203
A random player is unpredictable, which means the conditional frequency
202204
of cooperation after cooperation, and defection after defections, should

axelrod/strategies/memorytwo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from axelrod.actions import Actions
1+
from axelrod.actions import Actions, Action
22
from axelrod.player import Player
33
from .titfortat import TitForTat, TitFor2Tats
44
from .defector import Defector
@@ -30,7 +30,7 @@ class MEM2(Player):
3030
'manipulates_state': False
3131
}
3232

33-
def __init__(self):
33+
def __init__(self) -> None:
3434
super().__init__()
3535
self.players = {
3636
"TFT" : TitForTat(),
@@ -41,7 +41,7 @@ def __init__(self):
4141
self.shift_counter = 3
4242
self.alld_counter = 0
4343

44-
def strategy(self, opponent):
44+
def strategy(self, opponent: Player) -> Action:
4545
# Update Histories
4646
# Note that this assumes that TFT and TFTT do not use internal counters,
4747
# Rather that they examine the actual history of play

type_tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/grumpy.py
2727
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/handshake.py
2828
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/inverse.py
2929
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/mathematicalconstants.py
30+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/hunter.py
31+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/geller.py
32+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/memorytwo.py

0 commit comments

Comments
 (0)