or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/wtfpython@3.0.x
tile.json

tessl/pypi-wtfpython

tessl install tessl/pypi-wtfpython@3.0.0

Educational collection of surprising Python code snippets that demonstrate counter-intuitive behaviors and language internals

Agent Success

Agent success rate when using this tile

93%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.06x

Baseline

Agent success rate without this tile

88%

task.mdevals/scenario-6/

Exception Handler Analyzer

Build a utility that demonstrates Python's exception handling behaviors, specifically exception variable scope and try-finally-return interactions.

@generates

Requirements

Create a Python module that implements two functions to analyze exception handling behavior:

Exception Variable Scope Analysis

Implement a function that demonstrates Python's automatic exception variable deletion after except blocks.

  • It catches an exception and attempts to access the exception variable outside the except block @test
  • It returns information about whether the exception variable is accessible after the except block @test

Finally Block Return Override

Implement a function that shows how finally blocks can override return values.

  • It demonstrates that a return statement in finally overrides a return in try @test
  • It demonstrates that a return statement in finally overrides a return in except @test

API

def analyze_exception_scope():
    """
    Analyzes Python's exception variable deletion behavior.

    Catches an exception in an except block, then attempts to access
    the exception variable outside the except block.

    Returns:
        dict: A dictionary containing:
            - 'caught': bool - whether an exception was caught
            - 'accessible': bool - whether the variable is accessible outside except
            - 'error_type': str or None - name of error raised when accessing variable
    """
    pass

def test_finally_return(trigger_exception: bool):
    """
    Demonstrates how finally block return values override try/except returns.

    Args:
        trigger_exception: If True, raises an exception to test except path;
                          if False, tests the normal try path

    Returns:
        str: The actual return value (always 'finally' due to override behavior)
    """
    pass

Dependencies { .dependencies }

wtfpython { .dependency }

Provides educational examples of Python's surprising behaviors.

Implementation Notes

The module should clearly demonstrate Python's automatic exception variable cleanup (to prevent reference cycles) and the finally block's ability to override return values from try or except blocks. These behaviors are often surprising to developers unfamiliar with Python's exception handling internals.