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

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

Closed
hunyadi opened this issue Sep 26, 2023 · 3 comments
Labels
bug mypy got something wrong

Comments

@hunyadi
Copy link

hunyadi commented Sep 26, 2023

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
@hunyadi hunyadi added the bug mypy got something wrong label Sep 26, 2023
@insilications
Copy link

Can confirm this on the same kind of function taking a type argument and returning a tuple with generic type.

@urnest
Copy link
Contributor

urnest commented Jun 17, 2024

looks like this is fixed in master, I'd claim #17235 fixed it, but could be wrong...
... in any case, anyone care to confirm that this test case passes on master? (The fix would be in mypy >1.10.)

@hauntsaninja
Copy link
Collaborator

Yup, this was indeed fixed on master by #17235. Thanks urnest!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

4 participants