Skip to content

Generics inference fails for @contextmanagers with TypeVars. #2030

@amol-mandhane

Description

@amol-mandhane

Summary

Repro:

from contextlib import contextmanager
from collections.abc import Generator

class Base:
    pass

class Derived(Base):
    pass


def factory[T: Base](cls: type[T]) -> T:
    return cls()

d = factory(Derived)

@contextmanager
def yielder[T: Base](cls: type[T]) -> Generator[T]:
    yield cls()

with yielder(Derived) as d:  # error [invalid-argument-type] "Argument is incorrect: Expected `type[T@yielder]`, found `<class 'Derived'>`! 
    print(d)

https://play.ty.dev/b74578ac-adaf-4c71-a316-e813c7ad701c

Calling yielder(Derived) should infer T as Derived (since Derived is a subclass of Base), matching the behavior of the regular function factory(Derived).

Metadata

Metadata

Assignees

No one assigned

    Labels

    genericsBugs or features relating to ty's generics implementation

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions