prefect icon indicating copy to clipboard operation
prefect copied to clipboard

Document that args with leading underscores are not deployed

Open felix-ht opened this issue 3 years ago • 2 comments

First check

  • [X] I added a descriptive title to this issue.
  • [X] I used the GitHub search to find a similar request and didn't find it.
  • [X] I searched the Prefect documentation for this feature.

Prefect Version

2.x

Describe the current behavior

I was looking for ways to expose certain args only for testing and not for deployments. I though it might be a good idea to do this with a leading underscore.

@flow
def foo(a: int, _not_deployed="prod_path"):
    pass

# in a test file
def test_foo():
    with prefect_test_harness():
           foo.should_validate_parameters = False
           foo(1, _not_deployed="bar")

def test_foo_other_option():
    with prefect_test_harness():
           @flow
           def test_foo():
               foo.fn(1, _not_deployed="bar")
           test_foo()

While trying to implement this i found that this is already the case - presumably because pydantic strips fields it considers private from the schema. This is great

Describe the proposed behavior

I have two points to of complaint.

No documentation for this behavior (that i could find)

If the parameter with a leading underscore has no default value the flow is still deployed - but the run will always fail because the args with a leading underscore will not be passed (and cannot be set).

TypeError: foo() missing 1 required positional argument: '_not_deployed'

Example Use

No response

Additional context

No response

felix-ht avatar Nov 04 '22 14:11 felix-ht

This is unintentional but a fun find :) I wonder if we should keep this behavior, it seems convenient. If we want to do so, we'll need to error at definition time if it does not have a default value.

zanieb avatar Nov 04 '22 15:11 zanieb

@madkinsz there currently certainly is some quirkiness to it. Enforcing a default would be great!

The ability to inject some parameters for unittests into a flow without having to expose them in the deployment should be a rather common usecase.

Without this the next best option is to use blocks - which can be defined in the prod environment - and saved for test after the harness has been created. But this certainly feels needlessly complicated, especially for cases where you just want to pass a string.

felix-ht avatar Nov 04 '22 17:11 felix-ht

With pydantic>=2.0 a clear and informative error is now raised preventing leading underscores from parameter keyword arguments. I'm going to close this issue as resolved for now - if it becomes a desirable feature to work around Pydantic in these cases we can consider that in a separate enhancement request issue.

cicdw avatar Aug 29 '24 18:08 cicdw