214. Type Erasure in Python

🔹 1. Type Hints Are Erased at Runtime

from typing import List

def process_data(data: List[int]):
    print(f"Received: {data}")

print(process_data.__annotations__)
# Output: {'data': typing.List[int]} (Only visible in annotations, not enforced)

🔍 Key Takeaway:

  • List[int] does not exist at runtime—only List is used.

  • Type hints are stored in __annotations__, but Python does not enforce them.


🔹 2. Generic Type Arguments Are Not Retained

from typing import List

lst: List[int] = [1, 2, 3]

print(type(lst))  # <class 'list'> (No int information retained)

🔍 Key Takeaway:

  • List[int] becomes just list at runtime—type information is erased.


🔹 3. Checking If a Variable is a List (Without Type Info)

🔍 Key Takeaway:

  • List[int] is completely erased at runtime, so we only check for list.


🔹 4. Type Erasure in Function Parameters

🔍 Key Takeaway:

  • Even though List[int] is specified, Python allows List[str] at runtime.


🔹 5. Type Erasure with Custom Generics

🔍 Key Takeaway:

  • Generic parameters (T) are erased, so Box[int] is just Box at runtime.


🔹 6. Type Annotations vs. Actual Behavior

🔍 Key Takeaway:

  • Python does not enforce type hints at runtime; they are just for static checks.


🔹 7. Checking a Type Using __orig_bases__

🔍 Key Takeaway:

  • While type arguments are erased, they can be accessed using __orig_bases__.


🔹 8. Type Erasure in Subclassing

🔍 Key Takeaway:

  • List[int] cannot be used with isinstance(), as it's erased at runtime.


🔹 9. Recovering Type Information Using get_type_hints()

🔍 Key Takeaway:

  • get_type_hints() lets us recover erased type information.


🔹 10. Type Checking Using isinstance() with typing Types (Fails)

🔍 Key Takeaway:

  • List[int] cannot be used in isinstance() checks—only list works.


🚀 Summary: Type Erasure in Python

Concept

Description

Type Hints Are Ignored at Runtime

Python does not enforce type hints during execution.

Generic Types Become Their Base Type

List[int] becomes just list.

No Type Checking for Function Arguments

Python allows mismatched types at runtime.

Type Information Can Be Retrieved

Use __annotations__ or get_type_hints().

Cannot Use isinstance() with List[int]

It raises an error—use list instead.


🚀 Best Practices

Use Type Hints for Readability, Not EnforcementUse Static Type Checkers (e.g., mypy) for Catching ErrorsRemember That Generics Are Erased at RuntimeUse get_type_hints() for Type Introspection

Last updated