From 89c20eac867122eb7291170e98fdfacd0bec4a8d Mon Sep 17 00:00:00 2001 From: Marcel Wilson Date: Fri, 16 Sep 2022 12:30:38 -0500 Subject: [PATCH] #25 fixing mypy error on subclassed methods --- screenpy/actions/pause.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/screenpy/actions/pause.py b/screenpy/actions/pause.py index 5c062783..72333544 100644 --- a/screenpy/actions/pause.py +++ b/screenpy/actions/pause.py @@ -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. @@ -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: @@ -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( @@ -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}" @@ -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 ''}"