When working with union types in Python, always verify that all components of the union have the attributes you're trying to access or modify. Failure to do so can lead to runtime errors when the code attempts to access undefined attributes.
When working with union types in Python, always verify that all components of the union have the attributes you’re trying to access or modify. Failure to do so can lead to runtime errors when the code attempts to access undefined attributes.
For attribute access on unions:
from typing import Union
class A:
pass # No 'x' attribute
class B:
x: int # Has 'x' attribute
# Unsafe pattern - might cause runtime errors:
def unsafe(obj: Union[A, B]):
obj.x = 42 # Error: possibly-unbound-attribute
# Safe pattern - check type first:
def safe(obj: Union[A, B]):
if isinstance(obj, B):
obj.x = 42 # Safe, we've verified this is type B
# Alternative safe pattern - use hasattr:
def also_safe(obj: Union[A, B]):
if hasattr(obj, "x"):
obj.x = 42 # Safe, we've verified attribute exists
Similarly, when using the del
statement, track deleted variables carefully and avoid accessing them after deletion to prevent “unresolved-reference” errors.
Enter the URL of a public GitHub repository