Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to type hint a generic numeric type in Python?

Forgive me if this question has been asked before but I could not find any related answer.

Consider a function that takes a numerical type as input parameter:

def foo(a):     return ((a+1)*2)**4; 

This works with integers, floats and complex numbers.

Is there a basic type so that I can do a type hinting (of a real existing type/base class), such as:

def foo(a: numeric):     return ((a+1)*2)**4; 

Furthermore I need to use this in a collection type parameter, such as:

from typing import Collection; def foo(_in: Collection[numeric]):     return ((_in[0]+_in[1])*2)**4; 
like image 525
Francesco Boi Avatar asked Mar 10 '20 11:03

Francesco Boi


People also ask

Does Python have type hints?

Python's type hints provide you with optional static typing to leverage the best of both static and dynamic typing. Besides the str type, you can use other built-in types such as int , float , bool , and bytes for type hintings. To check the syntax for type hints, you need to use a static type checker tool.

What is generic in typing Python?

Generic is a programming library for Python that provides tools for generic programming. By now, there is only one feature – multiple dispatch.

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 .

What are 4 different numerical types in Python?

Python provides four basic types of numbers: plain integers, long integers, floating point numbers and complex numbers.

When to use generics in Python?

Using Generics in Python. If you are using type hints in Python… | by SteveYeah | Medium If you are using type hints in Python and static analysis tools such as mypy, then you have probably used the Any type quite often to get around typing functions and methods where the type of an argument can vary at runtime.

How to set type hints for multiple types in Python?

To set type hints for multiple types, you can use Union from the typing module. Second, use the Union to create a union type that includes int and float: Starting from Python 3.10, you can use the X | Y syntax to create a union type, for example: Python allows you to assign an alias to a type and use the alias for type hintings. For example:

How to check for type-hints in Python?

The Type-Hint is completely ignored by the Python interpreter. So, if we run this code again, we still get the same error. So, we have to use a static type checker that analyzes our code and tries to detect if we are violating our Type-Hints or not. The best known type checker is “ mypy “. We can install it by simply using pip .

How do I add a numeric type to NumPy?

There isn't a generic numeric type in the typing module, so you would have to create such a type with Union instead: from typing import Union numeric = Union [int, float, complex] ... To add support for Numpy's collection of numeric types, add np.number to that Union.


1 Answers

PEP 3141 added abstract base classes for numbers, so you could use:

from numbers import Number  def foo(a: Number) -> Number:     ... 
like image 87
juanpa.arrivillaga Avatar answered Sep 20 '22 10:09

juanpa.arrivillaga