Skip to content

Python: fix: remove dead code from test_middleware_with_agent.py#5140

Open
Serjbory wants to merge 2 commits intomicrosoft:mainfrom
Serjbory:fix/middleware-pytest-raises-dead-code
Open

Python: fix: remove dead code from test_middleware_with_agent.py#5140
Serjbory wants to merge 2 commits intomicrosoft:mainfrom
Serjbory:fix/middleware-pytest-raises-dead-code

Conversation

@Serjbory
Copy link
Copy Markdown

@Serjbory Serjbory commented Apr 7, 2026

Motivation and Context

Motivation and Context

Three with pytest.raises(...) blocks in test_middleware_with_agent.py contain multiple statements. Because pytest.raises exits immediately after the first exception is caught, all subsequent statements in the block are dead code that never executes. This creates a false sense of test coverage — the await agent.run(...) calls appear to be tested but are actually unreachable.

In all three cases, the Agent(...) constructor is what raises the expected MiddlewareException. The agent.run() calls after it are dead code.

Description

3 locations fixed in test_middleware_with_agent.py:

1. test_decorator_and_type_mismatch (line ~1470)

  • Moved @agent_middleware decorator outside the pytest.raises block (decorators don't raise)
  • Removed unreachable await agent.run(...) after Agent(...) constructor

2. test_neither_decorator_nor_type (line ~1597)

  • Removed unreachable await agent.run(...) after Agent(...) constructor

3. test_insufficient_parameters_error (line ~1606)

  • Moved @agent_middleware decorator outside the pytest.raises block
  • Removed unreachable await agent.run(...) after Agent(...) constructor

All 53 tests in test_middleware_with_agent.py pass after the changes.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

@moonbox3 moonbox3 added the python label Apr 7, 2026
@github-actions github-actions bot changed the title fix: remove dead code from test_middleware_with_agent.py Python: fix: remove dead code from test_middleware_with_agent.py Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants