From 19ca0aa2e95a6fbd07d993e1e248043d13c98c32 Mon Sep 17 00:00:00 2001 From: ishansurdi Date: Tue, 9 Dec 2025 15:10:02 +0530 Subject: [PATCH 1/3] Fix variable shadowing in request_response and websocket_session --- fastapi/routing.py | 8 +-- tests/test_variable_shadowing_fix.py | 93 ++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 tests/test_variable_shadowing_fix.py diff --git a/fastapi/routing.py b/fastapi/routing.py index 9be2b44bc..40a917bbe 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -96,7 +96,7 @@ def request_response( async def app(scope: Scope, receive: Receive, send: Send) -> None: request = Request(scope, receive, send) - async def app(scope: Scope, receive: Receive, send: Send) -> None: + async def inner_app(scope: Scope, receive: Receive, send: Send) -> None: # Starts customization response_awaited = False async with AsyncExitStack() as request_stack: @@ -117,7 +117,7 @@ def request_response( ) # Same as in Starlette - await wrap_app_handling_exceptions(app, request)(scope, receive, send) + await wrap_app_handling_exceptions(inner_app, request)(scope, receive, send) return app @@ -135,7 +135,7 @@ def websocket_session( async def app(scope: Scope, receive: Receive, send: Send) -> None: session = WebSocket(scope, receive=receive, send=send) - async def app(scope: Scope, receive: Receive, send: Send) -> None: + async def inner_app(scope: Scope, receive: Receive, send: Send) -> None: async with AsyncExitStack() as request_stack: scope["fastapi_inner_astack"] = request_stack async with AsyncExitStack() as function_stack: @@ -143,7 +143,7 @@ def websocket_session( await func(session) # Same as in Starlette - await wrap_app_handling_exceptions(app, session)(scope, receive, send) + await wrap_app_handling_exceptions(inner_app, session)(scope, receive, send) return app diff --git a/tests/test_variable_shadowing_fix.py b/tests/test_variable_shadowing_fix.py new file mode 100644 index 000000000..399db1693 --- /dev/null +++ b/tests/test_variable_shadowing_fix.py @@ -0,0 +1,93 @@ +"""Simple verification that the routing.py changes are syntactically correct""" + +import ast +import sys + +def check_syntax(filepath): + """Check if Python file has valid syntax""" + try: + with open(filepath, 'r', encoding='utf-8') as f: + code = f.read() + ast.parse(code) + return True, "Syntax OK" + except SyntaxError as e: + return False, f"Syntax Error: {e}" + +def check_no_shadowing(filepath): + """Check that we fixed the variable shadowing issue""" + with open(filepath, 'r', encoding='utf-8') as f: + content = f.read() + + # Check that inner_app exists (our fix) + if 'async def inner_app(scope: Scope, receive: Receive, send: Send)' in content: + inner_app_count = content.count('async def inner_app(scope: Scope, receive: Receive, send: Send)') + if inner_app_count >= 2: # Should be in both functions + return True, f"✓ Found {inner_app_count} instances of 'inner_app' (expected 2)" + else: + return False, f"✗ Found only {inner_app_count} instances of 'inner_app'" + else: + return False, "✗ 'inner_app' not found - fix not applied" + +def check_wrap_app_handling_exceptions(filepath): + """Check that wrap_app_handling_exceptions uses inner_app not app""" + with open(filepath, 'r', encoding='utf-8') as f: + content = f.read() + + # Check request_response function + lines = content.split('\n') + found_request_response = False + found_correct_usage = False + + for i, line in enumerate(lines): + if 'def request_response(' in line: + found_request_response = True + if found_request_response and 'await wrap_app_handling_exceptions(inner_app, request)' in line: + found_correct_usage = True + break + + if not found_correct_usage: + return False, "✗ wrap_app_handling_exceptions still uses 'app' instead of 'inner_app'" + + return True, "✓ wrap_app_handling_exceptions correctly uses 'inner_app'" + +if __name__ == "__main__": + filepath = "fastapi/routing.py" + + print("=" * 70) + print("Verifying Variable Shadowing Fix in routing.py") + print("=" * 70) + print() + + # Test 1: Syntax check + print("1. Checking Python syntax...") + success, message = check_syntax(filepath) + print(f" {message}") + if not success: + sys.exit(1) + + # Test 2: Check inner_app exists + print("\n2. Checking for 'inner_app' function...") + success, message = check_no_shadowing(filepath) + print(f" {message}") + if not success: + sys.exit(1) + + # Test 3: Check wrap_app_handling_exceptions usage + print("\n3. Checking wrap_app_handling_exceptions usage...") + success, message = check_wrap_app_handling_exceptions(filepath) + print(f" {message}") + if not success: + sys.exit(1) + + print() + print("=" * 70) + print("✅ ALL CHECKS PASSED - Variable shadowing fix is correct!") + print("=" * 70) + print() + print("Summary of changes:") + print(" • Renamed nested 'app' to 'inner_app' in request_response()") + print(" • Renamed nested 'app' to 'inner_app' in websocket_session()") + print(" • Updated wrap_app_handling_exceptions calls to use 'inner_app'") + print() + print("This fix eliminates variable shadowing and improves code clarity") + print("without changing any functionality.") From 7a19e1d0aac7d6c60cee7b61e6ec9ba028574306 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Tue, 9 Dec 2025 09:53:58 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=8E=A8=20Auto=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_variable_shadowing_fix.py | 55 ++++++++++++++++++---------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/tests/test_variable_shadowing_fix.py b/tests/test_variable_shadowing_fix.py index 399db1693..6a840f555 100644 --- a/tests/test_variable_shadowing_fix.py +++ b/tests/test_variable_shadowing_fix.py @@ -3,82 +3,97 @@ import ast import sys + def check_syntax(filepath): """Check if Python file has valid syntax""" try: - with open(filepath, 'r', encoding='utf-8') as f: + with open(filepath, encoding="utf-8") as f: code = f.read() ast.parse(code) return True, "Syntax OK" except SyntaxError as e: return False, f"Syntax Error: {e}" + def check_no_shadowing(filepath): """Check that we fixed the variable shadowing issue""" - with open(filepath, 'r', encoding='utf-8') as f: + with open(filepath, encoding="utf-8") as f: content = f.read() - + # Check that inner_app exists (our fix) - if 'async def inner_app(scope: Scope, receive: Receive, send: Send)' in content: - inner_app_count = content.count('async def inner_app(scope: Scope, receive: Receive, send: Send)') + if "async def inner_app(scope: Scope, receive: Receive, send: Send)" in content: + inner_app_count = content.count( + "async def inner_app(scope: Scope, receive: Receive, send: Send)" + ) if inner_app_count >= 2: # Should be in both functions - return True, f"✓ Found {inner_app_count} instances of 'inner_app' (expected 2)" + return ( + True, + f"✓ Found {inner_app_count} instances of 'inner_app' (expected 2)", + ) else: return False, f"✗ Found only {inner_app_count} instances of 'inner_app'" else: return False, "✗ 'inner_app' not found - fix not applied" + def check_wrap_app_handling_exceptions(filepath): """Check that wrap_app_handling_exceptions uses inner_app not app""" - with open(filepath, 'r', encoding='utf-8') as f: + with open(filepath, encoding="utf-8") as f: content = f.read() - + # Check request_response function - lines = content.split('\n') + lines = content.split("\n") found_request_response = False found_correct_usage = False - + for i, line in enumerate(lines): - if 'def request_response(' in line: + if "def request_response(" in line: found_request_response = True - if found_request_response and 'await wrap_app_handling_exceptions(inner_app, request)' in line: + if ( + found_request_response + and "await wrap_app_handling_exceptions(inner_app, request)" in line + ): found_correct_usage = True break - + if not found_correct_usage: - return False, "✗ wrap_app_handling_exceptions still uses 'app' instead of 'inner_app'" - + return ( + False, + "✗ wrap_app_handling_exceptions still uses 'app' instead of 'inner_app'", + ) + return True, "✓ wrap_app_handling_exceptions correctly uses 'inner_app'" + if __name__ == "__main__": filepath = "fastapi/routing.py" - + print("=" * 70) print("Verifying Variable Shadowing Fix in routing.py") print("=" * 70) print() - + # Test 1: Syntax check print("1. Checking Python syntax...") success, message = check_syntax(filepath) print(f" {message}") if not success: sys.exit(1) - + # Test 2: Check inner_app exists print("\n2. Checking for 'inner_app' function...") success, message = check_no_shadowing(filepath) print(f" {message}") if not success: sys.exit(1) - + # Test 3: Check wrap_app_handling_exceptions usage print("\n3. Checking wrap_app_handling_exceptions usage...") success, message = check_wrap_app_handling_exceptions(filepath) print(f" {message}") if not success: sys.exit(1) - + print() print("=" * 70) print("✅ ALL CHECKS PASSED - Variable shadowing fix is correct!") From e08791c0dc11728910c19d6bf5f10a81c3cac22b Mon Sep 17 00:00:00 2001 From: ishansurdi Date: Tue, 9 Dec 2025 16:27:43 +0530 Subject: [PATCH 3/3] Remove test file as requested --- tests/test_variable_shadowing_fix.py | 93 ---------------------------- 1 file changed, 93 deletions(-) delete mode 100644 tests/test_variable_shadowing_fix.py diff --git a/tests/test_variable_shadowing_fix.py b/tests/test_variable_shadowing_fix.py deleted file mode 100644 index 399db1693..000000000 --- a/tests/test_variable_shadowing_fix.py +++ /dev/null @@ -1,93 +0,0 @@ -"""Simple verification that the routing.py changes are syntactically correct""" - -import ast -import sys - -def check_syntax(filepath): - """Check if Python file has valid syntax""" - try: - with open(filepath, 'r', encoding='utf-8') as f: - code = f.read() - ast.parse(code) - return True, "Syntax OK" - except SyntaxError as e: - return False, f"Syntax Error: {e}" - -def check_no_shadowing(filepath): - """Check that we fixed the variable shadowing issue""" - with open(filepath, 'r', encoding='utf-8') as f: - content = f.read() - - # Check that inner_app exists (our fix) - if 'async def inner_app(scope: Scope, receive: Receive, send: Send)' in content: - inner_app_count = content.count('async def inner_app(scope: Scope, receive: Receive, send: Send)') - if inner_app_count >= 2: # Should be in both functions - return True, f"✓ Found {inner_app_count} instances of 'inner_app' (expected 2)" - else: - return False, f"✗ Found only {inner_app_count} instances of 'inner_app'" - else: - return False, "✗ 'inner_app' not found - fix not applied" - -def check_wrap_app_handling_exceptions(filepath): - """Check that wrap_app_handling_exceptions uses inner_app not app""" - with open(filepath, 'r', encoding='utf-8') as f: - content = f.read() - - # Check request_response function - lines = content.split('\n') - found_request_response = False - found_correct_usage = False - - for i, line in enumerate(lines): - if 'def request_response(' in line: - found_request_response = True - if found_request_response and 'await wrap_app_handling_exceptions(inner_app, request)' in line: - found_correct_usage = True - break - - if not found_correct_usage: - return False, "✗ wrap_app_handling_exceptions still uses 'app' instead of 'inner_app'" - - return True, "✓ wrap_app_handling_exceptions correctly uses 'inner_app'" - -if __name__ == "__main__": - filepath = "fastapi/routing.py" - - print("=" * 70) - print("Verifying Variable Shadowing Fix in routing.py") - print("=" * 70) - print() - - # Test 1: Syntax check - print("1. Checking Python syntax...") - success, message = check_syntax(filepath) - print(f" {message}") - if not success: - sys.exit(1) - - # Test 2: Check inner_app exists - print("\n2. Checking for 'inner_app' function...") - success, message = check_no_shadowing(filepath) - print(f" {message}") - if not success: - sys.exit(1) - - # Test 3: Check wrap_app_handling_exceptions usage - print("\n3. Checking wrap_app_handling_exceptions usage...") - success, message = check_wrap_app_handling_exceptions(filepath) - print(f" {message}") - if not success: - sys.exit(1) - - print() - print("=" * 70) - print("✅ ALL CHECKS PASSED - Variable shadowing fix is correct!") - print("=" * 70) - print() - print("Summary of changes:") - print(" • Renamed nested 'app' to 'inner_app' in request_response()") - print(" • Renamed nested 'app' to 'inner_app' in websocket_session()") - print(" • Updated wrap_app_handling_exceptions calls to use 'inner_app'") - print() - print("This fix eliminates variable shadowing and improves code clarity") - print("without changing any functionality.")