Skip to content

Preserve proxy set operations and relation assignments - #58

Merged
HexDecimal merged 2 commits into
HexDecimal:mainfrom
antonsoo:fix/retain-inplace-relation-updates
Oct 13, 2026
Merged

HexDecimal merged 2 commits into
HexDecimal:mainfrom
antonsoo:fix/retain-inplace-relation-updates

Conversation

@antonsoo

Copy link
Copy Markdown
Contributor

An in-place update of a relation set is written back to EntityRelations.__setitem__. That setter clears the targets before iterating its argument, so a live view of those same targets is now empty:

from tcod.ecs import Registry

world = Registry()
player = world["player"]
player.relation_tags_many["allies"] = {world["first"], world["second"]}
player.relation_tags_many["allies"] |= {world["third"]}
print(sorted(e.uid for e in player.relation_tags_many["allies"]))

Currently this prints []; it should print ['first', 'second', 'third']. Materialize the input iterable before clearing the old targets, which also handles lazy iterators over the same relation view.

The inherited binary set operations have a related constructor problem: their default factory tries to construct an entity-bound proxy from only an iterable. Provide a plain-set factory for both EntityTags and EntityRelationsMapping. Binary operations then return independent sets, and &= can compute its removal set without raising TypeError.

Regression checks cover all four in-place relation operators, cached queries, a lazy self-view, copying another relation key, non-mutating binary operations and tag intersection. Seven checks fail before the change; the separate-key assignment control already passes. The full pytest/doctest suite, mypy and project-pinned Ruff checks pass, with the same baseline xpasses and warning.

Nine native 80-turn party-selection runs are compared with plain Python sets and cached forward/reverse queries. The change removes 687 wrong turn states and 180 operator exceptions; all corrected histories match direct assignment. Nine direct-assignment controls retain identical histories across all 720 turns. This check validates game-state bookkeeping, without claiming performance or gameplay quality improvements.

@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.16%. Comparing base (1696e6e) to head (3ba212d).

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #58   +/-   ##
=======================================
  Coverage   99.15%   99.16%           
=======================================
  Files           9        9           
  Lines         949      956    +7     
=======================================
+ Hits          941      948    +7     
  Misses          8        8           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@HexDecimal HexDecimal left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I forget if I've heard about the _from_iterable classmethod before now. Luckily the entity attribute assignments are more for completeness than as a primary method of interacting with them. Still, it's really good to have this fix.

The code and tests are good, but what's fixed needs to be noted in the changelog. I can do that if you don't.

@HexDecimal HexDecimal self-assigned this Oct 13, 2026
@HexDecimal
HexDecimal force-pushed the fix/retain-inplace-relation-updates branch from 8937954 to 3ba212d Compare October 13, 2026 07:23
@HexDecimal
HexDecimal merged commit 1401233 into HexDecimal:main Oct 13, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants