Skip to content

Commit 3f29fd7

Browse files
Warn when activity method is registered without an instance (#1918)
Co-authored-by: Tim Conley <tconley1428@gmail.com>
1 parent 6adc0d8 commit 3f29fd7

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ to include examples, links to docs, or any other relevant information.
2020

2121
### Added
2222

23+
- Worker now warns when an activity method is registered from the class
24+
instead of from an instance (i.e. its first parameter is an unbound `self`).
2325
- `workflow.new_random()` accepts an optional `name` that is mixed into the seed, so differently
2426
named generators, and `workflow.random()`, produce different sequences.
2527

‎temporalio/worker/_activity.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ def __init__(
9595
f"Activity named {defn.name} is a class instead of an instance"
9696
)
9797

98+
# A plain function whose first parameter is "self" is almost always
99+
# a method referenced from the class instead of from an instance
100+
if inspect.isfunction(activity):
101+
params = list(inspect.signature(activity).parameters)
102+
if params and params[0] == "self":
103+
warnings.warn(
104+
f"Activity named {defn.name} has a first parameter named self "
105+
"but is not bound to an instance, did you mean to register it "
106+
"from an instance of the class?"
107+
)
108+
98109
# Some extra requirements for sync functions
99110
if not defn.is_async:
100111
if not activity_executor:

‎tests/worker/test_workflow.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4188,6 +4188,12 @@ async def test_workflow_activity_method(client: Client):
41884188
assert result == MyDataClass(field1="in worker, workflow param, in workflow")
41894189

41904190

4191+
async def test_workflow_activity_method_unbound_register(client: Client):
4192+
# Registering the method from the class instead of from an instance
4193+
with pytest.warns(UserWarning, match="is not bound to an instance"):
4194+
new_worker(client, ActivityMethodWorkflow, activities=[MethodActivity.add])
4195+
4196+
41914197
@workflow.defn
41924198
class WaitConditionTimeoutWorkflow:
41934199
def __init__(self) -> None:

0 commit comments

Comments
 (0)