Skip to content

Commit 122277b

Browse files
committed
Enable structural typing of RangeSummary to allow for datetime
Structural typing requires that the parameter names match. For numeric types like float, the __lt__ method takes an "x" parameter. For datetime, the __lt__ method takes an "other" parameter, which caused Pyright to show datetime as not matching the bounds of the TypeVar T when used with RangeSummary. This modifies the bounds to be a union of Protocol types that allow either the "x" or "other" parameter
1 parent 2b7cb9a commit 122277b

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

pystac/summaries.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,31 @@
3030
from abc import abstractmethod
3131

3232

33-
class Comparable(Protocol):
34-
"""Protocol for annotating comparable types."""
33+
class _Comparable_x(Protocol):
34+
"""Protocol for annotating comparable types.
35+
36+
For matching __lt__ that takes an 'x' parameter
37+
(e.g. float)
38+
"""
3539

3640
@abstractmethod
3741
def __lt__(self: "T", x: "T") -> bool:
3842
return NotImplemented
3943

4044

41-
T = TypeVar("T", bound=Comparable)
45+
class _Comparable_other(Protocol):
46+
"""Protocol for annotating comparable types.
47+
48+
For matching __lt___ that takes an 'other' parameter
49+
(e.g. datetime)
50+
"""
51+
52+
@abstractmethod
53+
def __lt__(self: "T", other: "T") -> bool:
54+
return NotImplemented
55+
56+
57+
T = TypeVar("T", bound=Union[_Comparable_x, _Comparable_other])
4258

4359

4460
class RangeSummary(Generic[T]):

0 commit comments

Comments
 (0)