Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
AMDmi3 opened this issue Mar 8, 2019 · 1 comment
Closed

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

AMDmi3 opened this issue Mar 8, 2019 · 1 comment

Comments

@AMDmi3
Copy link

AMDmi3 commented Mar 8, 2019

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()
@emmatyping
Copy link
Collaborator

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants