Skip to content

Commit 8d210df

Browse files
authored
Merge pull request #864 from janga1997/typeHint8
Add type hints to oncebitten, prober, punisher, rand
2 parents c8cd1fc + 53ba780 commit 8d210df

5 files changed

Lines changed: 38 additions & 33 deletions

File tree

axelrod/strategies/oncebitten.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import random
2-
from axelrod.actions import Actions
2+
from axelrod.actions import Actions, Action
33
from axelrod.player import Player
44

55
C, D = Actions.C, Actions.D
@@ -22,13 +22,13 @@ class OnceBitten(Player):
2222
'manipulates_state': False
2323
}
2424

25-
def __init__(self):
25+
def __init__(self) -> None:
2626
super().__init__()
2727
self.mem_length = 10
2828
self.grudged = False
2929
self.grudge_memory = 0
3030

31-
def strategy(self, opponent):
31+
def strategy(self, opponent: Player) -> Action:
3232
"""
3333
Begins by playing C, then plays D for mem_length rounds if the opponent
3434
ever plays D twice in a row.
@@ -72,7 +72,7 @@ class FoolMeOnce(Player):
7272
}
7373

7474
@staticmethod
75-
def strategy(opponent):
75+
def strategy(opponent: Player) -> Action:
7676
if not opponent.history:
7777
return C
7878
if opponent.defections > 1:
@@ -98,7 +98,7 @@ class ForgetfulFoolMeOnce(Player):
9898
'manipulates_state': False
9999
}
100100

101-
def __init__(self, forget_probability=0.05):
101+
def __init__(self, forget_probability: float=0.05) -> None:
102102
"""
103103
Parameters
104104
----------
@@ -110,7 +110,7 @@ def __init__(self, forget_probability=0.05):
110110
self._initial = C
111111
self.forget_probability = forget_probability
112112

113-
def strategy(self, opponent):
113+
def strategy(self, opponent: Player) -> Action:
114114
r = random.random()
115115
if not opponent.history:
116116
return self._initial
@@ -145,7 +145,7 @@ class FoolMeForever(Player):
145145
}
146146

147147
@staticmethod
148-
def strategy(opponent):
148+
def strategy(opponent: Player) -> Action:
149149
if opponent.defections > 0:
150150
return C
151151
return D

axelrod/strategies/prober.py

Lines changed: 13 additions & 13 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 axelrod.random_ import random_choice
44

@@ -31,7 +31,7 @@ class CollectiveStrategy(Player):
3131
'manipulates_state': False
3232
}
3333

34-
def strategy(self, opponent):
34+
def strategy(self, opponent: Player) -> Action:
3535
turn = len(self.history)
3636
if turn == 0:
3737
return C
@@ -61,7 +61,7 @@ class Prober(Player):
6161
'manipulates_state': False
6262
}
6363

64-
def strategy(self, opponent):
64+
def strategy(self, opponent: Player) -> Action:
6565
turn = len(self.history)
6666
if turn == 0:
6767
return D
@@ -94,7 +94,7 @@ class Prober2(Player):
9494
'manipulates_state': False
9595
}
9696

97-
def strategy(self, opponent):
97+
def strategy(self, opponent: Player) -> Action:
9898
turn = len(self.history)
9999
if turn == 0:
100100
return D
@@ -127,7 +127,7 @@ class Prober3(Player):
127127
'manipulates_state': False
128128
}
129129

130-
def strategy(self, opponent):
130+
def strategy(self, opponent: Player) -> Action:
131131
turn = len(self.history)
132132
if turn == 0:
133133
return D
@@ -165,7 +165,7 @@ class Prober4(Player):
165165
'manipulates_state': False
166166
}
167167

168-
def __init__(self):
168+
def __init__(self) -> None:
169169
super().__init__()
170170
self.init_sequence = [
171171
C, C, D, C, D, D, D, C, C, D, C, D, C, C, D, C, D, D, C, D
@@ -174,7 +174,7 @@ def __init__(self):
174174
self.unjust_Ds = 0
175175
self.turned_defector = False
176176

177-
def strategy(self, opponent):
177+
def strategy(self, opponent: Player) -> Action:
178178
if not self.history:
179179
return self.init_sequence[0]
180180
turn = len(self.history)
@@ -219,7 +219,7 @@ class HardProber(Player):
219219
'manipulates_state': False
220220
}
221221

222-
def strategy(self, opponent):
222+
def strategy(self, opponent: Player) -> Action:
223223
turn = len(self.history)
224224
if turn == 0:
225225
return D
@@ -258,7 +258,7 @@ class NaiveProber(Player):
258258
'manipulates_state': False
259259
}
260260

261-
def __init__(self, p=0.1):
261+
def __init__(self, p: Player=0.1) -> None:
262262
"""
263263
Parameters
264264
----------
@@ -270,7 +270,7 @@ def __init__(self, p=0.1):
270270
if (self.p == 0) or (self.p == 1):
271271
self.classifier['stochastic'] = False
272272

273-
def strategy(self, opponent):
273+
def strategy(self, opponent: Player) -> Action:
274274
# First move
275275
if len(self.history) == 0:
276276
return C
@@ -281,7 +281,7 @@ def strategy(self, opponent):
281281
choice = random_choice(1 - self.p)
282282
return choice
283283

284-
def __repr__(self):
284+
def __repr__(self) -> str:
285285
return "%s: %s" % (self.name, round(self.p, 2))
286286

287287

@@ -314,11 +314,11 @@ class RemorsefulProber(NaiveProber):
314314
'manipulates_state': False
315315
}
316316

317-
def __init__(self, p=0.1):
317+
def __init__(self, p: float=0.1) -> None:
318318
super().__init__(p)
319319
self.probing = False
320320

321-
def strategy(self, opponent):
321+
def strategy(self, opponent: Player) -> Action:
322322
# First move
323323
if len(self.history) == 0:
324324
return C

axelrod/strategies/punisher.py

Lines changed: 7 additions & 7 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

44
C, D = Actions.C, Actions.D
@@ -27,7 +27,7 @@ class Punisher(Player):
2727
'manipulates_state': False
2828
}
2929

30-
def __init__(self):
30+
def __init__(self) -> None:
3131
"""
3232
Initialised the player
3333
"""
@@ -36,7 +36,7 @@ def __init__(self):
3636
self.grudged = False
3737
self.grudge_memory = 1
3838

39-
def strategy(self, opponent):
39+
def strategy(self, opponent: Player) -> Action:
4040
"""
4141
Begins by playing C, then plays D for an amount of rounds proportional
4242
to the opponents historical '%' of playing D if the opponent ever
@@ -89,14 +89,14 @@ class InversePunisher(Player):
8989
'manipulates_state': False
9090
}
9191

92-
def __init__(self):
92+
def __init__(self) -> None:
9393
super().__init__()
94-
self.history = []
94+
self.history = [] # type: List[Action]
9595
self.mem_length = 1
9696
self.grudged = False
9797
self.grudge_memory = 1
9898

99-
def strategy(self, opponent):
99+
def strategy(self, opponent: Player) -> Action:
100100
"""
101101
Begins by playing C, then plays D for an amount of rounds proportional
102102
to the opponents historical '%' of playing C if the opponent ever plays
@@ -147,7 +147,7 @@ class LevelPunisher(Player):
147147
'manipulates_state': False
148148
}
149149

150-
def strategy(self, opponent):
150+
def strategy(self, opponent: Player) -> Action:
151151
if len(opponent.history) < 10:
152152
return C
153153
elif (len(opponent.history) - opponent.cooperations) / len(opponent.history) > 0.2:

axelrod/strategies/rand.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from axelrod.player import Player
2+
from axelrod.actions import Action
23
from axelrod.random_ import random_choice
34

45

@@ -22,7 +23,7 @@ class Random(Player):
2223
'manipulates_state': False
2324
}
2425

25-
def __init__(self, p=0.5):
26+
def __init__(self, p: float=0.5) -> None:
2627
"""
2728
Parameters
2829
----------
@@ -39,8 +40,8 @@ def __init__(self, p=0.5):
3940
if p in [0, 1]:
4041
self.classifier['stochastic'] = False
4142

42-
def strategy(self, opponent):
43+
def strategy(self, opponent: Player) -> Action:
4344
return random_choice(self.p)
4445

45-
def __repr__(self):
46+
def __repr__(self) -> str:
4647
return "%s: %s" % (self.name, round(self.p, 2))

type_tests.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,20 @@ mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/cycler.py
2121
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/darwin.py
2222
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/defector.py
2323
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/forgiver.py
24+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/geller.py
2425
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/gradualkiller.py
2526
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/grudger.py
2627
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/grumpy.py
2728
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/handshake.py
29+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/hunter.py
2830
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/inverse.py
2931
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/mathematicalconstants.py
32+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/memorytwo.py
3033
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/mindcontrol.py
3134
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/mindreader.py
3235
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/mutual.py
3336
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/negation.py
34-
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/hunter.py
35-
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/geller.py
36-
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/memorytwo.py
37+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/oncebitten.py
38+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/prober.py
39+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/punisher.py
40+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/rand.py

0 commit comments

Comments
 (0)