Skip to content

Remove parent-resolution from wool.routine wrapper — Closes #118#119

Merged
conradbzura merged 4 commits intowool-labs:mainfrom
conradbzura:118-remove-resolve-from-routine
Mar 25, 2026
Merged

Remove parent-resolution from wool.routine wrapper — Closes #118#119
conradbzura merged 4 commits intowool-labs:mainfrom
conradbzura:118-remove-resolve-from-routine

Conversation

@conradbzura
Copy link
Copy Markdown
Contributor

Summary

Remove _resolve() and its associated classmethod/staticmethod descriptor handling from wool.routine. The decorator now operates directly on the raw function passed to it, eliminating the per-invocation sys.modules walk that produced distinct class objects across workers and broke exception identity in nested dispatch scenarios.

@wool.routine now raises ValueError when applied to a classmethod or staticmethod descriptor. The correct decorator order is @classmethod @wool.routine (descriptor outer, routine inner). An info admonition documenting this requirement is added to the routine() docstring.

Closes #118

Proposed changes

Remove _resolve and simplify dispatch internals

_resolve() walked sys.modules[fn.__module__] using fn.__qualname__ to re-resolve the decorated function on every call. On spawned workers the re-imported module lives under __mp_main__, producing a different function object whose globals reference different class objects — breaking isinstance checks and except clauses for user-defined exceptions in nested dispatch.

With _resolve removed, the wrappers pass fn (the original closure-captured function) directly to _dispatch, _stream, and _execute. The parent parameter and the classmethod/staticmethod branch logic in _stream and _execute are also removed since the decorator now always receives the raw function.

Add descriptor guard with clear error message

A new guard at the top of routine() raises ValueError("@wool.routine must be applied before @classmethod/@staticmethod") when passed a descriptor object. This gives an actionable error message instead of the generic "Expected a coroutine function" that would otherwise fire.

Update decorator order in test fixtures and integration routines

All @routine @classmethod / @routine @staticmethod usages in test fixtures and integration routine definitions are flipped to the correct @classmethod @routine / @staticmethod @routine order.

Test cases

# Test Suite Given When Then Coverage Target
1 (module) @routine applied to a classmethod descriptor Decorator is evaluated ValueError with message about correct order Descriptor guard (classmethod)
2 (module) @routine applied to a staticmethod descriptor Decorator is evaluated ValueError with message about correct order Descriptor guard (staticmethod)
3 (module) @routine on various function types with dispatch enabled Decorated function is called Task is dispatched and result returned correctly Dispatch path for all 8 function types
4 (module) @routine on various function types with dispatch disabled Decorated function is called Function executes locally and result returned correctly Local execution path for all 8 function types

The _resolve() helper walked sys.modules to re-resolve the decorated
function on every invocation. In nested dispatch this produced distinct
class objects across workers, causing except clauses to silently miss
re-raised exceptions. Removing _resolve eliminates the class-identity
mismatch and the coupling to __module__ re-importability.

@wool.routine now rejects classmethod/staticmethod descriptors and
requires the correct decorator order (@classmethod @wool.routine).
@conradbzura conradbzura marked this pull request as ready for review March 25, 2026 01:47
@conradbzura conradbzura force-pushed the 118-remove-resolve-from-routine branch from 953ca33 to be8aced Compare March 25, 2026 01:52
Comment thread wool/src/wool/runtime/routine/wrapper.py Outdated
Add an info admonition to the wool.routine docstring explaining that
@classmethod/@staticmethod must be the outer decorator when combined
with @wool.routine, with a code example showing the correct order.
Flip decorator order in test fixtures and integration routines to
match the new requirement (@classmethod/@staticmethod outer,
@wool.routine inner). Add test_routine_with_descriptor_types to
verify the ValueError guard rejects the old ordering.
@conradbzura conradbzura force-pushed the 118-remove-resolve-from-routine branch from be8aced to 5f01704 Compare March 25, 2026 02:39
@conradbzura conradbzura self-assigned this Mar 25, 2026
@conradbzura conradbzura merged commit 3dda2e7 into wool-labs:main Mar 25, 2026
4 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.

Remove parent-resolution from wool.routine wrapper

1 participant