Skip to content

Commit 1d0e359

Browse files
MariosZouliasmarcharper
authored andcommitted
More type hints
1 parent 49b5f1a commit 1d0e359

File tree

5 files changed

+30
-26
lines changed

5 files changed

+30
-26
lines changed

axelrod/strategies/finite_state_machines.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from axelrod.actions import Actions
1+
from axelrod.actions import Actions, Action
22
from axelrod.player import Player
33

44
C, D = Actions.C, Actions.D
@@ -26,13 +26,13 @@ def __init__(self, transitions, initial_state):
2626
for (state, opp_action, next_state, next_action) in transitions:
2727
self.state_transitions[(state, opp_action)] = (next_state, next_action)
2828

29-
def move(self, opponent_action):
29+
def move(self, opponent_action: Action) -> Action:
3030
"""Computes the response move and changes state."""
3131
next_state, next_action = self.state_transitions[(self.state, opponent_action)]
3232
self.state = next_state
3333
return next_action
3434

35-
def __eq__(self, other):
35+
def __eq__(self, other: Player) -> bool:
3636
"""Equality of two FSMs"""
3737
check = True
3838
for attr in ["state", "state_transitions"]:
@@ -56,7 +56,7 @@ class FSMPlayer(Player):
5656
}
5757

5858
def __init__(self, transitions=None, initial_state=None,
59-
initial_action=None):
59+
initial_action=None) -> None:
6060
if not transitions:
6161
# Tit For Tat
6262
transitions = [(1, C, 1, C), (1, D, 1, D)]
@@ -68,7 +68,7 @@ def __init__(self, transitions=None, initial_state=None,
6868
self.initial_action = initial_action
6969
self.fsm = SimpleFSM(transitions, initial_state)
7070

71-
def strategy(self, opponent):
71+
def strategy(self, opponent: Player) -> Action:
7272
if len(self.history) == 0:
7373
return self.initial_action
7474
else:
@@ -78,7 +78,7 @@ def strategy(self, opponent):
7878
self.state = self.fsm.state
7979
return action
8080

81-
def reset(self):
81+
def reset(self) -> None:
8282
super().reset()
8383
self.fsm.state = self.initial_state
8484
self.state = self.initial_state
@@ -100,7 +100,7 @@ class Fortress3(FSMPlayer):
100100
'manipulates_state': False
101101
}
102102

103-
def __init__(self):
103+
def __init__(self) -> None:
104104
transitions = (
105105
(1, D, 2, D),
106106
(1, C, 1, D),
@@ -129,7 +129,7 @@ class Fortress4(FSMPlayer):
129129
'manipulates_state': False
130130
}
131131

132-
def __init__(self):
132+
def __init__(self) -> None:
133133
transitions = (
134134
(1, C, 1, D),
135135
(1, D, 2, D),
@@ -158,7 +158,7 @@ class Predator(FSMPlayer):
158158
'manipulates_state': False
159159
}
160160

161-
def __init__(self):
161+
def __init__(self) -> None:
162162
transitions = (
163163
(0, C, 0, D),
164164
(0, D, 1, D),
@@ -201,7 +201,7 @@ class Pun1(FSMPlayer):
201201
'manipulates_state': False
202202
}
203203

204-
def __init__(self):
204+
def __init__(self) -> None:
205205
transitions = (
206206
(1, C, 2, C),
207207
(1, D, 2, C),
@@ -226,7 +226,7 @@ class Raider(FSMPlayer):
226226
'manipulates_state': False
227227
}
228228

229-
def __init__(self):
229+
def __init__(self) -> None:
230230
transitions = (
231231
(0, C, 2, D),
232232
(0, D, 2, D),
@@ -255,7 +255,7 @@ class Ripoff(FSMPlayer):
255255
'manipulates_state': False
256256
}
257257

258-
def __init__(self):
258+
def __init__(self) -> None:
259259
transitions = (
260260
(1, C, 2, C),
261261
(1, D, 3, C),
@@ -282,7 +282,7 @@ class SolutionB1(FSMPlayer):
282282
'manipulates_state': False
283283
}
284284

285-
def __init__(self):
285+
def __init__(self) -> None:
286286
transitions = (
287287
(1, C, 2, D),
288288
(1, D, 1, D),
@@ -309,7 +309,7 @@ class SolutionB5(FSMPlayer):
309309
'manipulates_state': False
310310
}
311311

312-
def __init__(self):
312+
def __init__(self) -> None:
313313
transitions = (
314314
(1, C, 2, C),
315315
(1, D, 6, D),
@@ -342,7 +342,7 @@ class Thumper(FSMPlayer):
342342
'manipulates_state': False
343343
}
344344

345-
def __init__(self):
345+
def __init__(self) -> None:
346346
transitions = (
347347
(1, C, 1, C),
348348
(1, D, 2, D),
@@ -373,7 +373,7 @@ class EvolvedFSM4(FSMPlayer):
373373
'manipulates_state': False
374374
}
375375

376-
def __init__(self):
376+
def __init__(self) -> None:
377377
transitions = (
378378
(0, C, 0, C),
379379
(0, D, 2, D),
@@ -409,7 +409,7 @@ class EvolvedFSM16(FSMPlayer):
409409
'manipulates_state': False
410410
}
411411

412-
def __init__(self):
412+
def __init__(self) -> None:
413413
transitions = (
414414
(0, C, 0, C),
415415
(0, D, 12, D),
@@ -469,7 +469,7 @@ class EvolvedFSM16Noise05(FSMPlayer):
469469
'manipulates_state': False
470470
}
471471

472-
def __init__(self):
472+
def __init__(self) -> None:
473473
transitions = (
474474
(0, C, 8, C),
475475
(0, D, 3, D),

axelrod/strategies/hmm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def is_well_formed(self) -> bool:
6363
return False
6464
return True
6565

66-
def __eq__(self, other) -> bool:
66+
def __eq__(self, other: Player) -> bool:
6767
"""Equality of two HMMs"""
6868
check = True
6969
for attr in ["transitions_C", "transitions_D",
@@ -72,7 +72,7 @@ def __eq__(self, other) -> bool:
7272
return check
7373

7474

75-
def move(self, opponent_action) -> Action:
75+
def move(self, opponent_action: Action) -> Action:
7676
"""Changes state and computes the response action.
7777
7878
Parameters
@@ -140,7 +140,7 @@ def is_stochastic(self) -> bool:
140140
return True
141141
return False
142142

143-
def strategy(self, opponent) -> Action:
143+
def strategy(self, opponent: Player) -> Action:
144144
if len(self.history) == 0:
145145
return self.initial_action
146146
else:

axelrod/strategies/human.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from os import linesep
2-
from axelrod.actions import Actions
2+
from axelrod.actions import Actions, Action
33
from axelrod.player import Player
44
from prompt_toolkit import prompt
55
from prompt_toolkit.token import Token
@@ -19,7 +19,7 @@ class ActionValidator(Validator):
1919
Described at http://python-prompt-toolkit.readthedocs.io/en/latest/pages/building_prompts.html#input-validation
2020
"""
2121

22-
def validate(self, document):
22+
def validate(self, document) -> None:
2323
text = document.text
2424

2525
if text and text.upper() not in ['C', 'D']:
@@ -118,7 +118,7 @@ def _status_messages(self):
118118
'print': print_statement
119119
}
120120

121-
def _get_human_input(self): # pragma: no cover
121+
def _get_human_input(self) -> Action: # pragma: no cover
122122
"""
123123
A method to prompt the user for input, validate it and display
124124
the bottom toolbar.
@@ -137,7 +137,7 @@ def _get_human_input(self): # pragma: no cover
137137

138138
return action.upper()
139139

140-
def strategy(self, opponent, input_function=None):
140+
def strategy(self, opponent: Player, input_function=None):
141141
"""
142142
Ordinarily, the strategy prompts for keyboard input rather than
143143
deriving its own action.

axelrod/strategies/prober.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from axelrod.actions import Actions, Action
22
from axelrod.player import Player
33
from axelrod.random_ import random_choice
4+
from typing import List
5+
Vector = List[float]
46

57
import random
68

@@ -258,7 +260,7 @@ class NaiveProber(Player):
258260
'manipulates_state': False
259261
}
260262

261-
def __init__(self, p: Player=0.1) -> None:
263+
def __init__(self, p: float=0.1) -> None:
262264
"""
263265
Parameters
264266
----------

run_mypy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
"axelrod/strategies/rand.py",
4747
"axelrod/strategies/titfortat.py",
4848
"axelrod/strategies/hmm.py",
49+
"axelrod/strategies/human.py",
50+
"axelrod/strategies/finite_state_machines.py",
4951
"axelrod/strategies/worse_and_worse.py"]
5052

5153
exit_codes = []

0 commit comments

Comments
 (0)