Skip to content

Commit c8cd1fc

Browse files
authored
Merge pull request #863 from janga1997/typeHint7
Add type hints to mindcontrol, mindreader, mutual, negation
2 parents ce40b93 + 56bd294 commit c8cd1fc

5 files changed

Lines changed: 19 additions & 16 deletions

File tree

axelrod/strategies/mindcontrol.py

Lines changed: 5 additions & 5 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
@@ -19,7 +19,7 @@ class MindController(Player):
1919
}
2020

2121
@staticmethod
22-
def strategy(opponent):
22+
def strategy(opponent: Player) -> Action:
2323
"""
2424
Alters the opponents strategy method to be a lambda function which
2525
always returns C. This player will then always return D to take
@@ -47,14 +47,14 @@ class MindWarper(Player):
4747
'manipulates_state': False
4848
}
4949

50-
def __setattr__(self, name, val):
50+
def __setattr__(self, name: str, val: str):
5151
if name == 'strategy':
5252
pass
5353
else:
5454
self.__dict__[name] = val
5555

5656
@staticmethod
57-
def strategy(opponent):
57+
def strategy(opponent: Player) -> Action:
5858
opponent.strategy = lambda opponent: C
5959
return D
6060

@@ -77,6 +77,6 @@ class MindBender(MindWarper):
7777
}
7878

7979
@staticmethod
80-
def strategy(opponent):
80+
def strategy(opponent: Player) -> Action:
8181
opponent.__dict__['strategy'] = lambda opponent: C
8282
return D

axelrod/strategies/mindreader.py

Lines changed: 4 additions & 4 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._strategy_utils import look_ahead
66

@@ -22,7 +22,7 @@ class MindReader(Player):
2222
'manipulates_state': False
2323
}
2424

25-
def strategy(self, opponent):
25+
def strategy(self, opponent: Player) -> Action:
2626
"""Pretends to play the opponent a number of times before each match.
2727
The primary purpose is to look far enough ahead to see if a defect will
2828
be punished by the opponent.
@@ -61,7 +61,7 @@ class ProtectedMindReader(MindReader):
6161
'manipulates_state': False
6262
}
6363

64-
def __setattr__(self, name, val):
64+
def __setattr__(self, name: str, val: str):
6565
"""Stops any other strategy altering the methods of this class """
6666

6767
if name == 'strategy':
@@ -85,7 +85,7 @@ class MirrorMindReader(ProtectedMindReader):
8585
'manipulates_state': False
8686
}
8787

88-
def strategy(self, opponent):
88+
def strategy(self, opponent: Player) -> Action:
8989
"""Will read the mind of the opponent and play the opponent's strategy.
9090
9191
Also avoid infinite recursion when called by itself or another mind

axelrod/strategies/mutual.py

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

@@ -23,7 +23,7 @@ class Desperate(Player):
2323
'manipulates_state': False
2424
}
2525

26-
def strategy(self, opponent):
26+
def strategy(self, opponent: Player) -> Action:
2727
if not opponent.history:
2828
return random_choice()
2929
if self.history[-1] == D and opponent.history[-1] == D:
@@ -49,7 +49,7 @@ class Hopeless(Player):
4949
'manipulates_state': False
5050
}
5151

52-
def strategy(self, opponent):
52+
def strategy(self, opponent: Player) -> Action:
5353
if not opponent.history:
5454
return random_choice()
5555
if self.history[-1] == C and opponent.history[-1] == C:
@@ -75,7 +75,7 @@ class Willing(Player):
7575
'manipulates_state': False
7676
}
7777

78-
def strategy(self, opponent):
78+
def strategy(self, opponent: Player) -> Action:
7979
if not opponent.history:
8080
return random_choice()
8181
if self.history[-1] == D and opponent.history[-1] == D:

axelrod/strategies/negation.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from axelrod.actions import Actions, flip_action
1+
from axelrod.actions import Actions, flip_action, Action
22
from axelrod.player import Player
33
from axelrod.random_ import random_choice
44

@@ -26,10 +26,9 @@ class Negation(Player):
2626
'manipulates_state': False
2727
}
2828

29-
def strategy(self, opponent):
29+
def strategy(self, opponent: Player) -> Action:
3030
# Random first move
3131
if not self.history:
3232
return random_choice()
3333
# Act opposite of opponent otherwise
3434
return flip_action(opponent.history[-1])
35-

type_tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ 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/mindcontrol.py
31+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/mindreader.py
32+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/mutual.py
33+
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/negation.py
3034
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/hunter.py
3135
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/geller.py
3236
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/memorytwo.py

0 commit comments

Comments
 (0)