Skip to content

Commit

Permalink
ScreenPyHQ#25 fixing mypy error on subclassed methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bandophahita committed Sep 22, 2022
1 parent 2949415 commit 89c20ea
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions screenpy/actions/pause.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

import re
from time import sleep
from typing import Type, TypeVar

from screenpy.actor import Actor
from screenpy.exceptions import UnableToAct
from screenpy.pacing import beat

T_pause = TypeVar("T_pause", bound="Pause")


class Pause:
"""Pause the Actor's Actions for a set amount of time.
Expand All @@ -33,11 +36,11 @@ class Pause:
time: float

@classmethod
def for_(cls, number: float) -> "Pause":
def for_(cls: Type[T_pause], number: float) -> T_pause:
"""Specify how many seconds or milliseconds to wait for."""
return cls(number)

def seconds_because(self, reason: str) -> "Pause":
def seconds_because(self: T_pause, reason: str) -> T_pause:
"""Use seconds and provide a reason for the pause.
Aliases:
Expand All @@ -47,23 +50,23 @@ def seconds_because(self, reason: str) -> "Pause":
self.reason = self._massage_reason(reason)
return self

def second_because(self, reason: str) -> "Pause":
def second_because(self: T_pause, reason: str) -> T_pause:
"""Alias for :meth:`~screenpy.actions.Pause.seconds_because`."""
return self.seconds_because(reason)

def milliseconds_because(self, reason: str) -> "Pause":
def milliseconds_because(self: T_pause, reason: str) -> T_pause:
"""Use milliseconds and provide a reason for the pause."""
self.unit = f"millisecond{'s' if self.number != 1 else ''}"
self.time = self.time / 1000.0
self.reason = self._massage_reason(reason)
return self

def describe(self) -> str:
def describe(self: T_pause) -> str:
"""Describe the Action in present tense."""
return f"Pause for {self.number} {self.unit} {self.reason}."

@beat("{} pauses for {number} {unit} {reason}.")
def perform_as(self, _: Actor) -> None:
def perform_as(self: T_pause, _: Actor) -> None:
"""Direct the Actor to take their union-mandated break."""
if not self.reason:
raise UnableToAct(
Expand All @@ -73,7 +76,7 @@ def perform_as(self, _: Actor) -> None:

sleep(self.time)

def _massage_reason(self, reason: str) -> str:
def _massage_reason(self: T_pause, reason: str) -> str:
"""Apply some gentle massaging to the reason string."""
if not reason.startswith("because"):
reason = f"because {reason}"
Expand All @@ -82,7 +85,7 @@ def _massage_reason(self, reason: str) -> str:

return reason

def __init__(self, number: float) -> None:
def __init__(self: T_pause, number: float) -> None:
self.number = number
self.time = number
self.unit = f"second{'s' if self.number != 1 else ''}"
Expand Down

0 comments on commit 89c20ea

Please sign in to comment.