Skip to content

Commit 85eb020

Browse files
authored
fix typing in arcade.math.clamp (#2377)
* fix typing in arcade.math.clamp * formatting * why do you hate my comment
1 parent 68dbe27 commit 85eb020

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

arcade/math.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from pyglet.math import Vec2, Vec3
88

9-
from arcade.types import HasAddSubMul, Point, Point2
9+
from arcade.types import HasAddSubMul, Point, Point2, SupportsRichComparison
1010
from arcade.types.rect import Rect
1111
from arcade.types.vector_like import Point3
1212

@@ -34,16 +34,21 @@
3434
"quaternion_rotation",
3535
]
3636

37+
SupportsRichComparisonT = TypeVar("SupportsRichComparisonT", bound=SupportsRichComparison)
3738

38-
def clamp(a, low: float, high: float) -> float:
39+
40+
def clamp(
41+
a: SupportsRichComparisonT, low: SupportsRichComparisonT, high: SupportsRichComparisonT
42+
) -> SupportsRichComparisonT:
3943
"""Clamp a number between a range.
4044
4145
Args:
4246
a (float): The number to clamp
4347
low (float): The lower bound
4448
high (float): The upper bound
4549
"""
46-
return high if a > high else max(a, low)
50+
# Python will deal with > unsupported by falling back on <.
51+
return high if a > high else max(a, low) # type: ignore
4752

4853

4954
# This TypeVar helps match v1 and v2 as the same type below in lerp's

arcade/types/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# flake8: noqa: E402
2727
import sys
2828
from pathlib import Path
29-
from typing import NamedTuple, Union, TYPE_CHECKING, TypeVar, Iterable, Protocol
29+
from typing import Any, NamedTuple, Union, TYPE_CHECKING, TypeVar, Iterable, Protocol
3030

3131
from pytiled_parser import Properties
3232

@@ -249,3 +249,15 @@ class TiledObject(NamedTuple):
249249
"""Name of the object"""
250250
type: str | None = None
251251
"""Type of the object"""
252+
253+
254+
# Stolen from Pylance
255+
class SupportsDunderLT(Protocol[_T_contra]):
256+
def __lt__(self, other: _T_contra, /) -> bool: ...
257+
258+
259+
class SupportsDunderGT(Protocol[_T_contra]):
260+
def __gt__(self, other: _T_contra, /) -> bool: ...
261+
262+
263+
SupportsRichComparison = Union[SupportsDunderLT[Any], SupportsDunderGT[Any]]

0 commit comments

Comments
 (0)