Skip to content

Commit

Permalink
Merge pull request #1177 from Axelrod-Python/fix-typing-for-mypy-2.1
Browse files Browse the repository at this point in the history
Fix type hints for mypy 2.1
  • Loading branch information
drvinceknight authored May 14, 2018
2 parents 8ad448a + beee0f6 commit 2bc5247
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 20 deletions.
8 changes: 5 additions & 3 deletions axelrod/fingerprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def construct_tournament_elements(self, step: float,

def fingerprint(
self, turns: int = 50, repetitions: int = 10, step: float = 0.01,
processes: int=None, filename: str = None,
processes: int = None, filename: str = None,
progress_bar: bool = True
) -> dict:
"""Build and play the spatial tournament.
Expand Down Expand Up @@ -305,7 +305,7 @@ def fingerprint(

temp_file_descriptor = None
if filename is None:
temp_file_descriptor, filename = mkstemp()
temp_file_descriptor, filename = mkstemp() # type: ignore

edges, tourn_players = self.construct_tournament_elements(
step, progress_bar=progress_bar)
Expand All @@ -323,6 +323,7 @@ def fingerprint(
filename, progress_bar=progress_bar)

if temp_file_descriptor is not None:
assert filename is not None
os.close(temp_file_descriptor)
os.remove(filename)

Expand Down Expand Up @@ -443,7 +444,7 @@ def fingerprint(self, turns: int = 50, repetitions: int = 1000,

temp_file_descriptor = None
if filename is None:
temp_file_descriptor, filename = mkstemp()
temp_file_descriptor, filename = mkstemp() # type: ignore

edges = [(0, k + 1) for k in range(len(self.opponents))]
tournament = axl.Tournament(players=players,
Expand All @@ -455,6 +456,7 @@ def fingerprint(self, turns: int = 50, repetitions: int = 1000,
self.data = self.analyse_cooperation_ratio(filename)

if temp_file_descriptor is not None:
assert filename is not None
os.close(temp_file_descriptor)
os.remove(filename)

Expand Down
8 changes: 4 additions & 4 deletions axelrod/moran.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .match import Match
from .random_ import randrange

from typing import List, Tuple, Set
from typing import List, Tuple, Set, Optional


def fitness_proportionate_selection(scores: List) -> int:
Expand Down Expand Up @@ -102,7 +102,7 @@ def __init__(self, players: List[Player], turns: int = DEFAULT_TURNS,
self.populations = [] # type: List
self.set_players()
self.score_history = [] # type: List
self.winning_strategy_name = None # type: str
self.winning_strategy_name = None # type: Optional[str]
self.mutation_rate = mutation_rate
assert (mutation_rate >= 0) and (mutation_rate <= 1)
assert (noise >= 0) and (noise <= 1)
Expand Down Expand Up @@ -184,7 +184,7 @@ def death(self, index: int = None) -> int:
index:
The index of the player to be removed
"""
if self.mode == "db":
if index is None:
# Select a player to be replaced globally
i = randrange(0, len(self.players))
# Record internally for use in _matchup_indices
Expand All @@ -207,7 +207,7 @@ def birth(self, index: int = None) -> int:
"""
# Compute necessary fitnesses.
scores = self.score_all()
if self.mode == "db":
if index is not None:
# Death has already occurred, so remove the dead player from the
# possible choices
scores.pop(index)
Expand Down
6 changes: 4 additions & 2 deletions axelrod/strategies/apavlov.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from axelrod.action import Action
from axelrod.player import Player

from typing import Optional

C, D = Action.C, Action.D


Expand Down Expand Up @@ -29,7 +31,7 @@ class APavlov2006(Player):

def __init__(self) -> None:
super().__init__()
self.opponent_class = None # type: str
self.opponent_class = None # type: Optional[str]

def strategy(self, opponent: Player) -> Action:
# TFT for six rounds
Expand Down Expand Up @@ -93,7 +95,7 @@ class APavlov2011(Player):

def __init__(self) -> None:
super().__init__()
self.opponent_class = None # type: str
self.opponent_class = None # type: Optional[str]

def strategy(self, opponent: Player) -> Action:
# TFT for six rounds
Expand Down
5 changes: 4 additions & 1 deletion axelrod/strategies/darwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from axelrod.action import Action
from axelrod.player import Player

from typing import Optional

C, D = Action.C, Action.D


Expand Down Expand Up @@ -48,7 +50,7 @@ class Darwin(Player):
valid_callers = ["play"] # What functions may invoke our strategy.

def __init__(self) -> None:
self.outcomes = None # type: dict
self.outcomes = None # type: Optional[dict]
self.response = Darwin.genome[0]
super().__init__()

Expand All @@ -64,6 +66,7 @@ def strategy(self, opponent: Player) -> Action:
trial = len(self.history)

if trial > 0:
assert self.outcomes is not None
outcome = self.outcomes[(self.history[-1], opponent.history[-1])]
self.mutate(outcome, trial)
# Update genome with selected response
Expand Down
4 changes: 2 additions & 2 deletions axelrod/strategies/hunter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from axelrod.player import Player
from axelrod._strategy_utils import detect_cycle

from typing import List, Tuple
from typing import List, Tuple, Optional

C, D = Action.C, Action.D

Expand Down Expand Up @@ -120,7 +120,7 @@ class CycleHunter(Player):

def __init__(self) -> None:
super().__init__()
self.cycle = None # type: Tuple[Action]
self.cycle = None # type: Optional[Tuple[Action]]

def strategy(self, opponent: Player) -> Action:
if self.cycle:
Expand Down
1 change: 1 addition & 0 deletions axelrod/strategies/memoryone.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def receive_match_attributes(self):
self.set_four_vector(four_vector)

def __repr__(self) -> str:
assert self.p is not None
return "%s: %s" % (self.name, round(self.p, 2))


Expand Down
10 changes: 5 additions & 5 deletions axelrod/tests/unit/test_moran.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ def test_death_in_db(self):
players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat()
mp = MoranProcess(players, mutation_rate=0.5, mode="db")
axelrod.seed(1)
self.assertEqual(mp.death(0), 0)
self.assertEqual(mp.death(), 0)
self.assertEqual(mp.dead, 0)
axelrod.seed(5)
self.assertEqual(mp.death(0), 1)
self.assertEqual(mp.death(), 1)
self.assertEqual(mp.dead, 1)
axelrod.seed(2)
self.assertEqual(mp.death(0), 2)
self.assertEqual(mp.death(), 2)
self.assertEqual(mp.dead, 2)

def test_death_in_bd(self):
Expand All @@ -102,14 +102,14 @@ def test_birth_in_db(self):
players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat()
mp = MoranProcess(players, mode="db")
axelrod.seed(1)
self.assertEqual(mp.death(0), 0)
self.assertEqual(mp.death(), 0)
self.assertEqual(mp.birth(0), 2)

def test_birth_in_bd(self):
players = axelrod.Cooperator(), axelrod.Defector(), axelrod.TitForTat()
mp = MoranProcess(players, mode="bd")
axelrod.seed(1)
self.assertEqual(mp.birth(0), 0)
self.assertEqual(mp.birth(), 0)

def test_fixation_check(self):
players = axelrod.Cooperator(), axelrod.Cooperator()
Expand Down
7 changes: 4 additions & 3 deletions axelrod/tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

C, D = Action.C, Action.D

from typing import List, Tuple
from typing import List, Tuple, Optional


class Tournament(object):
Expand Down Expand Up @@ -83,8 +83,8 @@ def __init__(self, players: List[Player],
self._logger = logging.getLogger(__name__)

self.use_progress_bar = True
self.filename = None # type: str
self._temp_file_descriptor = None # type: int
self.filename = None # type: Optional[str]
self._temp_file_descriptor = None # type: Optional[int]

def setup_output(self, filename=None):
"""assign/create `filename` to `self`. If file should be deleted once
Expand Down Expand Up @@ -142,6 +142,7 @@ def play(self, build_results: bool = True, filename: str = None,
processes=processes,
progress_bar=progress_bar)
if self._temp_file_descriptor is not None:
assert self.filename is not None
os.close(self._temp_file_descriptor)
os.remove(self.filename)

Expand Down

0 comments on commit 2bc5247

Please sign in to comment.