Hi all, is there any update on the topic of using ...
# ask-metaflow
c
Hi all, is there any update on the topic of using mypy/types in metaflow? There have been multiple conversations on the topic but nothing for the past couple years afaict. Context: • https://github.com/Netflix/metaflow/issues/473https://outerbounds-community.slack.com/archives/C020U025QJK/p1668330213397169
I would argue that in 2021 this was debatable but in 2024 it's quite unusual to not use typing / mypy stubs.
s
🚀 2
b
it's in progress, see https://github.com/Netflix/metaflow/pull/1557 and https://github.com/Netflix/metaflow/pull/1682 most probably early next week... CC: @dry-beach-38304
❤️ 1
c
amazing, thanks!!
d
yep — it was more complicated than anticipated but it’s pretty cool 🙂 (particularly if you use pyright/pylance/mypy)
👏 3
c
looking forward to it!
Thanks for your work here! I upgraded from
metaflow==2.10
to
metaflow[stubs]==2.11.5
to try out the new mypy stubs, and I'm running into some new problems. E.g., I have a function that's annotated to return a
metaflow.Run
and mypy gives an error saying it's actually returning a
metaflow.client.core.Run
. Shall I open a Github issue for this or is there a quick fix (beyond changing all my imports to come from
metaflow.client.core
)?
d
what’s the code and error?
(as in, full code, what yoy are doing to get the error and error)
the
metaflow.Run
object is an alias for
metaflow.client.core.Run
so it should ideally not complain 🙂
c
something like this:
Copy code
from metaflow import (  # type: ignore
    Flow,
    FlowSpec,
    Parameter,
    Run,
    metadata,
    namespace,
)

def get_runs_by_inference_date(inference_date: date) -> tuple[Run, Run]:
    """For a given inference date, return the relevant training and inference runs.

    :param model_enum: the model
    :param inference_date: the inference date to get prod runs for
    :return: (Training run, Inference run)
    """
    namespace(None)

    # Inference run
    inference_runs = Flow("Inference").runs(
        "project_branch:prod.stage",
        f"flow-code:{FLOW_CODE}",
    )
    inference_runs_on_date = [x for x in inference_runs if x.created_at.date() == inference_date]
    assert len(inference_runs_on_date) > 0, "no inference runs located"
    assert len(inference_runs_on_date) == 1, "multiple inference runs located"
    inference_run = inference_runs_on_date[0]

    training_runs = list(
        Flow("Training").runs(
            f"model-id:{inference_run.data.model_id}",  # type: ignore
        )
    )
    assert len(training_runs) > 0, "no training runs located"
    assert len(training_runs) == 1, "multiple training runs located"
    training_run = training_runs[0]

    return training_run, inference_run
Copy code
error: Incompatible return value type (got "tuple[metaflow.client.core.Run, metaflow.client.core.Run]", expected "tuple[metaflow.Run, metaflow.Run]")  [return-value]
https://outerbounds-community.slack.com/archives/C02116BBNTU/p1710869001571979?thread_ts=1705612588.102529&cid=C02116BBNTU yes, that's what I thought, so I assumed it's some esoteric thing about mypy but I'm not knowledgable enough to say. But I know, e.g., pandas does something similar (
pd.DataFrame
is an alias to some deeper level module) but mypy doesn't complain about type hints using
pd.DataFrame
.
d
I’ll take a look why it’s complaining like that and see if I can improve it.
🙏 1
thankyou 1
c
another one (please lmk if you'd prefer me to log these elsewhere): Using the decorator
Copy code
@kubernetes(image=KUBERNETES_IMAGE, node_selector=KUBERNETES_NODE_SELECTOR)
gives
Copy code
error: Unexpected keyword argument "node_selector" for "kubernetes"  [call-arg]
even though that is a valid arg.
another common one is
Copy code
"MetaflowData" has no attribute <...>
when trying to access any artifact
d
that last one I won’t be able to fix — the values are dynamic so I can’t really generate stubs for it. For the previous one cc @brainy-truck-72938
👍 1