Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling for positional only and keyword only arguments when parsing a function signature from source code #13774

Merged
merged 1 commit into from
Jun 4, 2024

Conversation

desertaxle
Copy link
Member

@desertaxle desertaxle commented Jun 4, 2024

Updates prefect. utilities.callables.parameter_schema_from_entrypoint to correctly handle positional-only and keyword-only arguments. Previously, these types of arguments would be left out from the generated signature.

Closes #13709

Example

See the following tests for examples:

def test_function_with_kwargs_only(tmp_path: Path):
    source_code = dedent(
        """
    def f(
        *,
        x: int = 42,
    ):
        pass
    """
    )

    tmp_path.joinpath("test.py").write_text(source_code)
    schema = callables.parameter_schema_from_entrypoint(f"{tmp_path}/test.py:f")
    assert schema.model_dump_for_openapi() == {
        "properties": {
            "x": {"title": "x", "position": 0, "type": "integer", "default": 42}
        },
        "title": "Parameters",
        "type": "object",
        "definitions": {},
    }

def test_function_with_positional_only_args(tmp_path: Path):
    source_code = dedent(
        """
    def f(x=1, /, y=2, z=3):
        pass
    """
    )

    tmp_path.joinpath("test.py").write_text(source_code)
    schema = callables.parameter_schema_from_entrypoint(f"{tmp_path}/test.py:f")
    assert schema.model_dump_for_openapi() == {
        "properties": {
            "x": {"title": "x", "position": 0, "default": 1},
            "y": {"title": "y", "position": 1, "default": 2},
            "z": {"title": "z", "position": 2, "default": 3},
        },
        "title": "Parameters",
        "type": "object",
        "definitions": {},
    }

Checklist

  • This pull request references any related issue by including "closes <link to issue>"
    • If no issue exists and your change is not a small fix, please create an issue first.
  • If this pull request adds new functionality, it includes unit tests that cover the changes
  • This pull request includes a label categorizing the change e.g. maintenance, fix, feature, enhancement, docs.

For documentation changes:

  • This pull request includes redirect settings in mint.json for files that are removed or renamed.

For new functions or classes in the Python SDK:

  • This pull request includes helpful docstrings.
  • If a new Python file was added, this pull request contains a stub page in the Python SDK docs and an entry in docs/mint.json navigation.

@desertaxle desertaxle added the fix A fix for a bug in an existing feature label Jun 4, 2024
@desertaxle desertaxle changed the title Add handling for positional only and keyword only arguments when pars… Add handling for positional only and keyword only arguments when parsing a function signature from source code Jun 4, 2024
@desertaxle desertaxle marked this pull request as ready for review June 4, 2024 14:55
@desertaxle desertaxle requested a review from a team as a code owner June 4, 2024 14:55
Copy link
Collaborator

@zzstoatzz zzstoatzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@desertaxle desertaxle merged commit 260fc02 into main Jun 4, 2024
27 of 29 checks passed
@desertaxle desertaxle deleted the fix/13709 branch June 4, 2024 15:57
desertaxle added a commit that referenced this pull request Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix A fix for a bug in an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Parameter schema generation regression in 2.19.3 for keyword-only flows
2 participants