Skip to content

Default value of Type[T]-argument #5802

Closed
@galcik

Description

@galcik
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

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