Search code examples
pythontyping

Type hint dictionary where the keys are classes


I have a mapping variable which looks like this:

from datetime import datetime, date, time

default_types_map = {
    datetime: 'DATETIME',
    date: 'DATE',
    time: 'TIME',
    str: 'STRING',
    bool: 'BOOLEAN',
    float: 'FLOAT',
    int: 'INTEGER'
}

I want to create a function which takes that variable as default value, but I don't know how to anotate the type hint of the keys:

from typing import Mapping

def to_schema(types_map: Mapping[???, str]=default_types_map):
    ...

In other words, what should I place in the ??? type hint if all they keys should be classes?


Solution

  • Classes are instances of type, so:

    Mapping[type, str]