Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SingleParam types for pyright checking #471

Merged
merged 1 commit into from
Feb 13, 2024

Conversation

Holi0317
Copy link
Contributor

What was changed

Explicitly specify __arg is a position argument in Method*SingleParam types.

Why?

After updating pyright from 1.1.338 to 1.1.348 (following pylance release cycle), type check and inference related to client.execute_workflow and workflow.execute_activity are failing because of parameter name mismatch with __arg. Causing pyright rejecting the correct overloads.

This can be reproduced with the code sample in the collapsed block. Trying to guide pyright with type hinting on MethodAsyncSingleParam shows why the overload got rejected. The problem is "Parameter name mismatch"

image

I guess python sdk won't use the method types by keyword but only use them with position argument. So this change signal the sdk's intent to type checker.

After adding the last / in the type pyright is passing.

image
Sample
import os
import time
from dataclasses import dataclass
from datetime import timedelta

from temporalio import activity, workflow
from temporalio.client import Client
from temporalio.types import MethodAsyncSingleParam


@dataclass
class ComposeGreetingInput:
    greeting: str
    name: str


@activity.defn
def compose_greeting(input: ComposeGreetingInput) -> str:
    # We'll wait for 3 seconds, heartbeating in between (like all long-running
    # activities should do), then return the greeting
    for _ in range(0, 3):
        print(f"Heartbeating activity on PID {os.getpid()}")
        activity.heartbeat()
        time.sleep(1)
    return f"{input.greeting}, {input.name}!"


@workflow.defn
class GreetingWorkflow:
    @workflow.run
    async def run(self, name: str) -> str:
        return await workflow.execute_activity(
            compose_greeting,
            ComposeGreetingInput("Hello", name),
            start_to_close_timeout=timedelta(seconds=10),
            # Always set a heartbeat timeout for long-running activities
            heartbeat_timeout=timedelta(seconds=2),
        )


async def main():
    # Start client
    client = await Client.connect("localhost:7233")

    wf_run: MethodAsyncSingleParam = GreetingWorkflow.run

    # While the worker is running, use the client to run the workflow and
    # print out its result. Note, in many production setups, the client
    # would be in a completely separate process from the worker.
    await client.execute_workflow(
        GreetingWorkflow.run,
        "World",
        id="hello-activity-multiprocess-workflow-id",
        task_queue="hello-activity-multiprocess-task-queue",
    )

Checklist

  1. How was this tested:

I guess testing this within the repo relies on #420? If this passes mypy on CI this PR should be good enough...?

@Holi0317 Holi0317 requested a review from a team as a code owner February 13, 2024 21:03
@CLAassistant
Copy link

CLAassistant commented Feb 13, 2024

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@Holi0317
Copy link
Contributor Author

Ok tested with my company's project. After upgrading pyright and apply this patch locally, the errors caused by upgrading pyright and temporalio goes away.

Copy link
Member

@cretz cretz left a comment

Choose a reason for hiding this comment

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

Thanks!

@cretz cretz merged commit 8dce5b5 into temporalio:main Feb 13, 2024
12 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.

None yet

3 participants