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

chore: add type annotations and type checking #36

Merged
merged 2 commits into from
Aug 13, 2023
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
3 changes: 3 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
- name: Check formatting
run: pipenv run black --target-version=py310 .

- name: Check types
run: pipenv run mypy rps rps-sim.py

- name: Run tests
run: |
pipenv run pytest
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ black = "*"
pytest = "*"
pytest-mock = "*"
mypy-extensions = "*"
mypy = "*"

[requires]
python_version = "3.10"
Expand Down
38 changes: 37 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.mypy]
strict = true
9 changes: 6 additions & 3 deletions rps/game.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
def play_game(firstPlayer, secondPlayer):
from .player import Player
from .play import PlayResult

def play_game(firstPlayer: Player, secondPlayer: Player) -> PlayResult:
firstPlay = firstPlayer.play()
secondPlay = secondPlayer.play()
return firstPlay.match(secondPlay)


def play_games(firstPlayer, secondPlayer, times):
def play_games(firstPlayer: Player, secondPlayer: Player, times: int) -> list[PlayResult]:
results = []
for game in range(times):
for _ in range(times):
results.append(play_game(firstPlayer, secondPlayer))
return results
10 changes: 6 additions & 4 deletions rps/play.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from enum import IntEnum


Expand All @@ -7,7 +8,7 @@ class PlayResult(IntEnum):
WIN = 1


def human_readable(result):
def human_readable(result: PlayResult) -> str:
if result == PlayResult.WIN:
return "wins"
elif result == PlayResult.LOSS:
Expand All @@ -21,7 +22,7 @@ class Play(IntEnum):
PAPER = 2
SCISSORS = 3

def match(self, other):
def match(self, other: Play) -> PlayResult:
if self.value == other.value or type(other) is not Play:
return PlayResult.DRAW
if self.value == self.ROCK:
Expand All @@ -30,11 +31,12 @@ def match(self, other):
return PlayResult.WIN if other.value == self.PAPER else PlayResult.LOSS
elif self.value == self.PAPER:
return PlayResult.WIN if other.value == self.ROCK else PlayResult.LOSS
return PlayResult.DRAW

@staticmethod
def min():
def min() -> int:
return min([Play.ROCK, Play.PAPER, Play.SCISSORS])

@staticmethod
def max():
def max() -> int:
return max([Play.ROCK, Play.PAPER, Play.SCISSORS])
8 changes: 5 additions & 3 deletions rps/player.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import abc

from .play import Play
from .strategy import PlaySelectionStrategy


class Player(object):
@abc.abstractmethod
def play(self):
def play(self) -> Play:
"""Required method"""


class BotPlayer(Player):
def __init__(self, play_strategy):
def __init__(self, play_strategy: PlaySelectionStrategy):
self.__strategy = play_strategy

def play(self):
def play(self) -> Play:
return self.__strategy.play()
4 changes: 2 additions & 2 deletions rps/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class PlaySelectionStrategy(object):
__metaclass__ = abc.ABCMeta

@abc.abstractmethod
def play(self):
def play(self) -> Play:
"""Required method"""


class RandomPlayStrategy(PlaySelectionStrategy):
def play(self):
def play(self) -> Play:
return Play(randrange(Play.min(), Play.max() + 1))