broken pokémon sorting in classic release method with prefer='IV'
infinitewarp opened this issue · 2 comments
As I was working on new unit tests in #562, I discovered that the classic release method has broken sorting when prefer='IV'. The root cause is this ternary statement:
self.sort_key = lambda x: (x.cp, x.iv) if self.prefer == 'CP' else lambda x: (x.iv, x.cp)
As first glance you might think like the ternary would return the first lambda if 'CP' and the second lambda otherwise, but that's not the case. It's actually always returning the first lambda, and that lambda's return value is the ternary statement. You can see this in this simplified example:
>>> a = True
>>> b = lambda: 'yay' if a else lambda: 'boo'
>>> b()
'yay'
>>> a = False
>>> b = lambda: 'yay' if a else lambda: 'boo'
>>> b()
<function <lambda> at 0x1056c3410>
The net effect is sorting is totally broken if you want to prefer high-IV keepers.
I'm working on a fix now.
(also @magnetik89 FYI since you authored the original line)
FWIW, @magnetik89 I deliberately chose not to use another ternary statement to favor readability here. It was confusing already (thus the bug), and adding a bunch of parentheses to the line doesn't help that.