From 2f33e817fed372c74b5e98cb9169f1bb21f37780 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Fri, 8 Aug 2025 23:34:41 +0000 Subject: [PATCH] refactor: remove assert statement from non-test files Usage of `assert` statement in application logic is discouraged. `assert` is removed with compiling to optimized byte code. Consider raising an exception instead. Ideally, `assert` statement should be used only in tests. --- scripts/ci/model_compression_test.py | 6 ++++-- scripts/ci/model_monitoring_test.py | 30 ++++++++++++++++++---------- scripts/ci/t5_summarization_test.py | 12 +++++++---- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/scripts/ci/model_compression_test.py b/scripts/ci/model_compression_test.py index 67fa9e3ce..736b45f0c 100644 --- a/scripts/ci/model_compression_test.py +++ b/scripts/ci/model_compression_test.py @@ -122,8 +122,10 @@ def test_model_compression(): compression_ratio = original_size / compressed_size logger.info("Compression ratio: {compression_ratio:.2f}x") - assert compressed_size < original_size, "Model should be smaller after compression" - assert compression_ratio > 1.0, "Compression ratio should be greater than 1" + if compressed_size >= original_size: + raise AssertionError("Model should be smaller after compression") + if compression_ratio <= 1.0: + raise AssertionError("Compression ratio should be greater than 1") with tempfile.NamedTemporaryFile(suffix=".pt", delete=True) as temp_file: torch.save(quantized_model.state_dict(), temp_file.name) diff --git a/scripts/ci/model_monitoring_test.py b/scripts/ci/model_monitoring_test.py index 1def8ba8d..5ede8441f 100644 --- a/scripts/ci/model_monitoring_test.py +++ b/scripts/ci/model_monitoring_test.py @@ -121,10 +121,14 @@ def test_model_performance_monitoring(): logger.info("Recall: {metrics['recall']:.4f}") logger.info("F1 Score: {metrics['f1_score']:.4f}") - assert 0 <= metrics['accuracy'] <= 1, "Accuracy should be between 0 and 1" - assert 0 <= metrics['precision'] <= 1, "Precision should be between 0 and 1" - assert 0 <= metrics['recall'] <= 1, "Recall should be between 0 and 1" - assert 0 <= metrics['f1_score'] <= 1, "F1 score should be between 0 and 1" + if not 0 <= metrics['accuracy'] <= 1: + raise AssertionError("Accuracy should be between 0 and 1") + if not 0 <= metrics['precision'] <= 1: + raise AssertionError("Precision should be between 0 and 1") + if not 0 <= metrics['recall'] <= 1: + raise AssertionError("Recall should be between 0 and 1") + if not 0 <= metrics['f1_score'] <= 1: + raise AssertionError("F1 score should be between 0 and 1") logger.info("✅ Model performance monitoring test passed") return True @@ -162,8 +166,10 @@ def test_model_drift_detection(): logger.info("Accuracy drift: {accuracy_drift:.4f}") logger.info("F1 score drift: {f1_drift:.4f}") - assert accuracy_drift >= 0, "Drift should be non-negative" - assert f1_drift >= 0, "Drift should be non-negative" + if accuracy_drift < 0: + raise AssertionError("Drift should be non-negative") + if f1_drift < 0: + raise AssertionError("Drift should be non-negative") logger.info("✅ Model drift detection test passed") return True @@ -196,10 +202,14 @@ def test_monitoring_logging(): logger.info("Monitoring log entry: {log_entry}") - assert 'timestamp' in log_entry, "Log entry should have timestamp" - assert 'model_version' in log_entry, "Log entry should have model version" - assert 'metrics' in log_entry, "Log entry should have metrics" - assert 'status' in log_entry, "Log entry should have status" + if 'timestamp' not in log_entry: + raise AssertionError("Log entry should have timestamp") + if 'model_version' not in log_entry: + raise AssertionError("Log entry should have model version") + if 'metrics' not in log_entry: + raise AssertionError("Log entry should have metrics") + if 'status' not in log_entry: + raise AssertionError("Log entry should have status") logger.info("✅ Monitoring logging test passed") return True diff --git a/scripts/ci/t5_summarization_test.py b/scripts/ci/t5_summarization_test.py index 8b268f175..09c9a86fc 100755 --- a/scripts/ci/t5_summarization_test.py +++ b/scripts/ci/t5_summarization_test.py @@ -39,9 +39,12 @@ def test_t5_model_loading(): logger.info("✅ T5 model initialized successfully") # Test basic model properties - assert hasattr(model, "model"), "Model should have 'model' attribute" - assert hasattr(model, "tokenizer"), "Model should have 'tokenizer' attribute" - assert hasattr(model, "device"), "Model should have 'device' attribute" + if not hasattr(model, "model"): + raise AssertionError("Model should have 'model' attribute") + if not hasattr(model, "tokenizer"): + raise AssertionError("Model should have 'tokenizer' attribute") + if not hasattr(model, "device"): + raise AssertionError("Model should have 'device' attribute") logger.info("✅ Model attributes validation passed") @@ -83,7 +86,8 @@ def test_t5_summarization(): # Validate summary assert isinstance(summary, str), "Summary should be a string" - assert len(summary) > 0, "Summary should not be empty" + if len(summary) <= 0: + raise AssertionError("Summary should not be empty") assert len(summary) < len(test_text), "Summary should be shorter than input" logger.info("✅ Summary validation passed")