Skip to content

Infer return type of generic functions based on input #115

@betodealmeida

Description

@betodealmeida

Is it possible to infer the return type based on the input, for generic functions?

Eg, with this classic example:

from typing import TypeVar, Sequence

T = TypeVar('T')      # Declare type variable

def first(seq: Sequence[T]) -> T:   # Generic function
    return seq[0]

If I know that seq has type Sequence[int], how can I infer that first would return int in that case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions