From 93abdda1f2e4f5707e9c7141fc2f2f0421f7f046 Mon Sep 17 00:00:00 2001 From: YousefZahran1 Date: Thu, 7 May 2026 19:05:59 +0300 Subject: [PATCH] fix: sort labels by str() key to handle mixed int/str label types When classification labels contain values like '101' or '102', the internal label_key_valudator coerces them to int while other labels (e.g. 'foo', 'bar') remain as str. Calling sorted() on this mixed-type list raises TypeError in Python 3 because int and str are not orderable. Fix: use sorted(labels, key=str) in calculate_matrix so that labels are always sorted by their string representation, making the sort stable regardless of whether individual labels happen to be int or str. Fixes #1085 --- .../legacy/calculations/classification_performance.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/evidently/legacy/calculations/classification_performance.py b/src/evidently/legacy/calculations/classification_performance.py index 0c748aedd4..088f33a5cd 100644 --- a/src/evidently/legacy/calculations/classification_performance.py +++ b/src/evidently/legacy/calculations/classification_performance.py @@ -321,7 +321,10 @@ def calculate_lift_table(binded): def calculate_matrix(target: pd.Series, prediction: pd.Series, labels: List[Label]) -> ConfusionMatrix: - sorted_labels = sorted(labels) # type: ignore[type-var] + # Use str() as sort key to avoid TypeError when labels contain mixed int/str types. + # This happens when string labels like '101' are coerced to int by label_key_valudator + # while other labels (e.g. 'foo') remain as strings. + sorted_labels = sorted(labels, key=str) matrix = metrics.confusion_matrix(target, prediction, labels=sorted_labels) return ConfusionMatrix(labels=sorted_labels, values=[row.tolist() for row in matrix])