Skip to content

Commit 5ea489b

Browse files
CHORE: set-up mypy for v1 code, add it to CI, fix existing warnings
1 parent 96a5f39 commit 5ea489b

File tree

6 files changed

+44
-21
lines changed

6 files changed

+44
-21
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ jobs:
5555
run: make lint
5656
- name: Check static typing
5757
run: make type-check
58+
- name: Check static typing v1
59+
run: make v1-type-check
5860
- name: Test with pytest
5961
run: make coverage
6062
- name: Code coverage

Makefile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
.PHONY: tests doc build
22

3+
mapie_v0_folder_name = mapie_v0_package
4+
35
lint:
46
flake8 . --exclude=doc
57

68
type-check:
79
mypy mapie
810

11+
v1-type-check:
12+
mypy mapie_v1 --exclude $(mapie_v0_folder_name)
13+
914
tests:
1015
pytest -vs --doctest-modules mapie
1116

1217
integration-tests-v1:
13-
@pip install mapie --no-dependencies --target=./mapie_v1/integration_tests/mapie_v0_package >/dev/null 2>&1
14-
@mv ./mapie_v1/integration_tests/mapie_v0_package/mapie ./mapie_v1/integration_tests/mapie_v0_package/mapiev0
15-
@- export PYTHONPATH="${PYTHONPATH}:./mapie_v1/integration_tests/mapie_v0_package"; pytest -vs mapie_v1/integration_tests/tests -k $(pattern)
16-
@mv ./mapie_v1/integration_tests/mapie_v0_package/mapiev0 ./mapie_v1/integration_tests/mapie_v0_package/mapie
18+
@pip install mapie --no-dependencies --target=./mapie_v1/integration_tests/$(mapie_v0_folder_name) >/dev/null 2>&1
19+
@mv ./mapie_v1/integration_tests/$(mapie_v0_folder_name)/mapie ./mapie_v1/integration_tests/$(mapie_v0_folder_name)/mapiev0
20+
@- export PYTHONPATH="${PYTHONPATH}:./mapie_v1/integration_tests/$(mapie_v0_folder_name)"; pytest -vs mapie_v1/integration_tests/tests -k $(pattern)
21+
@mv ./mapie_v1/integration_tests/$(mapie_v0_folder_name)/mapiev0 ./mapie_v1/integration_tests/$(mapie_v0_folder_name)/mapie
1722

1823
coverage:
1924
pytest -vs \

mapie_v1/__init__.py

Whitespace-only changes.

mapie_v1/_utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import warnings
2-
from typing import Union, List
2+
from typing import Union, List, Tuple, cast
33

44
from numpy import array
55
from mapie._typing import ArrayLike, NDArray
@@ -56,3 +56,9 @@ def make_intervals_single_if_single_alpha(
5656
if len(alphas) == 1:
5757
return intervals[:, :, 0]
5858
return intervals
59+
60+
61+
def cast_point_predictions_to_ndarray(
62+
point_predictions: Union[NDArray, Tuple[NDArray, NDArray]]
63+
) -> NDArray:
64+
return cast(NDArray, point_predictions)

mapie_v1/classification.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ def fit(
3131
y_train: ArrayLike,
3232
fit_params: Optional[dict] = None,
3333
) -> Self:
34-
pass
34+
return self
3535

3636
def conformalize(
3737
self,
3838
X_conf: ArrayLike,
3939
y_conf: ArrayLike,
4040
predict_params: Optional[dict] = None,
4141
) -> Self:
42-
pass
42+
return self
4343

4444
def predict(self, X: ArrayLike) -> NDArray:
4545
"""
@@ -49,7 +49,7 @@ def predict(self, X: ArrayLike) -> NDArray:
4949
scikit-learn classifiers
5050
Shape (n_samples,)
5151
"""
52-
pass
52+
return np.ndarray(0)
5353

5454
def predict_sets(
5555
self,
@@ -66,7 +66,7 @@ def predict_sets(
6666
Shape (n_samples, n_classes, confidence_level) if confidence_level
6767
is a list of floats
6868
"""
69-
pass
69+
return np.ndarray(0)
7070

7171

7272
class CrossConformalClassifier:
@@ -89,26 +89,26 @@ def fit(
8989
y_train: ArrayLike,
9090
fit_params: Optional[dict] = None,
9191
) -> Self:
92-
pass
92+
return self
9393

9494
def conformalize(
9595
self,
9696
X_conf: ArrayLike,
9797
y_conf: ArrayLike,
9898
predict_params: Optional[dict] = None
9999
) -> Self:
100-
pass
100+
return self
101101

102102
def predict(self,
103-
X: ArrayLike):
103+
X: ArrayLike) -> NDArray:
104104
"""
105105
Return
106106
-----
107107
Return ponctual prediction similar to predict method of
108108
scikit-learn classifiers
109109
Shape (n_samples,)
110110
"""
111-
pass
111+
return np.ndarray(0)
112112

113113
def predict_sets(
114114
self,
@@ -125,4 +125,4 @@ def predict_sets(
125125
Shape (n_samples, n_classes, confidence_level) if confidence_level
126126
is a list of floats
127127
"""
128-
pass
128+
return np.ndarray(0)

mapie_v1/regression.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
)
1717
from mapie_v1._utils import transform_confidence_level_to_alpha_list, \
1818
check_method_not_naive, check_cv_not_string, hash_X_y, \
19-
check_if_X_y_different_from_fit, make_intervals_single_if_single_alpha
19+
check_if_X_y_different_from_fit, make_intervals_single_if_single_alpha, \
20+
cast_point_predictions_to_ndarray
2021

2122

2223
class SplitConformalRegressor:
@@ -237,7 +238,7 @@ def predict(
237238
Array of point predictions, with shape (n_samples,).
238239
"""
239240
predictions = self._mapie_regressor.predict(X, alpha=None)
240-
return predictions
241+
return cast_point_predictions_to_ndarray(predictions)
241242

242243

243244
class CrossConformalRegressor:
@@ -439,6 +440,8 @@ def conformalize(
439440
predict_params=predict_params
440441
)
441442

443+
return self
444+
442445
def predict_set(
443446
self,
444447
X: ArrayLike,
@@ -518,7 +521,10 @@ def predict(
518521
self._mapie_regressor._check_agg_function(aggregation_method)
519522
self._mapie_regressor.agg_function = aggregation_method
520523

521-
return self._mapie_regressor.predict(X, alpha=None, ensemble=ensemble)
524+
predictions = self._mapie_regressor.predict(
525+
X, alpha=None, ensemble=ensemble
526+
)
527+
return cast_point_predictions_to_ndarray(predictions)
522528

523529

524530
class JackknifeAfterBootstrapRegressor:
@@ -627,6 +633,7 @@ def fit(
627633
Self
628634
The fitted JackknifeAfterBootstrapRegressor instance.
629635
"""
636+
return self
630637

631638
def conformalize(
632639
self,
@@ -659,6 +666,7 @@ def conformalize(
659666
The JackknifeAfterBootstrapRegressor instance with
660667
calibrated prediction intervals.
661668
"""
669+
return self
662670

663671
def predict_set(
664672
self,
@@ -684,7 +692,7 @@ def predict_set(
684692
Prediction intervals of shape `(n_samples, 2)`,
685693
with lower and upper bounds for each sample.
686694
"""
687-
pass
695+
return np.ndarray(0)
688696

689697
def predict(
690698
self,
@@ -712,7 +720,7 @@ def predict(
712720
NDArray
713721
Array of point predictions, with shape `(n_samples,)`.
714722
"""
715-
pass
723+
return np.ndarray(0)
716724

717725

718726
class ConformalizedQuantileRegressor:
@@ -824,6 +832,7 @@ def fit(
824832
Self
825833
The fitted ConformalizedQuantileRegressor instance.
826834
"""
835+
return self
827836

828837
def conformalize(
829838
self,
@@ -855,6 +864,7 @@ def conformalize(
855864
The ConformalizedQuantileRegressor instance with calibrated
856865
prediction intervals.
857866
"""
867+
return self
858868

859869
def predict_set(
860870
self,
@@ -892,7 +902,7 @@ def predict_set(
892902
Prediction intervals with shape `(n_samples, 2)`, with lower
893903
and upper bounds for each sample.
894904
"""
895-
pass
905+
return np.ndarray(0)
896906

897907
def predict(
898908
self,
@@ -911,7 +921,7 @@ def predict(
911921
NDArray
912922
Array of point predictions with shape `(n_samples,)`.
913923
"""
914-
pass
924+
return np.ndarray(0)
915925

916926

917927
class GibbsConformalRegressor:

0 commit comments

Comments
 (0)