Skip to content

Tests: require_component instead of Optional get_component (review fix from #23) - #27

Merged
umutcanekinci merged 3 commits into
mainfrom
chore/pygamine-0.28.0
Nov 3, 2026
Merged

umutcanekinci merged 3 commits into
mainfrom
chore/pygamine-0.28.0

Conversation

@umutcanekinci

@umutcanekinci umutcanekinci commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #23. Its review asked that tests/test_cloud.py stop treating cloud.get_component(Rigidbody2D) (Optional) as non-None. The fix (3834a4e) was pushed in the same minute #23 merged, so it never reached main.

That commit also swapped the require_component lookups in src/ for a stored rigidbody attribute. The review on this PR showed that's riskier (the attribute can drift from the registered component), so the PR is narrowed: src/ stays as on main, and the tests use require_component(Rigidbody2D).

Tests only, no gameplay change. 247 tests pass, ruff clean.

🤖 Generated with Claude Code

umutcanekinci and others added 2 commits November 3, 2026 03:59
Address review on #23: store the component add_component() returns as
self.rigidbody instead of looking it up per access, use the Surface from
load_image() directly in Tile, switch test_cloud to cloud.rigidbody, and
put the clamp01 import in order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/gameplay/tiles/tile.py Outdated
renderer = self.add_component(SpriteRenderer2D)
renderer.set_image(load_image(ImagePath("tile")))
image = renderer.require_image()
image = load_image(ImagePath("tile"))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review: load_image(ImagePath("tile")) with no size returns the shared cached surface from pygamine's _cache, not a copy. Line 47 then runs polygon(self.unselected_surface, ...) on it, so the grey grid outline gets drawn into the global cache, once for every tile (64+ times on the same pixels). The bug was already there, but this rewrite makes image the cached object directly. Anything else that loads "tile" later (another map size, a UI preview) gets the outline baked in. Fix: image = load_image(ImagePath("tile")).copy().

Comment thread src/gameplay/buildings/building.py Outdated
image_path=self.get_image_path(),
)
self.add_component(Rigidbody2D).set_velocity((0, 0))
self.rigidbody = self.add_component(Rigidbody2D)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review: self.rigidbody is now a second reference next to _components["Rigidbody2D"]. add_component overwrites the entry by class name, so if a Rigidbody2D is ever re-added or replaced, the engine would update the new component while rigidbody (and so is_moving, has_arrived, control_moving) keeps reading the old one, which stays at the old velocity. The removed require_component property always returned the live component. Low risk today, since nothing re-adds it, but it is state that can drift. If the only goal was the Optional in tests/test_cloud.py, require_component(Rigidbody2D) in the test fixes that without the alias.

Comment thread src/gameplay/clouds/cloud_container.py Outdated
companion.require_component(Rigidbody2D).set_velocity(
cloud.require_component(Rigidbody2D).velocity
)
companion.rigidbody.set_velocity(cloud.rigidbody.velocity)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review: GameObjectList subclasses a plain list, so cloud here and in update() (for cloud in self) is Any. That means cloud.rigidbody / companion.rigidbody are not type-checked: a typo or a rename passes mypy, and the "typed attribute" gives no benefit in this file. Consider annotating the container (e.g. make CloudContainer a list[Cloud] or annotate the loop variable) so the attribute is actually typed where it's used most.

Comment thread tests/test_cloud.py Outdated
assert 0 <= cloud.rect.x <= SURFACE_SIZE[0] - 101
assert 0 <= cloud.rect.y <= SURFACE_SIZE[1] - 101
rb = cloud.get_component(Rigidbody2D)
rb = cloud.rigidbody

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review: the tests now set and read velocity only through the cloud.rigidbody alias. Nothing checks that the alias is the component the engine updates (cloud.rigidbody is cloud.get_component(Rigidbody2D)), so if they drifted apart, the wrap/companion tests would still pass while clouds stopped moving in game. A one-line identity assert in the spawn test would cover it.

Comment thread CHANGELOG.md Outdated
first menu button.
- pygamine 0.28.0: buildings, clouds and tiles use the engine's `require_component()` and
`require_image()` instead of `get_component()` plus a `None` assert, and the volume label uses
- pygamine 0.28.0: buildings and clouds keep their Rigidbody2D as a typed `rigidbody` attribute

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review: this bullet still credits pygamine 0.28.0, but after this PR the game no longer uses require_component() or require_image() from 0.28.0 (only clamp01()). It also leaves out the tile.py change. Suggest wording it as an internal change, separate from the 0.28.0 bump, or dropping it, since it's behavior-neutral.

Comment thread src/app/game.py
from pygamine import Application
from pygamine import clamp01
from pygamine import AssetManager
from pygamine import clamp01

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review (conventions): this hunk only moves an import. highrise/CLAUDE.md says: "Keep pure reformatting out of feature commits". It's tiny, but it's the only change in this file. Either drop it or put it in its own commit.

The review on this PR found the stored rigidbody attribute riskier than the
property already on main (it can drift from the registered component). Keep
main's src as is; tests/test_cloud.py now uses require_component(Rigidbody2D)
instead of the Optional get_component(), which is what #23's review asked for.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@umutcanekinci umutcanekinci changed the title Rigidbody2D as a typed attribute: review fix from #23 Tests: require_component instead of Optional get_component (review fix from #23) Nov 3, 2026
@umutcanekinci

Copy link
Copy Markdown
Collaborator Author

Review follow-up (lead): narrowed the PR to the tests only. Findings on building.py, cloud_container.py, CHANGELOG.md and game.py no longer apply: those files are back to main, where require_component returns the live component. The test-alias finding is covered: the tests now call require_component(Rigidbody2D) directly. The tile.py cache finding predates this PR, so it's filed as its own issue.

@umutcanekinci
umutcanekinci marked this pull request as ready for review November 3, 2026 18:09
@umutcanekinci
umutcanekinci added this pull request to the merge queue Nov 3, 2026
Merged via the queue into main with commit ce61529 Nov 3, 2026
7 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.

1 participant