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 cooperator, cycler, darwin #853

Merged
merged 1 commit into from
Feb 5, 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
5 changes: 3 additions & 2 deletions axelrod/strategies/cooperator.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 @@ -18,7 +19,7 @@ class Cooperator(Player):
}

@staticmethod
def strategy(opponent):
def strategy(opponent: Player) -> Action:
return C


Expand All @@ -37,7 +38,7 @@ class TrickyCooperator(Player):
}

@staticmethod
def strategy(opponent):
def strategy(opponent: Player) -> Action:
"""Almost always cooperates, but will try to trick the opponent by defecting.

Defect once in a while in order to get a better payout, when the opponent
Expand Down
21 changes: 11 additions & 10 deletions axelrod/strategies/cycler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from axelrod import Actions, Player, init_args
from axelrod.actions import Action

import copy

Expand All @@ -20,12 +21,12 @@ class AntiCycler(Player):
'manipulates_state': False
}

def __init__(self):
def __init__(self) -> None:
super().__init__()
self.cycle_length = 1
self.cycle_counter = 0

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
if self.cycle_counter < self.cycle_length:
self.cycle_counter += 1
return Actions.C
Expand Down Expand Up @@ -55,7 +56,7 @@ class Cycler(Player):
}

@init_args
def __init__(self, cycle="CCD"):
def __init__(self, cycle="CCD") -> None:
"""This strategy will repeat the parameter `cycle` endlessly,
e.g. C C D C C D C C D ...

Expand All @@ -71,7 +72,7 @@ def __init__(self, cycle="CCD"):
self.name = "Cycler {}".format(cycle)
self.classifier['memory_depth'] = len(cycle) - 1

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
curent_round = len(self.history)
index = curent_round % len(self.cycle)
return self.cycle[index]
Expand All @@ -84,7 +85,7 @@ class CyclerDC(Cycler):
classifier['memory_depth'] = 1

@init_args
def __init__(self, cycle="DC"):
def __init__(self, cycle="DC") -> None:
super().__init__(cycle=cycle)


Expand All @@ -95,7 +96,7 @@ class CyclerCCD(Cycler):
classifier['memory_depth'] = 2

@init_args
def __init__(self, cycle="CCD"):
def __init__(self, cycle="CCD") -> None:
super().__init__(cycle=cycle)


Expand All @@ -106,7 +107,7 @@ class CyclerDDC(Cycler):
classifier['memory_depth'] = 2

@init_args
def __init__(self, cycle="DDC"):
def __init__(self, cycle="DDC") -> None:
super().__init__(cycle=cycle)


Expand All @@ -117,7 +118,7 @@ class CyclerCCCD(Cycler):
classifier['memory_depth'] = 3

@init_args
def __init__(self, cycle="CCCD"):
def __init__(self, cycle="CCCD") -> None:
super().__init__(cycle=cycle)


Expand All @@ -128,7 +129,7 @@ class CyclerCCCCCD(Cycler):
classifier['memory_depth'] = 5

@init_args
def __init__(self, cycle="CCCCCD"):
def __init__(self, cycle="CCCCCD") -> None:
super().__init__(cycle=cycle)


Expand All @@ -139,5 +140,5 @@ class CyclerCCCDCD(Cycler):
classifier['memory_depth'] = 5

@init_args
def __init__(self, cycle="CCCDCD"):
def __init__(self, cycle="CCCDCD") -> None:
super().__init__(cycle=cycle)
7 changes: 5 additions & 2 deletions axelrod/strategies/darwin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import inspect
from axelrod import Actions, Player
from axelrod.actions import Action

from typing import List

C, D = Actions.C, Actions.D

Expand Down Expand Up @@ -35,14 +38,14 @@ class Darwin(Player):
genome = [C]
valid_callers = ["play"] # What functions may invoke our strategy.

def __init__(self):
def __init__(self) -> None:
super().__init__()
self.response = Darwin.genome[0]

def receive_match_attributes(self):
self.outcomes = self.match_attributes["game"].scores

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
# Frustrate psychics and ensure that simulated rounds
# do not influence genome.
if inspect.stack()[1][3] not in Darwin.valid_callers:
Expand Down
4 changes: 3 additions & 1 deletion type_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/appeaser.
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/averagecopier.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/forgiver.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/better_and_better.py

mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/darwin.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/cycler.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/cooperator.py