Whole file

benmoran56/esper

The author described this change as FIX: removing last component deletes entity as well (#83). It counts as a record because the checks below fail on the code as it stood at 730b92940 and pass on 226d876e0, with nothing else changed between the two runs.

Fix saved2023-05-10
Sharing licenceMIT · LICENSE
Change size+5 8

What the code was meant to do, written into the code itself as a save note

FIX: removing last component deletes entity as well (#83)

The change

1+import inspect as _inspect
12 import time as _time
23
34 from types import MethodType as _MethodType
45
6+from typing import cast as _cast
57 from typing import Iterable as _Iterable
68 from typing import List as _List
79 from typing import Optional as _Optional
317319 self._entities[entity][component_type] = component_instance
318320 self.clear_cache()
319321
320- def remove_component(self, entity: int, component_type: _Type[_C]) -> int:
322+ def remove_component(self, entity: int, component_type: _Type[_C]) -> _C:
321323 """Remove a Component instance from an Entity, by type.
322324
323- A Component instance can be removed by providing its type.
325+ A Component instance can only be removed by providing its type.
324326 For example: world.delete_component(enemy_a, Velocity) will remove
325327 the Velocity instance from the Entity enemy_a.
326328
332334 if not self._components[component_type]:
333335 del self._components[component_type]
334336
335- del self._entities[entity][component_type]
336-
337- if not self._entities[entity]:
338- del self._entities[entity]
339-
340337 self.clear_cache()
341- return entity
338+ return self._entities[entity].pop(component_type)
342339
343340 def _get_component(self, component_type: _Type[_C]) -> _Iterable[_Tuple[int, _C]]:
344341 entity_db = self._entities

The check that tells the two apart

failpass·tests/test_world.py::TestRemoveComponent::test_remove_component_returns_removed_instance
failpass·tests/test_world.py::TestRemoveComponent::test_remove_last_component_leaves_empty_entity

Check file tests/test_world.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it730b92940a5572aabb2948a01738072d5ea97792
Broken version dated2023-05-09
Moduleesper.__init__
Units changedWorld
Fingerprint2503a099d31533ee
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in benmoran56/esper