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

[fix] Validate interface variable names are python-valid #2538

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions flytekit/core/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def __init__(
self._inputs: Union[Dict[str, Tuple[Type, Any]], Dict[str, Type]] = {} # type: ignore
if inputs:
for k, v in inputs.items():
if not k.isidentifier():
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice. I was about to mention isidentifier. 😄

raise ValueError(f"Input name must be valid Python identifier: {k!r}")
if type(v) is tuple and len(cast(Tuple, v)) > 1:
self._inputs[k] = v # type: ignore
else:
Expand Down
10 changes: 10 additions & 0 deletions tests/flytekit/unit/core/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,16 @@ def z(a: int, b: str) -> typing.NamedTuple("NT", x_str=str, y_int=int):
assert typed_interface.outputs.get("y_int").description == "description for y_int"


def test_init_interface_with_invalid_parameters():
from flytekit.core.interface import Interface

with pytest.raises(ValueError, match=r"Input name must be valid Python identifier:"):
_ = Interface({"my.input": int}, {})

with pytest.raises(ValueError, match=r"Type names and field names must be valid identifiers:"):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The error message is odd here, but it is the standard error message from collections.namedtuple see: https://github.com/flyteorg/flytekit/pull/2538/files#r1657915913 https://github.com/python/cpython/blob/main/Lib/collections/__init__.py#L401-L402

_ = Interface({}, {"my.output": int})


def test_parameter_change_to_pickle_type():
ctx = context_manager.FlyteContext.current_context()

Expand Down
Loading