Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the benefit to using typing library classes vs. types in Python type hints?

I'm starting to learn type hints in Python to ease the future porting of code from Python to C. I am wondering what the difference is, if any, between using types directly in type hints vs. using the classes defined in the typing module.

For example, the difference between

def somefn(a: list[int]) -> tuple[str, int]:
    ...

and

from typing import List, Tuple

def somefn(a: List[int]) -> Tuple[str, int]:
    ...

It seems like some classes like Union, Any, Callable, and Iterable would be useful, but the utility of classes for data types that already exist as keywords in python is unclear to me.

like image 218
Ian Kent Avatar asked Dec 06 '17 21:12

Ian Kent


People also ask

What are reasons for using type hinting in Python?

Type hints improve IDEs and linters. They make it much easier to statically reason about your code. Type hints help you build and maintain a cleaner architecture. The act of writing type hints forces you to think about the types in your program.

What is typing library in Python?

Typing defines a standard notation for Python function and variable type annotations. The notation can be used for documenting code in a concise, standard format, and it has been designed to also be used by static and runtime type checkers, static analyzers, IDEs and other tools.

Do type hints slow down Python?

Type hints and annotations do provide attributes (see typing. get_type_hints ) that can be passed by 3rd party tools but native CPython will not type check these at runtime, so this should not affect the code performance significantly in the same way that comments don't.

Which type of typing is identified with Python?

Python will always remain a dynamically typed language. However, PEP 484 introduced type hints, which make it possible to also do static type checking of Python code. Unlike how types work in most other statically typed languages, type hints by themselves don't cause Python to enforce types.


1 Answers

It's useful for writing annotations that can be legally evaluated; list[int] will explode if you try to actually run it, while typing.List[int] returns a new generic type that knows the type of both container and contents. This is particularly important in the case of type aliases, where a specialized version of a generic is defined at the top level, then reused as annotation further on:

Vector = List[float]
def scale(scalar: float, vector: Vector) -> Vector:
     ...

is legal, while:

Vector = list[float]
def scale(scalar: float, vector: Vector) -> Vector:
     ...

blows up in your face. You'll note that non-container/generic types typically don't have a typing type (exceptions like Text are for porting concerns), because type aliases would only use them as "leaf" type, not root or branch types.

Update: As of 3.9, the standard collections can serve as type hints, so the typing types aren't strictly necessary; you can continue to use them if you like (necessary for code that must run on pre-3.9 Python), but it's not necessary if you can rely on 3.9+.

like image 114
ShadowRanger Avatar answered Sep 25 '22 12:09

ShadowRanger