Skip to content

Weird interaction between TypeVar and union #17193

Open
@tamird

Description

@tamird

See this code.

from typing import TypeVar

T = TypeVar("T", list, dict)

def ctor(cls: type[T]) -> T:
    return cls()
    
def ctor_union(cls: type[list] | type[dict]) -> list | dict:
    return cls()
    
def ctor_union_proxy(cls: type[list] | type[dict]) -> list | dict:
    return ctor(cls)

produces

main.py:12: error: Value of type variable "T" of "ctor" cannot be "Collection[Any]"  [type-var]
main.py:12: error: Incompatible return value type (got "Collection[Any]", expected "list[Any] | dict[Any, Any]")  [return-value]
Found 2 errors in 1 file (checked 1 source file)

possibly duplicate of #9424.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions