palantir / palantir/python-compute-module

[BUG] function_schema_parser does not support type aliases

Open
#50 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
15
Forks
11
PR merge metrics
No merged PRs in 30d

Description

app.py:

@dataclass
class ExternalImageSearchRequest:
    image_ids: list[str]


@dataclass
class ExternalImageSearchResponse:
    result: Optional[external.ImageSearchResponse]
    error: Optional[str]


@function
def external_image_search(_, event: ExternalImageSearchRequest) -> ExternalImageSearchResponse:
    try:
        result, error = external.image_search(config, session, event.image_ids)
        if error is not None:
            return ExternalImageSearchResponse(None, dumps(error))
        return ExternalImageSearchResponse(result, None)
    except Exception as e:
        return ExternalImageSearchResponse(None, dumps(e))

external.py

class ImageSearchResponseValue(TypedDict):
    key_1: str
    key_2: str
    key_3: str
    key_4: str


ImageSearchResponse = dict[str, list[ImageSearchResponseValue]]

trace:

Traceback (most recent call last):
    @function
  File "/app/client/queries/app.py", line 65, in <module>
     ^^^^^^^^
  File "/app/client/run-env/lib/python3.11/site-packages/compute_modules/annotations.py", line 34, in function
    return function_wrapper(maybe_func)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/client/run-env/lib/python3.11/site-packages/compute_modules/annotations.py", line 30, in function_wrapper
    add_function(func, streaming=streaming, edits=edits)
  File "/app/client/run-env/lib/python3.11/site-packages/compute_modules/function_registry/function_registry.py", line 41, in add_function
    parse_result = parse_function_schema(
                   ^^^^^^^^^^^^^^^^^^^^^^
  File "/app/client/run-env/lib/python3.11/site-packages/compute_modules/function_registry/function_schema_parser.py", line 56, in parse_function_schema
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    output = _extract_output(type_hints)
  File "/app/client/run-env/lib/python3.11/site-packages/compute_modules/function_registry/function_schema_parser.py", line 151, in _extract_output
    output_data_type, _ = _extract_data_type(type_hints[RETURN_KEY])
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/client/run-env/lib/python3.11/site-packages/compute_modules/function_registry/function_schema_parser.py", line 300, in _extract_data_type
    custom_type_fields[field_name], child_class_node = _extract_data_type(field_type_hint)
                                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/client/run-env/lib/python3.11/site-packages/compute_modules/function_registry/function_schema_parser.py", line 296, in _extract_data_type
  File "/app/client/run-env/lib/python3.11/site-packages/compute_modules/function_registry/function_schema_parser.py", line 320, in _assert_is_valid_custom_type
    _assert_is_valid_custom_type(type_hint)
    if issubclass(item, dict):
       ^^^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 1 must be a class

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in compute_modules/function_registry/function_schema_parser.py, especially _extract_data_type and _assert_is_valid_custom_type, then reproduce the traceback with the dataclass and type alias shown in the issue. Done means function_schema_parser accepts the aliased ImageSearchResponse annotation without raising TypeError and produces the expected function schema.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.