Skip to content

Commit 4f310ca

Browse files
committed
More typing stuff for mypy to pass
1 parent 0af5524 commit 4f310ca

16 files changed

+73
-72
lines changed

axelrod/deterministic_cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import pickle
1616
from collections import UserDict
17-
from typing import List, Tuple
17+
from typing import List, Optional, Tuple
1818

1919
from axelrod import Classifiers
2020

@@ -104,7 +104,7 @@ class DeterministicCache(UserDict):
104104
methods to save/load the cache to/from a file.
105105
"""
106106

107-
def __init__(self, file_name: str = None) -> None:
107+
def __init__(self, file_name: Optional[str] = None) -> None:
108108
"""Initialize a new cache.
109109
110110
Parameters

axelrod/ecosystem.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"""
1313

1414
import random
15-
from typing import Callable, List
15+
from typing import Callable, List, Optional
1616

1717
from axelrod.result_set import ResultSet
1818

@@ -29,8 +29,8 @@ class Ecosystem(object):
2929
def __init__(
3030
self,
3131
results: ResultSet,
32-
fitness: Callable[[float], float] = None,
33-
population: List[int] = None,
32+
fitness: Optional[Callable[[float], float]] = None,
33+
population: Optional[List[int]] = None,
3434
) -> None:
3535
"""Create a new ecosystem.
3636

axelrod/fingerprint.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from collections import namedtuple
33
from tempfile import mkstemp
4-
from typing import Any, List, Union
4+
from typing import Any, List, Optional, Union
55

66
import dask.dataframe as dd
77
import matplotlib.pyplot as plt
@@ -280,10 +280,10 @@ def fingerprint(
280280
turns: int = 50,
281281
repetitions: int = 10,
282282
step: float = 0.01,
283-
processes: int = None,
284-
filename: str = None,
283+
processes: Optional[int] = None,
284+
filename: Optional[str] = None,
285285
progress_bar: bool = True,
286-
seed: int = None,
286+
seed: Optional[int] = None,
287287
) -> dict:
288288
"""Build and play the spatial tournament.
289289
@@ -358,7 +358,7 @@ def plot(
358358
self,
359359
cmap: str = "seismic",
360360
interpolation: str = "none",
361-
title: str = None,
361+
title: Optional[str] = None,
362362
colorbar: bool = True,
363363
labels: bool = True,
364364
) -> plt.Figure:
@@ -437,11 +437,11 @@ def fingerprint(
437437
self,
438438
turns: int = 50,
439439
repetitions: int = 1000,
440-
noise: float = None,
441-
processes: int = None,
442-
filename: str = None,
440+
noise: Optional[float] = None,
441+
processes: Optional[int] = None,
442+
filename: Optional[str] = None,
443443
progress_bar: bool = True,
444-
seed: int = None,
444+
seed: Optional[int] = None,
445445
) -> np.ndarray:
446446
"""Creates a spatial tournament to run the necessary matches to obtain
447447
fingerprint data.
@@ -556,7 +556,7 @@ def plot(
556556
self,
557557
cmap: str = "viridis",
558558
interpolation: str = "none",
559-
title: str = None,
559+
title: Optional[str] = None,
560560
colorbar: bool = True,
561561
labels: bool = True,
562562
display_names: bool = False,

axelrod/mock_player.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from itertools import cycle
2-
from typing import List
2+
from typing import List, Optional
33

44
from axelrod.action import Action
55
from axelrod.player import Player
@@ -14,7 +14,7 @@ class MockPlayer(Player):
1414

1515
name = "Mock Player"
1616

17-
def __init__(self, actions: List[Action] = None) -> None:
17+
def __init__(self, actions: Optional[List[Action]] = None) -> None:
1818
super().__init__()
1919
if not actions:
2020
actions = []

axelrod/moran.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ def __init__(
1818
self,
1919
players: List[Player],
2020
turns: int = DEFAULT_TURNS,
21-
prob_end: float = None,
21+
prob_end: Optional[float] = None,
2222
noise: float = 0,
2323
game: Game = None,
2424
deterministic_cache: DeterministicCache = None,
2525
mutation_rate: float = 0.0,
2626
mode: str = "bd",
2727
interaction_graph: Graph = None,
2828
reproduction_graph: Graph = None,
29-
fitness_transformation: Callable = None,
29+
fitness_transformation: Optional[Callable] = None,
3030
mutation_method="transition",
3131
stop_on_fixation=True,
3232
seed=None,
@@ -175,7 +175,7 @@ def set_players(self) -> None:
175175
self.populations = [self.population_distribution()]
176176

177177
def fitness_proportionate_selection(
178-
self, scores: List, fitness_transformation: Callable = None
178+
self, scores: List, fitness_transformation: Optional[Callable] = None
179179
) -> int:
180180
"""Randomly selects an individual proportionally to score.
181181
@@ -229,7 +229,7 @@ def mutate(self, index: int) -> Player:
229229
# Just clone the player
230230
return self.players[index].clone()
231231

232-
def death(self, index: int = None) -> int:
232+
def death(self, index: Optional[int] = None) -> int:
233233
"""
234234
Selects the player to be removed.
235235
@@ -258,7 +258,7 @@ def death(self, index: int = None) -> int:
258258
i = self.index[vertex]
259259
return i
260260

261-
def birth(self, index: int = None) -> int:
261+
def birth(self, index: Optional[int] = None) -> int:
262262
"""The birth event.
263263
264264
Parameters
@@ -349,7 +349,6 @@ def _matchup_indices(self) -> Set[Tuple[int, int]]:
349349
# The other calculations are unnecessary
350350
if self.mode == "db":
351351
source = self.index[self.dead]
352-
self.dead = None
353352
sources = sorted(self.interaction_graph.out_vertices(source))
354353
else:
355354
# birth-death is global

axelrod/plot.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pathlib
2-
from typing import List, Union
2+
from typing import List, Optional, Union
33

44
import matplotlib
55
import matplotlib.pyplot as plt
@@ -25,8 +25,8 @@ def _violinplot(
2525
self,
2626
data: dataType,
2727
names: namesType,
28-
title: titleType = None,
29-
ax: matplotlib.axes.SubplotBase = None,
28+
title: Optional[titleType] = None,
29+
ax: Optional[matplotlib.axes.Axes] = None,
3030
) -> matplotlib.figure.Figure:
3131
"""For making violinplots."""
3232

@@ -76,7 +76,7 @@ def _boxplot_xticks_labels(self):
7676
return [str(n) for n in self.result_set.ranked_names]
7777

7878
def boxplot(
79-
self, title: titleType = None, ax: matplotlib.axes.SubplotBase = None
79+
self, title: Optional[titleType] = None, ax: Optional[matplotlib.axes.Axes] = None
8080
) -> matplotlib.figure.Figure:
8181
"""For the specific mean score boxplot."""
8282
data = self._boxplot_dataset
@@ -98,7 +98,7 @@ def _winplot_dataset(self):
9898
return wins, ranked_names
9999

100100
def winplot(
101-
self, title: titleType = None, ax: matplotlib.axes.SubplotBase = None
101+
self, title: Optional[titleType] = None, ax: Optional[matplotlib.axes.Axes] = None
102102
) -> matplotlib.figure.Figure:
103103
"""Plots the distributions for the number of wins for each strategy."""
104104

@@ -126,7 +126,7 @@ def _sdv_plot_dataset(self):
126126
return diffs, ranked_names
127127

128128
def sdvplot(
129-
self, title: titleType = None, ax: matplotlib.axes.SubplotBase = None
129+
self, title: Optional[titleType] = None, ax: Optional[matplotlib.axes.Axes] = None
130130
) -> matplotlib.figure.Figure:
131131
"""Score difference violin plots to visualize the distributions of how
132132
players attain their payoffs."""
@@ -143,7 +143,7 @@ def _lengthplot_dataset(self):
143143
]
144144

145145
def lengthplot(
146-
self, title: titleType = None, ax: matplotlib.axes.SubplotBase = None
146+
self, title: Optional[titleType] = None, ax: Optional[matplotlib.axes.Axes] = None
147147
) -> matplotlib.figure.Figure:
148148
"""For the specific match length boxplot."""
149149
data = self._lengthplot_dataset
@@ -174,8 +174,8 @@ def _payoff_heatmap(
174174
self,
175175
data: dataType,
176176
names: namesType,
177-
title: titleType = None,
178-
ax: matplotlib.axes.SubplotBase = None,
177+
title: Optional[titleType] = None,
178+
ax: Optional[matplotlib.axes.Axes] = None,
179179
cmap: str = "viridis",
180180
) -> matplotlib.figure.Figure:
181181
"""Generic heatmap plot"""
@@ -202,15 +202,15 @@ def _payoff_heatmap(
202202
return figure
203203

204204
def pdplot(
205-
self, title: titleType = None, ax: matplotlib.axes.SubplotBase = None
205+
self, title: Optional[titleType] = None, ax: Optional[matplotlib.axes.Axes] = None
206206
) -> matplotlib.figure.Figure:
207207
"""Payoff difference heatmap to visualize the distributions of how
208208
players attain their payoffs."""
209209
matrix, names = self._pdplot_dataset
210210
return self._payoff_heatmap(matrix, names, title=title, ax=ax)
211211

212212
def payoff(
213-
self, title: titleType = None, ax: matplotlib.axes.SubplotBase = None
213+
self, title: Optional[titleType] = None, ax: Optional[matplotlib.axes.Axes] = None
214214
) -> matplotlib.figure.Figure:
215215
"""Payoff heatmap to visualize the distributions of how
216216
players attain their payoffs."""
@@ -223,9 +223,9 @@ def payoff(
223223
def stackplot(
224224
self,
225225
eco,
226-
title: titleType = None,
226+
title: Optional[titleType] = None,
227227
logscale: bool = True,
228-
ax: matplotlib.axes.SubplotBase = None,
228+
ax: Optional[matplotlib.axes.Axes] = None,
229229
) -> matplotlib.figure.Figure:
230230

231231
populations = eco.population_sizes

axelrod/strategies/adaptive.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List
1+
from typing import List, Optional
22

33
from axelrod.action import Action
44
from axelrod.player import Player
@@ -26,7 +26,7 @@ class Adaptive(Player):
2626
"manipulates_state": False,
2727
}
2828

29-
def __init__(self, initial_plays: List[Action] = None) -> None:
29+
def __init__(self, initial_plays: Optional[List[Action]] = None) -> None:
3030
super().__init__()
3131
if not initial_plays:
3232
initial_plays = [C] * 6 + [D] * 5

axelrod/strategies/ann.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Tuple
1+
from typing import List, Optional, Tuple
22

33
import numpy as np
44

@@ -198,7 +198,7 @@ class ANN(Player):
198198
}
199199

200200
def __init__(
201-
self, num_features: int, num_hidden: int, weights: List[float] = None
201+
self, num_features: int, num_hidden: int, weights: Optional[List[float]] = None
202202
) -> None:
203203
Player.__init__(self)
204204
self.num_features = num_features
@@ -236,10 +236,10 @@ def __init__(
236236
self,
237237
num_features: int,
238238
num_hidden: int,
239-
weights: List[float] = None,
240-
mutation_probability: float = None,
239+
weights: Optional[List[float]] = None,
240+
mutation_probability: Optional[float] = None,
241241
mutation_distance: int = 5,
242-
seed: int = None,
242+
seed: Optional[int] = None,
243243
) -> None:
244244
EvolvablePlayer.__init__(self, seed=seed)
245245
(

axelrod/strategies/calculator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from axelrod._strategy_utils import detect_cycle
24
from axelrod.action import Action
35
from axelrod.player import Player
@@ -32,7 +34,7 @@ def __init__(self) -> None:
3234
self.joss_instance = Joss()
3335
super().__init__()
3436

35-
def set_seed(self, seed: int = None):
37+
def set_seed(self, seed: Optional[int] = None):
3638
super().set_seed(seed)
3739
self.joss_instance.set_seed(seed)
3840

axelrod/strategies/cycler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import copy
22
import itertools
3-
from typing import List, Tuple
3+
from typing import List, Optional, Tuple
44

55
from axelrod.action import Action, actions_to_str, str_to_actions
66
from axelrod.evolvable_player import (
@@ -109,11 +109,11 @@ class EvolvableCycler(Cycler, EvolvablePlayer):
109109

110110
def __init__(
111111
self,
112-
cycle: str = None,
113-
cycle_length: int = None,
112+
cycle: Optional[str] = None,
113+
cycle_length: Optional[int] = None,
114114
mutation_probability: float = 0.2,
115115
mutation_potency: int = 1,
116-
seed: int = None,
116+
seed: Optional[int] = None,
117117
) -> None:
118118
EvolvablePlayer.__init__(self, seed=seed)
119119
cycle, cycle_length = self._normalize_parameters(cycle, cycle_length)

0 commit comments

Comments
 (0)