Skip to content

Add type hints to gradualkiller, grudger, grumpy #856

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

Merged
merged 4 commits into from
Feb 9, 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
3 changes: 2 additions & 1 deletion axelrod/strategies/gradualkiller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from axelrod import Actions, Player
from axelrod.strategy_transformers import InitialTransformer
from axelrod.actions import Action

C, D = Actions.C, Actions.D

Expand Down Expand Up @@ -30,7 +31,7 @@ class GradualKiller(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
if opponent.history[5:7] == [D, D]:
return D
return C
19 changes: 10 additions & 9 deletions axelrod/strategies/grudger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from axelrod import Actions, Player
from axelrod.actions import Action

C, D = Actions.C, Actions.D

Expand Down Expand Up @@ -29,7 +30,7 @@ class Grudger(Player):
}

@staticmethod
def strategy(opponent):
def strategy(opponent: Player) -> Action:
"""Begins by playing C, then plays D for the remaining rounds if the
opponent ever plays D."""
if opponent.defections:
Expand All @@ -52,14 +53,14 @@ class ForgetfulGrudger(Player):
'manipulates_state': False
}

def __init__(self):
def __init__(self) -> None:
"""Initialised the player."""
super().__init__()
self.mem_length = 10
self.grudged = False
self.grudge_memory = 0

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
"""Begins by playing C, then plays D for mem_length rounds if the
opponent ever plays D."""
if self.grudge_memory >= self.mem_length:
Expand Down Expand Up @@ -97,7 +98,7 @@ class OppositeGrudger(Player):
}

@staticmethod
def strategy(opponent):
def strategy(opponent: Player) -> Action:
"""Begins by playing D, then plays C for the remaining rounds if the
opponent ever plays C."""
if opponent.cooperations:
Expand All @@ -120,7 +121,7 @@ class Aggravater(Player):
}

@staticmethod
def strategy(opponent):
def strategy(opponent: Player) -> Action:
if len(opponent.history) < 3:
return D
elif opponent.defections:
Expand Down Expand Up @@ -151,13 +152,13 @@ class SoftGrudger(Player):
'manipulates_state': False
}

def __init__(self):
def __init__(self) -> None:
"""Initialised the player."""
super().__init__()
self.grudged = False
self.grudge_memory = 0

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
"""Begins by playing C, then plays D, D, D, D, C, C against a defection
"""
if self.grudged:
Expand Down Expand Up @@ -201,7 +202,7 @@ class GrudgerAlternator(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
"""Begins by playing C, then plays Alternator for the remaining rounds
if the opponent ever plays D."""
if opponent.defections:
Expand Down Expand Up @@ -232,7 +233,7 @@ class EasyGo(Player):
}

@staticmethod
def strategy(opponent):
def strategy(opponent: Player) -> Action:
"""Begins by playing D, then plays C for the remaining rounds if the
opponent ever plays D."""
if opponent.defections:
Expand Down
7 changes: 4 additions & 3 deletions axelrod/strategies/grumpy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from axelrod import Actions, Player
from axelrod.actions import Action

C, D = Actions.C, Actions.D

Expand All @@ -19,8 +20,8 @@ class Grumpy(Player):
'manipulates_state': False
}

def __init__(self, starting_state='Nice', grumpy_threshold=10,
nice_threshold=-10):
def __init__(self, starting_state: str ='Nice', grumpy_threshold: int =10,
nice_threshold: int =-10) -> None:
"""
Parameters
----------
Expand All @@ -38,7 +39,7 @@ def __init__(self, starting_state='Nice', grumpy_threshold=10,
self.grumpy_threshold = grumpy_threshold
self.nice_threshold = nice_threshold

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

Expand Down
3 changes: 3 additions & 0 deletions type_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/cooperato
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/axelrod_second.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/backstabber.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/defector.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/gradualkiller.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/grudger.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/grumpy.py