Skip to content

Fix Ruff B017: replace blind pytest.raises(Exception) with specific Pandera exception - #1

Draft
neel26parekh with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-ci-failure-ruff-b017
Draft

Fix Ruff B017: replace blind pytest.raises(Exception) with specific Pandera exception#1
neel26parekh with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-ci-failure-ruff-b017

Conversation

Copilot AI commented Mar 10, 2026

Copy link
Copy Markdown

CI lint step was failing on B017 Do not assert blind exception: Exception in three tests in tests/test_validation.py. Replace the overly broad pytest.raises(Exception) with the concrete exception type raised by the Pandera validation layer.

Changes

  • tests/test_validation.py
    • Import pandera.errors.SchemaError
    • Replace pytest.raises(Exception) with pytest.raises(SchemaError) in test_invalid_ram_fails, test_invalid_weight_fails, and test_negative_price_fails
    • Fix import sort order (Ruff I001)
# before
with pytest.raises(Exception):
    validate_training_data(df)

# after
from pandera.errors import SchemaError
...
with pytest.raises(SchemaError):
    validate_training_data(df)
Original prompt

Fix CI failure caused by Ruff lint rule B017 in tests.

Context:

  • CI workflow at .github/workflows/ci.yml runs python -m ruff check src/ tests/.
  • Job logs show Ruff error B017 Do not assert blind exception: Exception in tests/test_validation.py at lines 37, 45, and 53.

Task:

  1. Update tests/test_validation.py to avoid pytest.raises(Exception).
  2. Replace those assertions with the specific exception type raised by the validation layer (project uses pandera), typically pandera.errors.SchemaError or pandera.errors.SchemaErrors.
  3. Add the appropriate import(s) and update the three failing tests (test_invalid_ram_fails, test_invalid_weight_fails, test_negative_price_fails).
  4. Ensure Ruff passes for these tests under the existing configuration in pyproject.toml.

Repo: neel26parekh/cost_estimation_network
Ref that showed failure: b281769

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: Please find a solution for this failing job. Use the logs, job definition, and any referenced files where the failure occurred. Keep your response focused on the solution and include code suggestions when appropriate.

Assistant: The job is failing in the Lint step of your CI workflow, not in pytest. Your workflow runs:

  • python -m ruff check src/ tests/ (from .github/workflows/ci.yml at ref b2817693c0a2ee0e7c7e4d98c926e939b7c1ed5c, lines 19–22)

From the logs, Ruff reports B017 in tests/test_validation.py:

  • B017 Do not assert blind exception: Exception
  • Occurs at:
    • tests/test_validation.py:37:10
    • tests/test_validation.py:45:10
    • tests/test_validation.py:53:10

Fix (recommended): assert the specific exception type

Right now the tests use pytest.raises(Exception), which Ruff flags as too broad. Update them to expect the concrete exception your validator raises.

Since you’re using Pandera (pandera>=0.20.0 in pyproject.toml), the most common exception is pandera.errors.SchemaError (sometimes SchemaErrors for multiple failures).

Edit tests/test_validation.py (ref b2817693c0a2ee0e7c7e4d98c926e939b7c1ed5c) like this:

import pandas as pd
import pytest
from pandera.errors import SchemaError  # add this

from laptop_price.validation import validate_training_data, validate_inference_data


def test_invalid_ram_fails() -> None:
    row = _valid_training_row()
    row["Ram"] = 0
    df = pd.DataFrame([row])
    with pytest.raises(SchemaError):
        validate_training_data(df)


def test_invalid_weight_fails() -> None:
    row = _valid_training_row()
    row["Weight"] = 15.0
    df = pd.DataFrame([row])
    with pytest.raises(SchemaError):
        validate_training_data(df)


def test_negative_price_fails() -> None:
    row = _valid_training_row()
    row["Price"] = -100.0
    df = pd.DataFrame([row])
    with pytest.raises(SchemaError):
        validate_training_data(df)

If your implementation raises pandera.errors.SchemaErrors instead, swap SchemaError for SchemaErrors.

Alternative (less strict): broaden, but not “blind”

If you truly expect more than one possible error type (e.g., SchemaError or KeyError), you can do:

with pytest.raises((SchemaError, KeyError, ValueError)):
    validate_training_data(df)

After the change

Ruff should stop flagging B017 in those tests, and the lint job should proceed. (Your logs also mention many other Ruff errors, but the excerpt you provided clearly includes these B017 violations; fixing them is necessary for the job to pass.)

User: do that

Assistant: I’ve queued the change (replace pytest.raises(Exception) with the specific Pandera validation exception) and we’re now waiting on your approval in the dialog to proceed with applying it and opening the update.

This pull request was created from Copilot chat.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: neel26parekh <96442078+neel26parekh@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix CI failure caused by Ruff lint rule B017 in tests Fix Ruff B017: replace blind pytest.raises(Exception) with specific Pandera exception Mar 10, 2026
@neel26parekh

Copy link
Copy Markdown
Owner

@copilot make it github green like github

Co-authored-by: neel26parekh <96442078+neel26parekh@users.noreply.github.com>

Copilot AI commented Mar 12, 2026

Copy link
Copy Markdown
Author

@copilot make it github green like github

Fixed in d654944. All Ruff lint errors across src/ and tests/ are now resolved — python -m ruff check src/ tests/ passes with zero errors. This covers:

  • E501 long lines (wrapped function signatures, argparse calls, SQL, arithmetic expressions, test lambdas)
  • B905 zip() missing strict=
  • W292 missing trailing newlines
  • I001 unsorted imports
  • UP017 datetime.UTC alias
  • UP035 collections.abc.Iterable
  • F401 unused imports

The CI lint step should now go green.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants