Skip to content

Incorrect handling of default arguments for type Type with Typevars #6525

Closed

Description

@AMDmi3

Consider a function which takes type and returns instance of that type. This is handled correctly by mypy:

from typing import Type, TypeVar

MyType = TypeVar('MyType', str, int)

def func(t: Type[MyType]) -> MyType:
    return t()

However, if I set default argument for t, mypy fails:

from typing import Type, TypeVar

MyType = TypeVar('MyType', str, int)

# error: Incompatible default for argument "t" (default has type "Type[str]", argument has type "Type[int]")
def func(t: Type[MyType] = str) -> MyType:
    return t()
from typing import Type, TypeVar

MyType = TypeVar('MyType', str, int)

# error: Incompatible default for argument "t" (default has type "Type[int]", argument has type "Type[str]")
def func(t: Type[MyType] = int) -> MyType:
    return t()
from typing import Type, TypeVar

MyType = TypeVar('MyType')

# error: Incompatible default for argument "t" (default has type "Type[int]", argument has type "Type[MyType]")
def func(t: Type[MyType] = int) -> MyType:
    return t()

IMO that's a bug. Without TypeVar it works correctly though:

from typing import Type, TypeVar, Union

def func(t: Type[Union[str, int]] = str) -> Union[str, int]:
    return t()

Activity

  1. emmatyping commented on Mar 8, 2019

    @emmatyping
    Member

    This is a duplicate of #4236. Please make sure to search the issue tracker when reporting bugs (using a couple of words such as "default" and "generic"/"typevar" then scanning through the listed issues works well).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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