Fix handler registration order in Scene (#1642)
Some checks are pending
Tests / tests (macos-latest, 3.10) (push) Waiting to run
Tests / tests (macos-latest, 3.11) (push) Waiting to run
Tests / tests (macos-latest, 3.12) (push) Waiting to run
Tests / tests (macos-latest, 3.13) (push) Waiting to run
Tests / tests (macos-latest, 3.9) (push) Waiting to run
Tests / tests (ubuntu-latest, 3.10) (push) Waiting to run
Tests / tests (ubuntu-latest, 3.11) (push) Waiting to run
Tests / tests (ubuntu-latest, 3.12) (push) Waiting to run
Tests / tests (ubuntu-latest, 3.13) (push) Waiting to run
Tests / tests (ubuntu-latest, 3.9) (push) Waiting to run
Tests / tests (windows-latest, 3.10) (push) Waiting to run
Tests / tests (windows-latest, 3.11) (push) Waiting to run
Tests / tests (windows-latest, 3.12) (push) Waiting to run
Tests / tests (windows-latest, 3.13) (push) Waiting to run
Tests / tests (windows-latest, 3.9) (push) Waiting to run
Tests / pypy-tests (macos-latest, pypy3.10) (push) Waiting to run
Tests / pypy-tests (macos-latest, pypy3.9) (push) Waiting to run
Tests / pypy-tests (ubuntu-latest, pypy3.10) (push) Waiting to run
Tests / pypy-tests (ubuntu-latest, pypy3.9) (push) Waiting to run

* Fix handler registration order in `Scene`

Previously, `Scene` handlers were registered based on the sorted output of `inspect.getmembers`, causing incorrect execution order. Now, handlers are registered in the order they are defined in the class, ensuring reliable behavior and proper sequence when handling filters with varying specificity. Added test cases to validate the correct handler ordering.

* Add dynamic dataclass and class attribute resolvers

Introduced `dataclass_kwargs` to ensure compatibility with different Python versions and modular attribute handling. Added utilities for resolving class attributes dynamically, enhancing flexibility with MRO-based resolvers. Updated tests to verify new features and ensure proper functionality across various scenarios.

* Update changelog
This commit is contained in:
Alex Root Junior 2025-03-01 22:08:14 +02:00 committed by GitHub
parent e622ada2fc
commit 8b4976b3de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 397 additions and 10 deletions

View file

@ -1680,3 +1680,73 @@ class TestSceneInheritance:
assert child_2_handler.handler is _empty_handler
assert child_3_handler.handler is not _empty_handler
def collect_handler_names(scene):
return [handler.handler.__name__ for handler in scene.__scene_config__.handlers]
class TestSceneHandlersOrdering:
def test_correct_ordering(self):
class Scene1(Scene):
@on.message()
async def handler1(self, message: Message) -> None:
pass
@on.message()
async def handler2(self, message: Message) -> None:
pass
class Scene2(Scene):
@on.message()
async def handler2(self, message: Message) -> None:
pass
@on.message()
async def handler1(self, message: Message) -> None:
pass
assert collect_handler_names(Scene1) == ["handler1", "handler2"]
assert collect_handler_names(Scene2) == ["handler2", "handler1"]
def test_inherited_handlers_follow_mro_order(self):
class Scene1(Scene):
@on.message()
async def handler1(self, message: Message) -> None:
pass
@on.message()
async def handler2(self, message: Message) -> None:
pass
class Scene2(Scene1):
@on.message()
async def handler3(self, message: Message) -> None:
pass
@on.message()
async def handler4(self, message: Message) -> None:
pass
assert collect_handler_names(Scene2) == ["handler3", "handler4", "handler1", "handler2"]
def test_inherited_overwrite_follow_mro_order(self):
class Scene1(Scene):
@on.message()
async def handler1(self, message: Message) -> None:
pass
@on.message()
async def handler2(self, message: Message) -> None:
pass
class Scene2(Scene1):
@on.message()
async def handler2(self, message: Message) -> None:
pass
@on.message()
async def handler3(self, message: Message) -> None:
pass
assert collect_handler_names(Scene2) == ["handler3", "handler1", "handler2"]