Skip to content

Instantly share code, notes, and snippets.

@apoorvparijat
Created July 29, 2025 12:38
Show Gist options
  • Select an option

  • Save apoorvparijat/193b287789d52a92f5ca70fd535172a8 to your computer and use it in GitHub Desktop.

Select an option

Save apoorvparijat/193b287789d52a92f5ca70fd535172a8 to your computer and use it in GitHub Desktop.
from typeguard import install_import_hook, TypeguardFinder, config
# COPY-PASTE SOLUTION: Custom TypeguardFinder with Debug Configuration
# Step 1: Configure debug instrumentation
config.debug_instrumentation = True # Set to False to disable debug
# Step 2: Define packages to instrument
packages_to_instrument = [
'grpc.aio',
'grpc.aio._channel',
'grpc.aio._server',
'grpc.aio._utils',
'grpc.aio._interceptor',
'grpc.aio._base_channel',
'grpc.aio._base_server',
'grpc.aio._typing',
'grpc.aio._call',
'grpc.aio._metadata',
# SYNC - Test only grpc._server to confirm it's causing the HandlerCallDetails TypeCheckError
'grpc._server',
]
# Step 3: Create custom TypeguardFinder (optional - customize as needed)
class CustomTypeGuardFinder(TypeguardFinder):
def should_instrument(self, module_name: str) -> bool:
# Custom logic here if needed, or just use parent implementation
result = super().should_instrument(module_name)
if config.debug_instrumentation and result:
print(f"DEBUG: Instrumenting module: {module_name}")
return result
# Step 4: Install import hook with custom finder
install_import_hook(packages_to_instrument, cls=CustomTypeGuardFinder)
@apoorvparijat
Copy link
Author

import logging

from typeguard import install_import_hook, TypeguardFinder, config, ForwardRefPolicy, CollectionCheckStrategy

# COPY-PASTE SOLUTION: Enhanced Debug and Verbosity Configuration

# Step 1: Configure comprehensive debug and verbosity
config.debug_instrumentation = True  # Set to False to disable debug
config.forward_ref_policy = ForwardRefPolicy.WARN  # WARN, ERROR, or IGNORE
config.collection_check_strategy = CollectionCheckStrategy.FIRST_ITEM  # ALL_ITEMS for more thorough checking

# Enable verbose logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('typeguard')
logger.setLevel(logging.DEBUG)

# Step 2: Define packages to instrument
packages_to_instrument = [
    'grpc.aio',
    'grpc.aio._channel',
    'grpc.aio._server',
    'grpc.aio._utils',
    'grpc.aio._interceptor',
    'grpc.aio._base_channel',
    'grpc.aio._base_server',
    'grpc.aio._typing',
    'grpc.aio._call',
    'grpc.aio._metadata',
    # SYNC - Test only grpc._server to confirm it's causing the HandlerCallDetails TypeCheckError
    'grpc._server',
]

# Step 3: Create enhanced custom TypeguardFinder with verbose logging
class VerboseTypeGuardFinder(TypeguardFinder):
    def should_instrument(self, module_name: str) -> bool:
        result = super().should_instrument(module_name)
        if result:
            print(f"🔧 INSTRUMENTING: {module_name}")
            logger.info(f"Instrumenting module: {module_name}")
        else:
            print(f"⏭️  SKIPPING: {module_name}")
            logger.debug(f"Skipping module: {module_name}")
        return result

# Step 4: Install import hook with verbose finder
print("📋 Installing TypeguardFinder with debug enabled...")
print(f"📦 Packages to instrument: {len(packages_to_instrument)}")
for pkg in packages_to_instrument:
    print(f"   • {pkg}")

install_import_hook(packages_to_instrument, cls=VerboseTypeGuardFinder)
print("✅ TypeguardFinder installed successfully!")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment