
Can type bounds/constraints be stored in global variables and later used in generic function definitions with Python 3.14+?
"I would love to address these questions publicly because the snippets used could be very misleading. Source - https://stackoverflow.com/q/79917832 Posted by Rodrigo Torres Retrieved 2026-03-31, License - CC BY-SA 4.0 # in types.py type Scalar = bool | int | str | float SCALAR_TYPES = ( bool , int , str , float ) Yes, it works, but only part of what you’re doing is actually “real” typing, and that distinction matters. Here is a clean breakdown on your snippest What's really solid and correct This is fully valid and aligns with modern typing (PEP 695 style): type Scalar = bool | int | str | float ` def extract_scalar [ T : Scalar ]( data : dict , key : str , expected : type [ T ]) -> T : ... Kindly note that Scalar is a proper type alias and T: Scalar is a bound That is my recommended approach Let look at What works, but is a bit misleading SCALAR_TYPES = ( bool , int , str , float ) def extract_scalar [ T : SCALAR_TYPES ](...) -> T : ... Yes, this runs. Yes, type checkers may accept it
Continue reading on Dev.to Python
Opens in a new tab

