Sitemap
Lynn G. Kwong

Lynn G. Kwong

I’m a Software Developer (https://lynn-kwong.medium.com/) keen on sharing thoughts, tutorials, and solutions for the best practice of software development.

Member-only story

5 Advanced Python Type Hint Features 🚀

6 min readDec 28, 2025
Press enter or click to view image in full size
Image by OpenClipart-Vectors on Pixabay

Python’s type hinting system has come a long way since its introduction in PEP 484. Basic hints like int, str, or list[str] are just the beginning. Modern Python (3.10 and above) offers a vocabulary of more advanced type hints for expressing complex type relationships. These advanced features can make your code more readable and catch more bugs before runtime by leveraging static type checkers. In this article, we’ll explore five important advanced type hint features, using the latest Python 3.10+ syntax. Each section introduces a concept with simple examples to show how and when to use it.

1. Generics with TypeVar (and Generic Types)

Sometimes you want a function or class to work with any type, but still enforce type consistency. This is where type variables and generics come in. TypeVar is used to define a type variable that can represent an arbitrary type. By using a TypeVar, you can write generic functions or classes that adapt to multiple types while retaining type safety.

For example, here’s a generic function that returns the first element of a list of any type:

from typing import TypeVar

T = TypeVar('T') # Declare a type variable

def get_first(items: list[T]) -> T:
return items[0]

print(get_first([10, 20, 30])) # returns 10 (int)…

--

--

Lynn G. Kwong
Lynn G. Kwong

Written by Lynn G. Kwong

I’m a Software Developer (https://lynn-kwong.medium.com/) keen on sharing thoughts, tutorials, and solutions for the best practice of software development.