https://prefect.io logo
Title
a

ale

11/19/2020, 12:54 PM
Hi folks, is there a way to set the expected type for a Parameter? I have a parameter which is a string, but when passing it to tasks, Pycharm raises a warning about type mismatch…
d

Dylan

11/19/2020, 2:48 PM
Hey @ale! When you pass data between tasks in a Prefect Flow, you’re actually passing Task Results (since the actual data is passed at runtime, not build time). That’s likely the source of the pycharm type mismatch. In the task that accepts the parameter, is the type a string at runtime?
a

ale

11/19/2020, 2:52 PM
Yep, the runtime type is a string, so no problems during execution. It’s just somehow annoying during development 😬
d

Dylan

11/19/2020, 3:10 PM
If you think there’s a way to improve the experience, feel free to open an issue with your suggestion! 😄
a

ale

11/19/2020, 3:59 PM
I’ll think about it 🙂 I asked because I was not sure if I was making some mistake when using parameters. Thanks @Dylan 👍
d

Dylan

11/19/2020, 3:59 PM
👍 anytime!
s

Saulius Beinorius

11/20/2020, 4:31 PM
I investigated this a bit (because I saw other libraries like
pydantic
having classes that are able to masquerade as other types) and I came up with this snippet, with the bonus of having a transformation function akin to `argparse`:
from typing import Callable, TypeVar

T = TypeVar("T")

class _Parameter:
    def __init__(self, name: str, transform_function: Callable[[str], T], default: T):
        self.name = name
        self.transform_function = transform_function
        self.default = default

def Parameter(name: str, transform_function: Callable[[str], T], default: T) -> T:
    return _Parameter(name, transform_function, default)

x: float = Parameter("parameter_name", transform_function=lambda s: float(s), default=2.0)
This manages to trick PyCharm, although I'm not sure about
mypy
, that might be more difficult to get through
a

ale

11/20/2020, 4:54 PM
Thanks @Saulius Beinorius!