Skip to content

fix: stop estimate_hand_value from changing HandConfig.yaku - #357

Open
Nihisil wants to merge 3 commits into
masterfrom
fix/yaku-config-mutation
Open

Nihisil wants to merge 3 commits into
masterfrom
fix/yaku-config-mutation

Conversation

@Nihisil

@Nihisil Nihisil commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

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%)

@Nihisil
Nihisil requested a review from Apricot-S as a code owner November 13, 2026 11:53
@Nihisil Nihisil added the bug label Nov 13, 2026
Apricot-S
Apricot-S previously approved these changes Nov 14, 2026
@Apricot-S

Copy link
Copy Markdown
Collaborator

I agree with the direction of this change. However, I think there are compatibility implications worth distinguishing:

Case Impact
Double yakuman enabled No change: instance customizations are preserved.
Double yakuman disabled Custom attributes such as name are no longer preserved because a new instance is created. Han values were already overwritten with 13 before this change.
Daisharin Custom han values and other instance attributes are no longer preserved. The name was already overwritten according to the suit before this change.
Dora / paarenchan Custom attributes such as name are no longer preserved. Han values were already overwritten during calculation before this change.

For the cases where a new instance is created, code relying on the result containing the same instance as config.yaku is also affected.

Separately, changing Yaku.__eq__ and Yaku.__hash__ to use yaku_id changes the behavior of all yaku instances. Distinct instances of the same yaku now compare equal, collapse into a single set entry, and are treated as the same dictionary key.

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 list[list[tile_34]] to tuple[Block, ...], introducing a new shanten calculation algorithm, and adding APIs for calculating effective tiles and discard candidates.

@Nihisil

Nihisil commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Thank, I need to think how to handle it better, I will return here in a few days

@Nihisil

Nihisil commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

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

@Nihisil
Nihisil requested a review from Apricot-S November 18, 2026 01:55
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.
@9124y78syf78923
9124y78syf78923 force-pushed the fix/yaku-config-mutation branch from c40aee2 to baa65d8 Compare November 21, 2026 08:41
Comment thread CHANGELOG.md Resolved

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@Nihisil
Nihisil requested a review from Apricot-S November 21, 2026 10:24
if not isinstance(other, Yaku):
return NotImplemented

return (self._origin or self) is (other._origin or other)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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._origin

copy() 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.

Comment on lines +78 to +88
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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants