Repository navigation
Conversation
|
I agree with the direction of this change. However, I think there are compatibility implications worth distinguishing:
For the cases where a new instance is created, code relying on the result containing the same instance as Separately, changing Since this changes the equality semantics of a public class, I consider it a breaking change. Should this be introduced in a major version release? I would support this change as part of a major version release. I am also considering algorithmic improvements involving substantial breaking changes, such as switching yaku evaluation from |
|
Thank, I need to think how to handle it better, I will return here in a few days |
|
I decided to revert changes that were forcing us to 3.0.0 release and in current realization we should be able to release as minor release and at the same time fix the issue we are having |
estimate_hand_value wrote per-hand state onto the shared yaku objects: dora, aka dora and ura dora han, the paarenchan count, the daisharin name, and 13 han for double yakuman when has_double_yakuman was off. Reusing a config changed earlier results, and once the flag had been off, double yakuman stayed single after it was turned back on. Those yaku are now created per hand with that hand's han or name. Yaku compare and hash by yaku_id, so `config.yaku.X in result.yaku` and the aotenjou precursor filter still match them. Refs #287
Comparing yaku by yaku_id changed equality for every yaku instance: separate instances of one yaku became equal and merged in sets and dicts. Yaku compare by identity again. Per-hand yaku are now made with Yaku.copy(), which keeps custom attributes such as name and compares and hashes as the yaku it was copied from, so `config.yaku.X in result.yaku` and the aotenjou precursor filter still match them.
c40aee2 to
baa65d8
Compare
There was a problem hiding this comment.
Could we clarify the intended behavior of copy.copy() for a yaku returned by the calculator? For example, if scored is the double kokushi yaku from result.yaku, the same comparison gives different results depending on the scoring option:
copy.copy(scored) == scored # Double yakuman enabled: False (26 han)
copy.copy(scored) == scored # Double yakuman disabled: True (13 han)In the 13-han case, the shallow copy inherits _origin. Before this change, the comparison was False in both cases. This also affects dictionary keys: inserting copy.copy(scored) as a second key overwrites the first entry only in the 13-han case.
I see why the scored yaku should compare equal to config.yaku.daburu_kokushi, so callers can still find it in result.yaku. Could we preserve that lookup while keeping the behavior of an explicit copy.copy() consistent across both scoring options?
There was a problem hiding this comment.
Thanks, I have updated the changelog and modified copy behaviour
A plain copy.copy of a scored yaku kept its _origin, so it compared equal to the scored yaku only when that yaku was a copy, for example double yakuman scored as single. Yaku.__copy__ now drops _origin, so copy.copy gives a separate yaku under both scoring options. Add a compatibility example for the scored yaku to the changelog.
| if not isinstance(other, Yaku): | ||
| return NotImplemented | ||
|
|
||
| return (self._origin or self) is (other._origin or other) |
There was a problem hiding this comment.
Could we use an explicit None check when resolving _origin? A Yaku subclass can define __bool__ or __len__. If the original yaku evaluates to False, _origin or self selects the scored copy itself, so even dora in [dora.copy()] returns False.
A small helper would keep copy(), __eq__, and __hash__ consistent:
def _identity_origin(self) -"Yaku":
return self if self._origin is None else self._origincopy() could assign copied._origin = self._identity_origin(), __eq__ could compare the two origins with is, and __hash__ could use object.__hash__(self._identity_origin()). A regression test with a Dora subclass whose __bool__ returns False could check both dora in [scored] and {scored: 3}[dora] == 3.
| def __copy__(self: _YakuT) -> _YakuT: # noqa: PYI019 | ||
| """ | ||
| Return a shallow copy that is a separate yaku, even when this one was made by :meth:`copy`. | ||
|
|
||
| :return: shallow copy of this yaku, equal only to itself | ||
| """ | ||
| copied = self.__class__.__new__(self.__class__) | ||
| copied.__dict__.update(self.__dict__) | ||
| copied.__dict__.pop("_origin", None) | ||
| return copied | ||
|
|
There was a problem hiding this comment.
The behavior changes when __slots__ is present, but I feel this might be acceptable as long as it’s noted in the CHANGELOG. What do you think?
Refs #287
I tried few approaches and current one looks like less invasive way to fix it, and also additionally checked that
type(yaku)()doesn't hurt performance much (naive copy.copy() version reduced performance for 5%)