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

Clarify that TGamePoints must be comparable to itself #6

Open
PeterJCLaw opened this issue Mar 2, 2024 · 0 comments
Open

Clarify that TGamePoints must be comparable to itself #6

PeterJCLaw opened this issue Mar 2, 2024 · 0 comments

Comments

@PeterJCLaw
Copy link
Owner

The current type signature allows passing game points types which don't implement useful comparisons, which isn't really the case. For example, the following code will error at runtime but passes the type checking:

import league_ranker

class Foo: pass

league_ranker.calc_positions({'a': Foo(), 'b': Foo()})

The fix is likely something like:

diff --git a/league_ranker/__init__.py b/league_ranker/__init__.py
index 426b46c..9c918b8 100644
--- a/league_ranker/__init__.py
+++ b/league_ranker/__init__.py
@@ -2,6 +2,7 @@
 
 from collections import defaultdict
 from typing import (
+    Any,
     Collection,
     Container,
     Dict,
@@ -10,15 +11,22 @@ from typing import (
     Mapping,
     NewType,
     Optional,
+    Protocol,
     Sequence,
     Set,
     Tuple,
     TypeVar,
 )
 
+
+class SupportsLessThan(Protocol):
+    def __lt__(self, __other: Any) -> bool:
+        ...
+
+
 T = TypeVar('T')
 TZone = TypeVar('TZone', bound=Hashable)
-TGamePoints = TypeVar('TGamePoints')
+TGamePoints = TypeVar('TGamePoints', bound=SupportsLessThan)
 
 RankedPosition = NewType('RankedPosition', int)
 LeaguePoints = NewType('LeaguePoints', int)

but will need to account for the supported versions of Python etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant