Skip to content

Type inference error for function taking type argument and returning list of tuple with generic type #16181

@hunyadi

Description

@hunyadi

Bug Report

mypy prints

Argument 1 to "transform_value" has incompatible type "_T_co"; expected "str"  [arg-type]

when calling a function taking type argument and returning a list of tuple with generic type, and using the de-structuring operator to access one of the tuple members:

def transform_value(value: str) -> str:
    ...

def query_all(signature: type[T], statement: str) -> list[T]:
    ...

rows = query_all(tuple[str, int], "...")
for row in rows:
    first, second = row
    value = transform_value(first)    # <-- type error

To Reproduce

See Gist:

from typing import TypeVar

T = TypeVar("T")


def transform_value(value: str) -> str:
    return value


def query_all(signature: type[T], statement: str) -> list[T]:
    return []


def run_query_1() -> None:
    rows = query_all(tuple[str, int], "...")
    for row in rows:
        first, second = row
        value = transform_value(first)


def run_query_2() -> None:
    rows = query_all(str, "...")
    for row in rows:
        value = transform_value(row)

Actual Behavior

Revealed type for first in run_query_1 is _T_co rather than str:

Argument 1 to "transform_value" has incompatible type "_T_co"; expected "str"  [arg-type]

run_query_2 passes with no error (expected behavior).

Your Environment

Python 3.11.5
mypy 1.5.1 (compiled: yes)
Mypy extension for VS Code

[mypy]
check_untyped_defs = True
disallow_untyped_calls = True
disallow_untyped_decorators = True
disallow_untyped_defs = True
ignore_missing_imports = True
show_column_numbers = True
warn_redundant_casts = True
warn_unused_ignores = True

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions