Skip to content

Clarify that TGamePoints must be comparable to itself #6

@PeterJCLaw

Description

@PeterJCLaw

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions