Skip to content

TypeVar is not resolved correctly with None optional argument #8708

Closed as not planned
@yhlam

Description

@yhlam

Running mypy --no-implicit-optional test.py with the code below gives the error Incompatible default for argument "default" (default has type "None", argument has type "D")

from typing import Sequence, TypeVar, Union

D = TypeVar("D")
E = TypeVar("E")


def get_first(values: Sequence[E], default: D = None) -> Union[E, D]:
    if len(values) == 0:
        return default
    else:
        return values[0]

I would expect mypy doesn't emit any error. The type of default should be resolved to None when the function is called without the optional argument, e.g. get_first([]), so that the return type is Union[E, None], which is equivalent to Optional[E].

I am using mypy 0.770 and python 3.7

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