From fb05db69bdccd34f7314237e6febb6403788d668 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Tue, 29 Jul 2025 12:55:13 +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_calibration_test.py | 9 ++-- scripts/ci/model_compression_test.py | 12 +++-- scripts/ci/model_monitoring_test.py | 78 ++++++++++++++++++---------- scripts/ci/t5_summarization_test.py | 12 +++-- 4 files changed, 74 insertions(+), 37 deletions(-) diff --git a/scripts/ci/model_calibration_test.py b/scripts/ci/model_calibration_test.py index d76342b91..51ce4f33a 100644 --- a/scripts/ci/model_calibration_test.py +++ b/scripts/ci/model_calibration_test.py @@ -112,7 +112,8 @@ def test_model_calibration(): # Test temperature setting logger.info("Testing temperature calibration...") model.temperature.data.fill_(1.0) - assert model.temperature.item() == 1.0, "Temperature not set correctly" + if model.temperature.item() != 1.0: + raise AssertionError("Temperature not set correctly") logger.info("✅ Temperature calibration works") # Create test data @@ -156,8 +157,10 @@ def test_model_calibration(): logger.info(f"Macro F1: {macro_f1:.4f}") # Basic validation - assert 0 <= micro_f1 <= 1, f"Invalid F1 score: {micro_f1}" - assert 0 <= macro_f1 <= 1, f"Invalid F1 score: {macro_f1}" + if not 0 <= micro_f1 <= 1: + raise AssertionError(f"Invalid F1 score: {micro_f1}") + if not 0 <= macro_f1 <= 1: + raise AssertionError(f"Invalid F1 score: {macro_f1}") logger.info("✅ Metrics calculation works") diff --git a/scripts/ci/model_compression_test.py b/scripts/ci/model_compression_test.py index 0f9b78edc..fc22a0a8e 100644 --- a/scripts/ci/model_compression_test.py +++ b/scripts/ci/model_compression_test.py @@ -133,9 +133,12 @@ def test_model_compression(): logger.info(f"Inference speedup: {speedup:.2f}x") # Basic validation - assert quantized_size < original_size, f"Quantized model should be smaller: {quantized_size} >= {original_size}" - assert size_reduction > 0, f"Size reduction should be positive: {size_reduction}" - assert speedup > 0, f"Speedup should be positive: {speedup}" + if quantized_size >= original_size: + raise AssertionError(f"Quantized model should be smaller: {quantized_size} >= {original_size}") + if size_reduction <= 0: + raise AssertionError(f"Size reduction should be positive: {size_reduction}") + if speedup <= 0: + raise AssertionError(f"Speedup should be positive: {speedup}") # Test model saving logger.info("Testing model saving...") @@ -152,7 +155,8 @@ def test_model_compression(): }, output_path) # Verify file was created - assert output_path.exists(), f"Compressed model file not created: {output_path}" + if not output_path.exists(): + raise AssertionError(f"Compressed model file not created: {output_path}") logger.info(f"✅ Compressed model saved to {output_path}") # Clean up diff --git a/scripts/ci/model_monitoring_test.py b/scripts/ci/model_monitoring_test.py index 9bbec51f7..833b0e861 100644 --- a/scripts/ci/model_monitoring_test.py +++ b/scripts/ci/model_monitoring_test.py @@ -71,12 +71,15 @@ def test_performance_tracker(): # Test current performance current_perf = tracker.get_current_performance() - assert "f1_score" in current_perf - assert current_perf["f1_score"] == 0.75 + if "f1_score" not in current_perf: + raise AssertionError + if current_perf["f1_score"] != 0.75: + raise AssertionError # Test trend analysis trend = tracker.get_trend_analysis() - assert "insufficient_data" in trend + if "insufficient_data" not in trend: + raise AssertionError # Test degradation detection degradation_alert = tracker.detect_degradation() @@ -121,7 +124,8 @@ def test_drift_detector(): drift_metrics = detector.detect_drift(current_data) assert isinstance(drift_metrics, DriftMetrics) - assert not drift_metrics.drift_detected + if drift_metrics.drift_detected: + raise AssertionError # Test with drifted data drifted_data = pd.DataFrame( @@ -161,10 +165,14 @@ def test_alert_system(): ) # Test alert properties - assert alert.alert_type == "TEST_ALERT" - assert alert.severity == "MEDIUM" - assert alert.message == "Test alert message" - assert not alert.action_required + if alert.alert_type != "TEST_ALERT": + raise AssertionError + if alert.severity != "MEDIUM": + raise AssertionError + if alert.message != "Test alert message": + raise AssertionError + if alert.action_required: + raise AssertionError # Test alert serialization alert_dict = { @@ -201,7 +209,8 @@ def test_config_creation(): # Check if config file was created config_path = Path(test_config_path) - assert config_path.exists() + if not config_path.exists(): + raise AssertionError # Load and validate config import yaml @@ -210,10 +219,14 @@ def test_config_creation(): config = yaml.safe_load(f) # Check required fields - assert "model_path" in config - assert "window_size" in config - assert "monitor_interval" in config - assert "alert_threshold" in config + if "model_path" not in config: + raise AssertionError + if "window_size" not in config: + raise AssertionError + if "monitor_interval" not in config: + raise AssertionError + if "alert_threshold" not in config: + raise AssertionError # Clean up config_path.unlink() @@ -254,15 +267,21 @@ def test_monitoring_initialization(): monitor = ModelHealthMonitor(test_config_path) # Test basic properties - assert monitor.config["window_size"] == 10 - assert monitor.config["alert_threshold"] == 0.1 - assert not monitor.monitoring_active + if monitor.config["window_size"] != 10: + raise AssertionError + if monitor.config["alert_threshold"] != 0.1: + raise AssertionError + if monitor.monitoring_active: + raise AssertionError # Test health status health_status = monitor.get_health_status() - assert "timestamp" in health_status - assert "model_loaded" in health_status - assert "monitoring_active" in health_status + if "timestamp" not in health_status: + raise AssertionError + if "model_loaded" not in health_status: + raise AssertionError + if "monitoring_active" not in health_status: + raise AssertionError # Clean up Path(test_config_path).unlink() @@ -295,13 +314,20 @@ def test_metrics_collection(): ) # Test metrics properties - assert metrics.f1_score == 0.75 - assert metrics.precision == 0.78 - assert metrics.recall == 0.72 - assert metrics.inference_time_ms == 150.0 - assert metrics.throughput_rps == 33.3 - assert metrics.memory_usage_mb == 512.0 - assert metrics.gpu_utilization == 45.0 + if metrics.f1_score != 0.75: + raise AssertionError + if metrics.precision != 0.78: + raise AssertionError + if metrics.recall != 0.72: + raise AssertionError + if metrics.inference_time_ms != 150.0: + raise AssertionError + if metrics.throughput_rps != 33.3: + raise AssertionError + if metrics.memory_usage_mb != 512.0: + raise AssertionError + if metrics.gpu_utilization != 45.0: + raise AssertionError # Test metrics serialization metrics_dict = { diff --git a/scripts/ci/t5_summarization_test.py b/scripts/ci/t5_summarization_test.py index a2ce52761..7c03f298b 100755 --- a/scripts/ci/t5_summarization_test.py +++ b/scripts/ci/t5_summarization_test.py @@ -34,9 +34,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") @@ -71,7 +74,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")