function _unpack_args
Unpacks tuple arguments that have been marked with the __typing_unpacked_tuple_args__ attribute, flattening nested tuple type arguments while preserving non-unpacked arguments.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/typing_extensions.py
2529 - 2537
simple
Purpose
This function is used internally in Python's typing system to handle unpacked tuple type arguments (using the * operator in type hints). It processes a variable number of arguments, checking each for the special __typing_unpacked_tuple_args__ attribute. If found and the tuple doesn't end with ellipsis (...), it extends the result list with the unpacked subargs; otherwise, it appends the argument as-is. This is crucial for handling variadic generics and TypeVarTuple unpacking in type annotations.
Source Code
def _unpack_args(*args):
newargs = []
for arg in args:
subargs = getattr(arg, '__typing_unpacked_tuple_args__', None)
if subargs is not None and (not (subargs and subargs[-1] is ...)):
newargs.extend(subargs)
else:
newargs.append(arg)
return newargs
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
*args |
- | - | var_positional |
Parameter Details
*args: Variable number of arguments to be processed. These can be any Python objects, but the function specifically looks for objects with the __typing_unpacked_tuple_args__ attribute, which is typically set on special typing constructs that represent unpacked tuple types. Arguments without this attribute are passed through unchanged.
Return Value
Returns a list containing the processed arguments. Arguments with __typing_unpacked_tuple_args__ (that don't end with ellipsis) are unpacked and their elements are added individually to the list. All other arguments are added as single elements. The return type is list, containing a flattened version of the input arguments.
Usage Example
# This is an internal typing utility function, typically not called directly by users.
# Example of how it might be used internally:
class MockUnpackedTuple:
def __init__(self, args):
self.__typing_unpacked_tuple_args__ = args
# Create mock objects with unpacked tuple args
arg1 = MockUnpackedTuple([int, str, float])
arg2 = bool
arg3 = MockUnpackedTuple([list, dict])
# Call the function
result = _unpack_args(arg1, arg2, arg3)
# Result would be: [int, str, float, bool, list, dict]
# Example with ellipsis (not unpacked)
arg_with_ellipsis = MockUnpackedTuple([int, str, ...])
result2 = _unpack_args(arg_with_ellipsis, bool)
# Result would be: [<MockUnpackedTuple object>, bool]
Best Practices
- This is an internal function from Python's typing module and should generally not be called directly by user code
- The function is designed to work with typing system internals, specifically objects that have the __typing_unpacked_tuple_args__ attribute
- The function preserves arguments that end with ellipsis (...) without unpacking them, as ellipsis indicates an unbounded tuple type
- When implementing custom typing constructs, ensure the __typing_unpacked_tuple_args__ attribute is properly set if you want the arguments to be unpacked by this function
- The function returns a new list and does not modify the input arguments
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function _is_unpacked_typevartuple 69.1% similar
-
function IntVar 51.5% similar
-
function _set_module 43.8% similar
-
function _concatenate_getitem 43.2% similar
-
function _create_concatenate_alias 42.6% similar