From 8be2b2b94d1b57cc297cd47140ce65c913ee3481 Mon Sep 17 00:00:00 2001 From: Mithesh Date: Tue, 17 Nov 2026 00:21:51 +0530 Subject: [PATCH 1/4] Enable precise pytest exception assertion lint rules --- pyproject.toml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 00f22a98..e46793f1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -134,6 +134,14 @@ select = [ "ANN", "PTH", "PT018", + "PT010", + "PT011", + "PT012", + "PT014", + "PT016", + "PT028", + "PT030", + "PT031", "RET", "SIM", "TID", From 834837658e5de78774f4344f48e98249319b838e Mon Sep 17 00:00:00 2001 From: Mithesh Date: Tue, 17 Nov 2026 00:22:47 +0530 Subject: [PATCH 2/4] Assert specific exception messages and narrow raises blocks --- tests/test_build_ai_vlm_checks.py | 2 +- tests/test_episode.py | 22 +++++++++---------- tests/test_episode_to_arrow.py | 2 +- tests/test_lerobot_export.py | 2 +- tests/test_manifest_deduplication.py | 14 ++++++------ tests/test_manifest_splits.py | 16 +++++++------- tests/test_prefetch.py | 6 +++++- tests/test_processing_regressions.py | 2 +- tests/test_runtime_client.py | 10 ++++----- tests/test_source_sampling.py | 2 +- tests/test_source_windows.py | 18 ++++++++++++---- tests/test_video_import.py | 32 +++++++++++++++------------- tests/test_video_streaming.py | 4 +++- 13 files changed, 73 insertions(+), 59 deletions(-) diff --git a/tests/test_build_ai_vlm_checks.py b/tests/test_build_ai_vlm_checks.py index 66fe8edb..9016caf6 100644 --- a/tests/test_build_ai_vlm_checks.py +++ b/tests/test_build_ai_vlm_checks.py @@ -690,7 +690,7 @@ def test_model_answers_reject_ambiguous_or_out_of_contract_json( task: str, response_text: str ) -> None: checks = hflow.build_ai_vlm_checks - with pytest.raises(ValueError) as captured_error: + with pytest.raises(ValueError, match=task.replace("-", " ")) as captured_error: checks.parse_task_response(checks.EvaluationTask(task), response_text) assert captured_error.value.__cause__ is None assert captured_error.value.__suppress_context__ diff --git a/tests/test_episode.py b/tests/test_episode.py index 675c0539..e4a17253 100644 --- a/tests/test_episode.py +++ b/tests/test_episode.py @@ -500,14 +500,13 @@ def fail_rename(self: Path, target: Path) -> Path: monkeypatch.setattr(Path, "rename", fail_rename) - with ( - Episode(canonical_mcap, workdir=workdir) as ep, - pytest.raises(PermissionError, match="simulated permission denied"), - ): + with Episode(canonical_mcap, workdir=workdir) as ep: if method == "frames": - ep.frames(fps=2.0) + with pytest.raises(PermissionError, match="simulated permission denied"): + ep.frames(fps=2.0) else: - ep.frames_at_indices(frame_indices=[0, 2]) + with pytest.raises(PermissionError, match="simulated permission denied"): + ep.frames_at_indices(frame_indices=[0, 2]) assert not [d for d in workdir.iterdir() if d.is_dir() and d.name.endswith(".tmp")] @@ -543,14 +542,13 @@ def failing_run(*args: Any, **kwargs: Any) -> Any: else "ffmpeg extracted 0 of 2 selected frames" ) - with ( - Episode(canonical_mcap, workdir=workdir) as ep, - pytest.raises(RuntimeError, match=expected_error), - ): + with Episode(canonical_mcap, workdir=workdir) as ep: if method == "frames": - ep.frames(fps=2.0) + with pytest.raises(RuntimeError, match=expected_error): + ep.frames(fps=2.0) else: - ep.frames_at_indices(frame_indices=[0, 2]) + with pytest.raises(RuntimeError, match=expected_error): + ep.frames_at_indices(frame_indices=[0, 2]) assert not [d for d in workdir.iterdir() if d.is_dir()] diff --git a/tests/test_episode_to_arrow.py b/tests/test_episode_to_arrow.py index 09954a11..2826662c 100644 --- a/tests/test_episode_to_arrow.py +++ b/tests/test_episode_to_arrow.py @@ -48,7 +48,7 @@ def test_to_arrow_rejects_payload_field_that_shadows_log_time() -> None: log_times=[1_000_000_000, 2_000_000_000], ) - with pytest.raises(ValueError) as excinfo: + with pytest.raises(ValueError, match="reserved Arrow column") as excinfo: channel.to_arrow() message = str(excinfo.value) assert "log_time_ns" in message diff --git a/tests/test_lerobot_export.py b/tests/test_lerobot_export.py index cedda609..a09d67ac 100644 --- a/tests/test_lerobot_export.py +++ b/tests/test_lerobot_export.py @@ -331,7 +331,7 @@ def write_changed_schema(table: pa.Table, where: Path) -> None: destination = tmp_path / "out" destination.mkdir() (destination / "sentinel.txt").write_text("previous export") - with pytest.raises(ValueError) as error: + with pytest.raises(ValueError, match="schema differs from source") as error: export.export(destination, manifest=manifest, camera_keys=CAMS) message = str(error.value) assert "data/chunk-000/file-001.parquet" in message diff --git a/tests/test_manifest_deduplication.py b/tests/test_manifest_deduplication.py index 0746dd5d..bad2c655 100644 --- a/tests/test_manifest_deduplication.py +++ b/tests/test_manifest_deduplication.py @@ -177,18 +177,18 @@ def test_reserved_metadata_names_cannot_overwrite_source_provenance(tmp_path: Pa @pytest.mark.parametrize( - "identities, conflicts", + "identities, conflicts, expected_error", [ - ((), ()), - (("digest", "DIGEST"), ()), - (("digest",), ("label", "LABEL")), - (("retained_sample_id",), ()), + ((), (), "identity_columns must be nonempty"), + (("digest", "DIGEST"), (), "identity_columns must contain distinct"), + (("digest",), ("label", "LABEL"), "conflict_columns must contain distinct"), + (("retained_sample_id",), (), "column names are reserved"), ], ) def test_invalid_settings_reject_ambiguous_or_reserved_columns( - identities: tuple[str, ...], conflicts: tuple[str, ...] + identities: tuple[str, ...], conflicts: tuple[str, ...], expected_error: str ) -> None: - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=expected_error): ManifestDeduplicationSettings("sample_id", identities, conflicts) diff --git a/tests/test_manifest_splits.py b/tests/test_manifest_splits.py index 05f6f310..95888af5 100644 --- a/tests/test_manifest_splits.py +++ b/tests/test_manifest_splits.py @@ -165,19 +165,19 @@ def test_invalid_identities_never_publish_a_split( @pytest.mark.parametrize( - "name, fraction", + "name, fraction, expected_error", [ - ("../escape", 0.5), - ("assignments", 0.5), - ("train", float("nan")), - ("train", True), - ("train", 0), + ("../escape", 0.5, "partition name must match"), + ("assignments", 0.5, "partition name 'assignments' is reserved"), + ("train", float("nan"), "partition fraction must be finite"), + ("train", True, "partition fraction must be finite"), + ("train", 0, "partition fraction must be finite"), ], ) def test_partition_values_reject_unsafe_names_and_invalid_fractions( - name: str, fraction: float + name: str, fraction: float, expected_error: str ) -> None: - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=expected_error): ManifestPartition(name, fraction) diff --git a/tests/test_prefetch.py b/tests/test_prefetch.py index 296fce65..790ebc25 100644 --- a/tests/test_prefetch.py +++ b/tests/test_prefetch.py @@ -170,12 +170,16 @@ def prepare(item: int, directory: Path) -> int: async def scenario() -> list[int]: received: list[int] = [] - with pytest.raises(ValueError, match="window 2 is unreadable"): + + async def consume() -> None: async with prefetch( range(5), prepare, working_directory=tmp_path, lookahead=2 ) as prepared_items: async for prepared in prepared_items: received.append(prepared.item) + + with pytest.raises(ValueError, match="window 2 is unreadable"): + await consume() return received assert asyncio.run(scenario()) == [0, 1] diff --git a/tests/test_processing_regressions.py b/tests/test_processing_regressions.py index 429b0677..a67277fb 100644 --- a/tests/test_processing_regressions.py +++ b/tests/test_processing_regressions.py @@ -941,7 +941,7 @@ def two_missing( ) -> hflow.CheckResult: return hflow.CheckResult(measurements={"n": len(topics) + threshold}) - with pytest.raises(ValueError) as raised: + with pytest.raises(ValueError, match="required parameter") as raised: app.check(version="1")(cast(hflow.steps.CheckFunction, two_missing)) message = str(raised.value) assert "threshold=..." in message diff --git a/tests/test_runtime_client.py b/tests/test_runtime_client.py index 21b7a614..a8d21496 100644 --- a/tests/test_runtime_client.py +++ b/tests/test_runtime_client.py @@ -599,7 +599,7 @@ def test_ingest_refuses_a_batch_count_the_run_could_not_honour(stub_server: str) def test_ingest_rejects_invalid_uri_before_making_http_requests(stub_server: str, uri: str) -> None: client = AirflowClient(stub_server, "airflow", "right-password") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=r"non-empty string|separator|relative to the data root"): client.ingest("pipeline_ingest", [uri]) assert _StubAirflowHandler.requests_seen == [] @@ -1020,12 +1020,10 @@ def test_wait_until_healthy_times_out_with_last_status(stub_server: str) -> None _StubAirflowHandler.healthy = False client = AirflowClient(stub_server, "airflow", "right-password") instantly_advancing_clock = InstantlyAdvancingClock() - with ( - pytest.MonkeyPatch.context() as monkeypatch, - pytest.raises(TimeoutError, match="scheduler=unhealthy"), - ): + with pytest.MonkeyPatch.context() as monkeypatch: monkeypatch.setattr("hflow.runtime._client.time", instantly_advancing_clock) - client.wait_until_healthy(timeout_s=0.3, poll_interval_s=10.0) + with pytest.raises(TimeoutError, match="scheduler=unhealthy"): + client.wait_until_healthy(timeout_s=0.3, poll_interval_s=10.0) assert instantly_advancing_clock.current_time_s == pytest.approx(0.3) diff --git a/tests/test_source_sampling.py b/tests/test_source_sampling.py index ddcab016..66ce9835 100644 --- a/tests/test_source_sampling.py +++ b/tests/test_source_sampling.py @@ -432,7 +432,7 @@ def test_oversized_windows_are_rejected_before_output_creation( ], ) def test_sampling_settings_refuse_invalid_limits(field_name: str, value: object) -> None: - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=field_name): replace(SourceFrameSampling(), **{field_name: value}) diff --git a/tests/test_source_windows.py b/tests/test_source_windows.py index 1928b9eb..2f29bc50 100644 --- a/tests/test_source_windows.py +++ b/tests/test_source_windows.py @@ -49,11 +49,21 @@ def test_plans_refuse_partial_coverage_when_limits_cannot_be_satisfied() -> None @pytest.mark.parametrize("duration_millis", [True, 1.5, "100", 0, -1]) def test_invalid_durations_are_not_silently_coerced(duration_millis: object) -> None: - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="duration_millis must be"): plan_source_windows(duration_millis, maximum_window_millis=100) # ty: ignore[invalid-argument-type] -@pytest.mark.parametrize("bounds", [(-1, 1), (0, 0), (2, 1), (False, 1)]) -def test_source_windows_require_a_nonempty_forward_interval(bounds: tuple[int, int]) -> None: - with pytest.raises(ValueError): +@pytest.mark.parametrize( + ("bounds", "expected_error"), + [ + ((-1, 1), "start_millis must be >= 0"), + ((0, 0), "end_millis must be > 0"), + ((2, 1), "end_millis must be greater than start_millis"), + ((False, 1), "start_millis must be an int"), + ], +) +def test_source_windows_require_a_nonempty_forward_interval( + bounds: tuple[int, int], expected_error: str +) -> None: + with pytest.raises(ValueError, match=expected_error): SourceWindow(*bounds) diff --git a/tests/test_video_import.py b/tests/test_video_import.py index e96705c9..a4715f50 100644 --- a/tests/test_video_import.py +++ b/tests/test_video_import.py @@ -217,25 +217,27 @@ def test_invalid_sources_and_incomplete_excerpts_publish_nothing( @pytest.mark.parametrize( - "config", + "config, expected_error", [ - {"duration_s": 0}, - {"source_start_s": -1}, - {"image_hz": 0}, - {"image_height": True}, - {"start_time_ns": -1}, - {"start_time_ns": True}, + ({"duration_s": 0}, "duration_s must be positive"), + ({"source_start_s": -1}, "source_start_s nonnegative"), + ({"image_hz": 0}, "image_hz must be positive"), + ({"image_height": True}, "image_height must be an int"), + ({"start_time_ns": -1}, "start_time_ns must be in"), + ({"start_time_ns": True}, "start_time_ns must be an int"), # Inside the field's range but the final frame's timestamp overflows. - {"start_time_ns": (1 << 64) - 1}, - {"camera_name": ""}, - {"metadata": (("task", "one"), ("task", "two"))}, - {"maximum_encoded_bytes": 0}, - {"maximum_encoded_bytes": True}, - {"maximum_encoded_bytes": 1.5}, + ({"start_time_ns": (1 << 64) - 1}, "excerpt exceeds the MCAP timestamp range"), + ({"camera_name": ""}, "camera_name must be nonempty"), + ({"metadata": (("task", "one"), ("task", "two"))}, "metadata contains duplicate key"), + ({"maximum_encoded_bytes": 0}, "maximum_encoded_bytes must be > 0"), + ({"maximum_encoded_bytes": True}, "maximum_encoded_bytes must be an int"), + ({"maximum_encoded_bytes": 1.5}, "maximum_encoded_bytes must be an int"), ], ) -def test_invalid_import_configuration_is_rejected(config: dict[str, object]) -> None: - with pytest.raises(ValueError): +def test_invalid_import_configuration_is_rejected( + config: dict[str, object], expected_error: str +) -> None: + with pytest.raises(ValueError, match=expected_error): replace(VideoImportConfig(duration_s=1), **config) diff --git a/tests/test_video_streaming.py b/tests/test_video_streaming.py index 2f08aa23..3e84d487 100644 --- a/tests/test_video_streaming.py +++ b/tests/test_video_streaming.py @@ -174,7 +174,9 @@ def test_streaming_fps_preserves_exact_median_and_errors(timestamps: list[int]) try: expected = video.estimate_fps_from_log_times(timestamps, topic="/camera") except ValueError as error: - with pytest.raises(ValueError) as streaming_error: + with pytest.raises( + ValueError, match=r"camera topic '/camera'.*frame rate" + ) as streaming_error: video.estimate_fps_from_streaming_log_times(iter(timestamps), topic="/camera") assert str(streaming_error.value) == str(error) else: From ba1b0019e13853f03fd8fc6ed1ed31bda42eef24 Mon Sep 17 00:00:00 2001 From: Mithesh Date: Tue, 17 Nov 2026 00:23:18 +0530 Subject: [PATCH 3/4] Strengthen Build AI example exception assertions --- examples/build_ai_evaluation/tests/test_evaluation.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/build_ai_evaluation/tests/test_evaluation.py b/examples/build_ai_evaluation/tests/test_evaluation.py index 402e0ec5..972ee2b7 100644 --- a/examples/build_ai_evaluation/tests/test_evaluation.py +++ b/examples/build_ai_evaluation/tests/test_evaluation.py @@ -133,7 +133,10 @@ def test_task_schemas_and_parsers_enforce_the_answer_contract() -> None: f"{task.value}: the schema permits {value!r} but the parser does not accept it" ) for value in rejected_values: - with pytest.raises(ValueError): + expected_error = ( + "hand count" if task is EvaluationTask.HAND_COUNT else "active manipulation" + ) + with pytest.raises(ValueError, match=expected_error): parser(json.dumps({property_name: value})) @@ -581,7 +584,7 @@ def test_run_metadata_names_the_file_and_the_bad_field( metadata_path.parent.mkdir(parents=True, exist_ok=True) metadata_path.write_text(run_json_text) - with pytest.raises(ValueError) as error: + with pytest.raises(ValueError, match=r"run\.json") as error: _prepare_output_directory(configuration) message = str(error.value) From a60dd2338452edc776e10aa025f0fedd769ee4f1 Mon Sep 17 00:00:00 2001 From: Mithesh Date: Tue, 17 Nov 2026 00:23:50 +0530 Subject: [PATCH 4/4] Strengthen Egosuite example exception assertions --- examples/egosuite_evaluation/tests/test_evaluation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/egosuite_evaluation/tests/test_evaluation.py b/examples/egosuite_evaluation/tests/test_evaluation.py index aefd95da..34c2db77 100644 --- a/examples/egosuite_evaluation/tests/test_evaluation.py +++ b/examples/egosuite_evaluation/tests/test_evaluation.py @@ -394,7 +394,7 @@ def test_saved_label_report_rejects_unsupported_envelope_before_frames( report_path = tmp_path / "labels.json" report_path.write_text(json.dumps({**envelope, "frames": [None]})) - with pytest.raises(ValueError) as error: + with pytest.raises(ValueError, match=rf"field '{field_name}' has value") as error: load_projected_hand_label_report(report_path) message = str(error.value) @@ -1016,7 +1016,7 @@ def test_run_metadata_names_the_file_and_the_bad_field( metadata_path.parent.mkdir(parents=True, exist_ok=True) metadata_path.write_text(run_json_text) - with pytest.raises(ValueError) as error: + with pytest.raises(ValueError, match=r"run\.json") as error: _prepare_output_directory(configuration) message = str(error.value)