Skip to content

Incompatible return value type with TypeVar #10003

Closed
@marouane-miftah

Description

@marouane-miftah

mypy=0.800
python=3.8.6

The following code generates
error: Incompatible return value type (got "str", expected "Union[ndarray, T]")

from __future__ import annotations
from typing import TypeVar, Union
import numpy

T = TypeVar("T")

def _test(a: list[T]) -> Union[numpy.ndarray, T]:
    if isinstance(a[0], str):
        return a[0]
    return numpy.array(a)

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