Hi folks, is there a way to set the expected type ...
# prefect-server
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…
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?
Yep, the runtime type is a string, so no problems during execution. It’s just somehow annoying during development 😬
If you think there’s a way to improve the experience, feel free to open an issue with your suggestion! 😄
I’ll think about it 🙂 I asked because I was not sure if I was making some mistake when using parameters. Thanks @Dylan 👍
👍 anytime!
I investigated this a bit (because I saw other libraries like
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`:
Copy code
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
, that might be more difficult to get through
Thanks @Saulius Beinorius!