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 mindcontrol, mindreader, mutual, negation #863

Merged
merged 7 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
10 changes: 5 additions & 5 deletions axelrod/strategies/mindcontrol.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

C, D = Actions.C, Actions.D
Expand All @@ -19,7 +19,7 @@ class MindController(Player):
}

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

def __setattr__(self, name, val):
def __setattr__(self, name: str, val: str):
if name == 'strategy':
pass
else:
self.__dict__[name] = val

@staticmethod
def strategy(opponent):
def strategy(opponent: Player) -> Action:
opponent.strategy = lambda opponent: C
return D

Expand All @@ -77,6 +77,6 @@ class MindBender(MindWarper):
}

@staticmethod
def strategy(opponent):
def strategy(opponent: Player) -> Action:
opponent.__dict__['strategy'] = lambda opponent: C
return D
8 changes: 4 additions & 4 deletions axelrod/strategies/mindreader.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._strategy_utils import look_ahead

Expand All @@ -22,7 +22,7 @@ class MindReader(Player):
'manipulates_state': False
}

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

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

if name == 'strategy':
Expand All @@ -85,7 +85,7 @@ class MirrorMindReader(ProtectedMindReader):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
"""Will read the mind of the opponent and play the opponent's strategy.

Also avoid infinite recursion when called by itself or another mind
Expand Down
8 changes: 4 additions & 4 deletions axelrod/strategies/mutual.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 axelrod.random_ import random_choice

Expand All @@ -23,7 +23,7 @@ class Desperate(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
if not opponent.history:
return random_choice()
if self.history[-1] == D and opponent.history[-1] == D:
Expand All @@ -49,7 +49,7 @@ class Hopeless(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
if not opponent.history:
return random_choice()
if self.history[-1] == C and opponent.history[-1] == C:
Expand All @@ -75,7 +75,7 @@ class Willing(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
if not opponent.history:
return random_choice()
if self.history[-1] == D and opponent.history[-1] == D:
Expand Down
5 changes: 2 additions & 3 deletions axelrod/strategies/negation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from axelrod.actions import Actions, flip_action
from axelrod.actions import Actions, flip_action, Action
from axelrod.player import Player
from axelrod.random_ import random_choice

Expand Down Expand Up @@ -26,10 +26,9 @@ class Negation(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
# Random first move
if not self.history:
return random_choice()
# Act opposite of opponent otherwise
return flip_action(opponent.history[-1])

4 changes: 4 additions & 0 deletions type_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ 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/mindcontrol.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/mindreader.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/mutual.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/negation.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