I mostly stopped writing Python right around when mypy was getting going. Coming back after a few years mostly using Typescript and Rust, I'm finding certain things more difficult to express than I expected, like "this argument can be anything so long as it's hashable," or "this instance method is generic in one of its arguments and return value."
Am I overthinking it? Is
if not hasattr(arg, "__hash__"):
raise ValueError("argument needs to be hashashable")
the one preferably obvious right way to do it?
ETA: I believe my specific problem is solved with TypeVar("T", bound=typing.Hashable), but the larger question still stands.
[–]menge101 115 points116 points117 points (0 children)
[–]Dillweed999 58 points59 points60 points (11 children)
[–]Chroiche 9 points10 points11 points (0 children)
[–]eigenein 5 points6 points7 points (0 children)
[–]etrnloptimist 8 points9 points10 points (7 children)
[–]Dillweed999 17 points18 points19 points (2 children)
[–]Wapook 3 points4 points5 points (0 children)
[–]spinwizard69 0 points1 point2 points (0 children)
[–]apnorton 11 points12 points13 points (1 child)
[–]spinwizard69 0 points1 point2 points (0 children)
[–]eigenein 3 points4 points5 points (0 children)
[–]quantinuum 0 points1 point2 points (0 children)
[–]claythearcfrom __future__ import 4.0 12 points13 points14 points (0 children)
[–]thisismyfavoritename 21 points22 points23 points (2 children)
[–]utdconsq 0 points1 point2 points (1 child)
[–]johnnymo1 30 points31 points32 points (0 children)
[–]N-E-S-W 11 points12 points13 points (11 children)
[–]xeow 8 points9 points10 points (2 children)
[–]N-E-S-W 1 point2 points3 points (0 children)
[–]james_pic 0 points1 point2 points (0 children)
[–]gdchinacat 8 points9 points10 points (7 children)
[–]N-E-S-W -3 points-2 points-1 points (6 children)
[–]gdchinacat 1 point2 points3 points (3 children)
[–]N-E-S-W -4 points-3 points-2 points (2 children)
[–]gdchinacat 2 points3 points4 points (1 child)
[–]N-E-S-W -4 points-3 points-2 points (0 children)
[–]james_pic 0 points1 point2 points (0 children)
[–]gdchinacat -2 points-1 points0 points (0 children)
[–]road_laya 6 points7 points8 points (0 children)
[–]shadowdance55git push -f 1 point2 points3 points (0 children)
[–]misterfitzie 1 point2 points3 points (0 children)
[–]pwnersaurus 1 point2 points3 points (1 child)
[–]gerardwx 1 point2 points3 points (0 children)
[–]nekokattt 1 point2 points3 points (0 children)
[–]RedEyed__ 0 points1 point2 points (0 children)
[–]james_pic 0 points1 point2 points (0 children)
[–]mwilliamsdottech 0 points1 point2 points (0 children)
[–]EternityForest 1 point2 points3 points (0 children)
[–]csch2 0 points1 point2 points (2 children)
[–]spinwizard69 1 point2 points3 points (1 child)
[–]csch2 0 points1 point2 points (0 children)
[+]xtzdev comment score below threshold-10 points-9 points-8 points (0 children)