https://prefect.io logo
#prefect-community
Title
# prefect-community
n

Nick DeCraene

10/06/2022, 4:47 PM
I'm looking to get mypy working with 2.0 and it looks like every task's return value is changed to
None
instead of what is hinted at in the method. Does anyone know of a work-around besides ignoring the errors?
example:
Copy code
@task(retries=5, retry_delay_seconds=30)
def extract(ctx: RunContext, today: Optional[dt.datetime] = None) -> List[int]:

@task()
def get_ctx() -> RunContext:
    return RunContext()

@flow()
def my_flow(date: Optional[dt.dateime]):
    ctx = get_ctx()
    data = extract(ctx, date)
    for d in data:
        do_something(d)
will issue
error: No overload variant of "__call__" of "Task" matches argument types "None", "Optional[datetime]"  [call-overload]
meaning, the call to
extract
is getting a
None
type passed for the ctx arg
r

Rob Freedy

10/07/2022, 1:45 PM
Hey @Nick DeCraene! At first look it looks like you are returning the RunContext object correctly in the get_ctx task. Would you mind sharing that class definition if you have it? Also wanted to mention that since you are getting an object called RunContext(), it might be worth looking into creating a new block for this context and loading it that way? https://docs.prefect.io/concepts/blocks/?h=class#creating-new-block-types
n

Nick DeCraene

10/07/2022, 2:19 PM
ah so that RunContext is just a python dataclass. It is really just an easier way to group different vars that we want to use for different tasks. But now that i say that, i do wonder if we need it anymore, given 2.0 flows are just normal functions. In 1.0 we would use this to pass metadata from one task to the next. I do have another example of mypy having issues with the same scenario but its a
List[Tuple[int, str]]
instead
r

Rob Freedy

10/07/2022, 5:59 PM
Are you able to provide the simple example you are using with the List[Tuple[int,str]] parameter? It should being coerced correctly as long as the type hint is set correctly: https://orion-docs.prefect.io/concepts/flows/#parameters and https://discourse.prefect.io/t/does-prefect-perform-a-type-validation-on-input-parameters/55
n

Nick DeCraene

10/11/2022, 8:35 PM
@Rob Freedy sorry this took a second, here's the other example:
Copy code
@task(retries=3, retry_delay_seconds=10)
def get_cids(
    cid_to_run: Optional[str] = None,
    org_to_run: Optional[int] = None,
    all_orgs: bool = False,
) -> List[Tuple[int, str]]:
    """Get a list cids and org ids to pull data for [(org, cid), (org, cid), ...]"""

    with ro_session_scope() as session:
        info = get_info(session, cid_to_run, org_to_run, all_orgs)
            
    return [(i[0], i[1]) for i in info]


@flow(name="my_flow")
def run_experiments(
    cid_to_run: Optional[str] = None,
    org_to_run: Optional[int] = None,
):
    org_cids: List[Tuple[int, str]] = get_cids(cid_to_run, org_to_run, all_orgs=True)
    # rest of the flow here
get_cids pretty much just grabs some info from a db, but always returns a List[Tuple[int, str]]. The mypy issue occurs when assigning
org_cids
as
None
can't be cast to
List
r

Rob Freedy

10/12/2022, 7:06 PM
are you able to provide the full stack trace of the error you are getting? I have not been able to recreate the error
3 Views