function _set_module
Internal utility function that sets the __module__ attribute of a type variable-like object to the calling module's name for proper pickling support.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/typing_extensions.py
1651 - 1655
simple
Purpose
This function is used internally by typing_extensions to ensure that dynamically created type variables, type aliases, and similar typing constructs have their __module__ attribute set correctly. This is essential for pickling these objects, as Python's pickle module needs to know which module an object belongs to in order to serialize and deserialize it properly. The function uses stack introspection to determine the caller's module and only sets the attribute if the object wasn't created within the typing_extensions module itself.
Source Code
def _set_module(typevarlike):
# for pickling:
def_mod = _caller(depth=2)
if def_mod != 'typing_extensions':
typevarlike.__module__ = def_mod
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
typevarlike |
- | - | positional_or_keyword |
Parameter Details
typevarlike: A type variable-like object (such as TypeVar, ParamSpec, TypeVarTuple, or similar typing constructs) whose __module__ attribute needs to be set. This object must have a writable __module__ attribute.
Return Value
This function returns None. It modifies the input object in-place by setting its __module__ attribute as a side effect.
Usage Example
# This is an internal function not meant for direct use.
# It's typically called internally when creating type variables:
#
# Example of how it might be used internally:
# from typing import TypeVar
#
# def create_typevar(name):
# tv = TypeVar(name)
# _set_module(tv) # Sets tv.__module__ to caller's module
# return tv
#
# # In user code:
# T = create_typevar('T')
# # T.__module__ would be set to '__main__' or the actual module name
Best Practices
- This is an internal utility function and should not be called directly by end users
- The function assumes the existence of a _caller() helper function that performs stack introspection with a depth parameter
- Only modifies the __module__ attribute if the calling module is not 'typing_extensions' itself
- Used to ensure proper pickling behavior for dynamically created typing constructs
- The depth=2 parameter in _caller() is carefully chosen to skip the immediate caller and get the actual defining module
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function _caller 55.0% similar
-
function _set_default 54.0% similar
-
function IntVar 48.6% similar
-
function _has_generic_or_protocol_as_origin 44.1% similar
-
function _unpack_args 43.8% similar