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 handshake, inverse, mathematicalconstants #858

Merged
merged 8 commits into from
Feb 10, 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
8 changes: 5 additions & 3 deletions axelrod/strategies/handshake.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from axelrod.actions import Actions
from axelrod.actions import Actions, Action
from axelrod.player import Player

from typing import List

C, D = Actions.C, Actions.D


Expand All @@ -24,13 +26,13 @@ class Handshake(Player):
'manipulates_state': False
}

def __init__(self, initial_plays=None):
def __init__(self, initial_plays: List[Action]=None) -> None:
super().__init__()
if not initial_plays:
initial_plays = [C, D]
self.initial_plays = initial_plays

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
# Begin by playing the sequence C, D
index = len(self.history)
if index < len(self.initial_plays):
Expand Down
4 changes: 2 additions & 2 deletions axelrod/strategies/inverse.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 @@ -21,7 +21,7 @@ class Inverse(Player):
}

@staticmethod
def strategy(opponent):
def strategy(opponent: Player) -> Action:
"""Looks at opponent history to see if they have defected.

If so, player defection is inversely proportional to when this occurred.
Expand Down
12 changes: 7 additions & 5 deletions axelrod/strategies/mathematicalconstants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import math
from axelrod.actions import Actions

from axelrod.actions import Actions, Action
from axelrod.player import Player

C, D = Actions.C, Actions.D

class CotoDeRatio(Player):
"""The player will always aim to bring the ratio of co-operations to
Expand All @@ -20,16 +22,16 @@ class CotoDeRatio(Player):
def strategy(self, opponent):
# Initially cooperate
if len(opponent.history) == 0:
return Actions.C
return C
# Avoid initial division by zero
if not opponent.defections:
return Actions.D
return D
# Otherwise compare ratio to golden mean
cooperations = opponent.cooperations + self.cooperations
defections = opponent.defections + self.defections
if cooperations / defections > self.ratio:
return Actions.D
return Actions.C
return D
return C


class Golden(CotoDeRatio):
Expand Down
3 changes: 3 additions & 0 deletions type_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/forgiver.
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
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