mypy: function accepting type, returning instance

I recently ended up with a function that looks a bit like this (most of the logic was removed for sake of example):

def build(record, **kwargs):
    # Pretend we're doing stuff with the **kwargs here..
    return record(**kwargs)

Where record could be one of several classes. All we care about is, the function accepts a type as first argument, and returns an instance of that type.

The problem

At first, the most logical way to describe this to mypy was to use a TypeVar like this:

from typing import Any, TypeVar, Type

T = TypeVar('T')

def build(record: Type ...

Page 1 / 1