Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type hint for "hashable"

Tags:

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?

like image 992
Mark Amery Avatar asked Jun 28 '16 13:06

Mark Amery


People also ask

How do you write a hint tuple?

Code insight: Type Annotations for list, set, tuple, frozenset in Python 3.9 insert from typing import ... Hit Alt+Enter on data and select "Add type hint for variable data ". So Code Insight behaves correctly adding lowercase letter tuple .

How do you write a class hint in Python?

In a type hint, if we specify a type (class), then we mark the variable as containing an instance of that type. To specify that a variable instead contains a type, we need to use type[Cls] (or the old syntax typing. Type ).

What are type hints?

Introduction to Python type hints It means that you need to declare types of variables, parameters, and return values of a function upfront. The predefined types allow the compilers to check the code before compiling and running the program.

How do you use type hints in Python?

Here's how you can add type hints to our function: Add a colon and a data type after each function parameter. Add an arrow ( -> ) and a data type after the function to specify the return data type.

What is the difference between hashable and set and HASHABLE?

Hashable: An object is Hashable if it has a hash value that is the same for the lifetime of the object. Hash table: A data structure that maps keys to values. Hashing algorithm: An algorithm that is used to generate a hash. Set: An unordered collection of values (where all the values are of the same type).

What type hints are supported by this module?

This module supports type hints as specified by PEP 484 and PEP 526. The most fundamental support consists of the types Any, Union, Tuple, Callable, TypeVar, and Generic.

What are the different types of type hints in Java?

This module provides runtime support for type hints. The most fundamental support consists of the types Any, Union, Callable , TypeVar, and Generic. For a full specification, please see PEP 484. For a simplified introduction to type hints, see PEP 483.

Can a strong type inherit from a hash function?

Our purpose will be here to allow any strong type to inherit from the hash function of its underlying type, if it exists. And this functionality should be explicitly asked for when defining the strong type, exactly like the other functionalities inherited from the underlying type.


1 Answers

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'>
like image 151
Mark Amery Avatar answered Sep 28 '22 20:09

Mark Amery