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

Default value of Type[T]-argument #5802

Closed
galcik opened this issue Oct 19, 2018 · 1 comment
Closed

Default value of Type[T]-argument #5802

galcik opened this issue Oct 19, 2018 · 1 comment

Comments

@galcik
Copy link

galcik commented Oct 19, 2018

from typing import Type, TypeVar

T = TypeVar("T", bound="Base")


class Base(object):
    pass


class Subclass(Base):
    pass


def create_instance(instance_type: Type[T] = Base) -> T:
    return instance_type()

Mypy (0.641) fails with:

Incompatible default for argument "instance_type" (default has type "Type[Base]", argument has type "Type[T]")

Revealed types of return values:

create_instance()  # <nothing>, but I would expect Base*
create_instance(Base)  # Base*, OK
create_instance(Subclass)  # Subclass*, OK
@ilevkivskyi
Copy link
Member

ilevkivskyi commented Oct 19, 2018

@galcik All behaviour you see is expected. What you want is lower bound, while mypy only supports upper bounds for type variables, see discussions at the end of #3737, which I think this issue is a duplicate of.

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