Sure, but at that point you've just reimplemented python in macros ;)
The other thing to note is that
sorted([(k.weight, k.name) for k in somelist], reverse=True)
is essentially already typechecked: def biggest_ks(ks: K):
return sorted([(k.weight, k.name) for k in ks], reverse=True)
The above code now has all the same type guarantees as your c++, actually maybe more since the macros you use are going to be...uhhh, mysterious.