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

Deduplicate choices #1994

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
23 changes: 23 additions & 0 deletions python/cog/types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import io
import mimetypes
import os
Expand Down Expand Up @@ -53,6 +54,28 @@ class CogBuildConfig(TypedDict, total=False): # pylint: disable=too-many-ancest
run: Optional[Union[List[str], List[Dict[str, Any]]]]


# The following decorator is used to mutate the definition of choices in the
# case that the value(s) are duplicated. This results in the inability to
# create the enum as the values are not unique. This is generally a hack to
# work around previously created invalid schemas.
def _deduplicate_choices(func: Any) -> Any:
def wrapper(*args: Any, **kwargs: Any) -> Any:
sig = inspect.signature(func)
bound_args = sig.bind(*args, **kwargs)
bound_args.apply_defaults()

if (
"choices" in bound_args.arguments
and bound_args.arguments["choices"] is not None
):
bound_args.arguments["choices"] = list(set(bound_args.arguments["choices"]))

return func(*bound_args.args, **bound_args.kwargs)

return wrapper


@_deduplicate_choices
def Input( # pylint: disable=invalid-name, too-many-arguments
default: Any = ...,
description: str = None,
Expand Down
2 changes: 1 addition & 1 deletion python/tests/server/fixtures/input_choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@


class Predictor(BasePredictor):
def predict(self, text: str = Input(choices=["foo", "bar"])) -> str:
def predict(self, text: str = Input(choices=["foo", "bar", "foo"])) -> str:
assert type(text) == str
return text
Loading