Skip to content

Fixed import types#6230

Open
Vessel9817 wants to merge 2 commits intogoogle-ai-edge:masterfrom
Vessel9817:patch-1
Open

Fixed import types#6230
Vessel9817 wants to merge 2 commits intogoogle-ai-edge:masterfrom
Vessel9817:patch-1

Conversation

@Vessel9817
Copy link
Copy Markdown

Fixed import types

Motivation

When detecting landmarks in live stream mode, the asynchronous result callback should be able to be properly typed:

import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision

def _callback(
    detection_result: vision.HandLandmarkerResult,
    image: mp.Image,
    timestamp: int,
) -> None:
    ...

#model_asset_path, max_hands = ...
base_options = python.BaseOptions(model_asset_path=model_asset_path)
options = vision.HandLandmarkerOptions(
    base_options=base_options,
    num_hands=max_hands,
    running_mode=vision.RunningMode.LIVE_STREAM,
    result_callback=_callback
)
detector = vision.HandLandmarker.create_from_options(options)

#img, timestamp = ...
detector.detect_async(img, timestamp)

Instead, because of the way HandLandmarkerResult is exported in mediapipe.tasks.python.vision, its revealed type is Unknown. Since Unknown is not a static type, type checkers consider HandLandmarkerResult to be a variable. This brings us to the actual issue at play:

Variable not allowed in type expression

def _callback(
    detection_result: vision.HandLandmarkerResult, # <-- Here
    image: mp.Image,
    timestamp: int,
) -> None:
    ...

Reproduction

from typing import TypeAlias

from mediapipe.tasks.python import vision

# Expected type: HandLandmarkerResult
# Actual type: Unknown
vision.HandLandmarkerResult

# Expected result: No type error
# Actual result: Variable not allowed in type expression
T: TypeAlias = vision.HandLandmarkerResult

Solution

Used from...import instead of import:

- import a.b.c
+ from a.b import c

c_export = c

del c

Final notes

The vision tasks API module may not be the only occurrence of this issue within the package. Therefore, the solution illustrated in this PR may need to be applied more generally to it in the future.

@google-cla
Copy link
Copy Markdown

google-cla Bot commented Jan 29, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Signed-off-by: Vessel9817 <4i6cusbngdhhc.jn6@gmail.com>
Signed-off-by: Vessel9817 <4i6cusbngdhhc.jn6@gmail.com>
@Vessel9817 Vessel9817 reopened this Jan 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants