Skip to content

Explicit TypeVars (e.g. TypeVar("T", int, str)) can't be satisfied from refinement + new construction #241

Open
@fliepeltje

Description

@fliepeltje

When returning a value that fits within the typevar constraint, the checker returns an Incompatible return type [7] error. To reproduce:

T = TypeVar("T", int, str)

def fn(value: T) -> T:
    if isinstance(value, int):
        return int(5)
    else:
        return str("five")

This raises Incompatible return type [7]: Expected Variable[T <: [int, str]] but got int and Incompatible return type [7]: Expected Variable[T <: [int, str]] but got str

This works correctly:

def fn(value: T) -> T:
    return value

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementImprovements and feature requests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions