Search code examples
pythonpython-typing

Type hint for "hashable"


Sometimes an argument to a function I write can be of any type as long as it is a hashable - for instance, because my function adds it to a set or uses it as a dictionary key.

Is there a way to type-hint this fact using the PEP 484 type hints introduced in Python 3.5? The typing module doesn't seem to include a hashable type, but is there some other way?


Solution

  • The typing module does in fact contain a Hashable type (now documented). It's an alias for collections.abc.Hashable.

    >>> import typing
    >>> typing.Hashable
    <class 'collections.abc.Hashable'>