Skip to content

Commit ba23d34

Browse files
janga1997marcharper
authored andcommitted
Add type hints to gradualkiller, grudger, grumpy (#856)
* Add type hints to gradualkiller * Add type hints to grudger * Add type hints to grumpy * Add files to type_tests
1 parent 88a80a2 commit ba23d34

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

axelrod/strategies/gradualkiller.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from axelrod import Actions, Player
22
from axelrod.strategy_transformers import InitialTransformer
3+
from axelrod.actions import Action
34

45
C, D = Actions.C, Actions.D
56

@@ -30,7 +31,7 @@ class GradualKiller(Player):
3031
'manipulates_state': False
3132
}
3233

33-
def strategy(self, opponent):
34+
def strategy(self, opponent: Player) -> Action:
3435
if opponent.history[5:7] == [D, D]:
3536
return D
3637
return C

axelrod/strategies/grudger.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from axelrod import Actions, Player
2+
from axelrod.actions import Action
23

34
C, D = Actions.C, Actions.D
45

@@ -29,7 +30,7 @@ class Grudger(Player):
2930
}
3031

3132
@staticmethod
32-
def strategy(opponent):
33+
def strategy(opponent: Player) -> Action:
3334
"""Begins by playing C, then plays D for the remaining rounds if the
3435
opponent ever plays D."""
3536
if opponent.defections:
@@ -52,14 +53,14 @@ class ForgetfulGrudger(Player):
5253
'manipulates_state': False
5354
}
5455

55-
def __init__(self):
56+
def __init__(self) -> None:
5657
"""Initialised the player."""
5758
super().__init__()
5859
self.mem_length = 10
5960
self.grudged = False
6061
self.grudge_memory = 0
6162

62-
def strategy(self, opponent):
63+
def strategy(self, opponent: Player) -> Action:
6364
"""Begins by playing C, then plays D for mem_length rounds if the
6465
opponent ever plays D."""
6566
if self.grudge_memory >= self.mem_length:
@@ -97,7 +98,7 @@ class OppositeGrudger(Player):
9798
}
9899

99100
@staticmethod
100-
def strategy(opponent):
101+
def strategy(opponent: Player) -> Action:
101102
"""Begins by playing D, then plays C for the remaining rounds if the
102103
opponent ever plays C."""
103104
if opponent.cooperations:
@@ -120,7 +121,7 @@ class Aggravater(Player):
120121
}
121122

122123
@staticmethod
123-
def strategy(opponent):
124+
def strategy(opponent: Player) -> Action:
124125
if len(opponent.history) < 3:
125126
return D
126127
elif opponent.defections:
@@ -151,13 +152,13 @@ class SoftGrudger(Player):
151152
'manipulates_state': False
152153
}
153154

154-
def __init__(self):
155+
def __init__(self) -> None:
155156
"""Initialised the player."""
156157
super().__init__()
157158
self.grudged = False
158159
self.grudge_memory = 0
159160

160-
def strategy(self, opponent):
161+
def strategy(self, opponent: Player) -> Action:
161162
"""Begins by playing C, then plays D, D, D, D, C, C against a defection
162163
"""
163164
if self.grudged:
@@ -201,7 +202,7 @@ class GrudgerAlternator(Player):
201202
'manipulates_state': False
202203
}
203204

204-
def strategy(self, opponent):
205+
def strategy(self, opponent: Player) -> Action:
205206
"""Begins by playing C, then plays Alternator for the remaining rounds
206207
if the opponent ever plays D."""
207208
if opponent.defections:
@@ -232,7 +233,7 @@ class EasyGo(Player):
232233
}
233234

234235
@staticmethod
235-
def strategy(opponent):
236+
def strategy(opponent: Player) -> Action:
236237
"""Begins by playing D, then plays C for the remaining rounds if the
237238
opponent ever plays D."""
238239
if opponent.defections:

axelrod/strategies/grumpy.py

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

34
C, D = Actions.C, Actions.D
45

@@ -19,8 +20,8 @@ class Grumpy(Player):
1920
'manipulates_state': False
2021
}
2122

22-
def __init__(self, starting_state='Nice', grumpy_threshold=10,
23-
nice_threshold=-10):
23+
def __init__(self, starting_state: str ='Nice', grumpy_threshold: int =10,
24+
nice_threshold: int =-10) -> None:
2425
"""
2526
Parameters
2627
----------
@@ -38,7 +39,7 @@ def __init__(self, starting_state='Nice', grumpy_threshold=10,
3839
self.grumpy_threshold = grumpy_threshold
3940
self.nice_threshold = nice_threshold
4041

41-
def strategy(self, opponent):
42+
def strategy(self, opponent: Player) -> Action:
4243
"""A player that gets grumpier the more the opposition defects,
4344
and nicer the more they cooperate.
4445

type_tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/cooperato
1919
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/axelrod_second.py
2020
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/backstabber.py
2121
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/defector.py
22+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/gradualkiller.py
23+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/grudger.py
24+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/grumpy.py

0 commit comments

Comments
 (0)