🔍 Code Extractor

function _set_module

Maturity: 20

Internal utility function that sets the __module__ attribute of a type variable-like object to the calling module's name for proper pickling support.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/typing_extensions.py
Lines:
1651 - 1655
Complexity:
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

    Retrieves the module name of the calling function at a specified stack depth, with fallback mechanisms for different Python platforms.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/typing_extensions.py
  • function _set_default 54.0% similar

    Internal utility function that sets default value metadata on a type parameter object by adding a has_default method and __default__ attribute.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/typing_extensions.py
  • function IntVar 48.6% similar

    A wrapper function that creates a TypeVar using Python's typing module, intended to represent integer type variables.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/typing_extensions.py
  • function _has_generic_or_protocol_as_origin 44.1% similar

    Internal function that inspects the call stack to determine if the caller is from typing.py and has Generic or Protocol as the origin parameter.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/typing_extensions.py
  • function _unpack_args 43.8% similar

    Unpacks tuple arguments that have been marked with the __typing_unpacked_tuple_args__ attribute, flattening nested tuple type arguments while preserving non-unpacked arguments.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/typing_extensions.py
← Back to Browse