Skip to content

Commit 1fb1225

Browse files
authored
Use typing_extensions.Self (#358)
1 parent 6be1479 commit 1fb1225

File tree

109 files changed

+514
-714
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+514
-714
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ ignore = [
5858
"PYI001",
5959
"PYI002",
6060
"PYI017",
61-
"PYI019", # Request for more autofixes: https://github.com/astral-sh/ruff/issues/15798
6261
"PYI024",
6362
"PYI048",
6463
"PYI051", # Request for autofix: https://github.com/astral-sh/ruff/issues/14185

stubs/sklearn/base.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import re
55
import warnings
66
from collections import defaultdict as defaultdict
77
from collections.abc import Iterable
8-
from typing import Any, ClassVar, TypeVar
8+
from typing import Any, ClassVar
9+
from typing_extensions import Self
910

1011
import numpy as np
1112
from numpy import ndarray
@@ -18,16 +19,14 @@ from .utils._param_validation import validate_parameter_constraints as validate_
1819
from .utils._set_output import _SetOutputMixin
1920
from .utils.validation import check_array as check_array, check_is_fitted as check_is_fitted, check_X_y as check_X_y
2021

21-
BaseEstimator_Self = TypeVar("BaseEstimator_Self", bound=BaseEstimator)
22-
2322
# Author: Gael Varoquaux <gael.varoquaux@normalesup.org>
2423
# License: BSD 3 clause
2524

2625
def clone(estimator: BaseEstimator | Iterable[BaseEstimator], *, safe: bool = True) -> Any: ...
2726

2827
class BaseEstimator:
2928
def get_params(self, deep: bool = True) -> dict: ...
30-
def set_params(self: BaseEstimator_Self, **params) -> BaseEstimator_Self: ...
29+
def set_params(self, **params) -> Self: ...
3130
def __repr__(self, N_CHAR_MAX: int = 700) -> str: ...
3231
def __getstate__(self): ...
3332
def __setstate__(self, state) -> None: ...

stubs/sklearn/calibration.pyi

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ from functools import partial as partial
44
from inspect import signature as signature
55
from math import log as log
66
from numbers import Integral as Integral
7-
from typing import ClassVar, Literal, TypeVar
7+
from typing import ClassVar, Literal
8+
from typing_extensions import Self
89

910
import numpy as np
1011
from matplotlib.artist import Artist
@@ -33,9 +34,6 @@ from .utils.multiclass import check_classification_targets as check_classificati
3334
from .utils.parallel import Parallel as Parallel, delayed as delayed
3435
from .utils.validation import check_consistent_length as check_consistent_length, check_is_fitted as check_is_fitted
3536

36-
CalibratedClassifierCV_Self = TypeVar("CalibratedClassifierCV_Self", bound=CalibratedClassifierCV)
37-
_SigmoidCalibration_Self = TypeVar("_SigmoidCalibration_Self", bound=_SigmoidCalibration)
38-
3937
class CalibratedClassifierCV(ClassifierMixin, MetaEstimatorMixin, BaseEstimator):
4038
calibrated_classifiers_: list = ...
4139
feature_names_in_: ndarray = ...
@@ -55,12 +53,12 @@ class CalibratedClassifierCV(ClassifierMixin, MetaEstimatorMixin, BaseEstimator)
5553
base_estimator: str | BaseEstimator = "deprecated",
5654
) -> None: ...
5755
def fit(
58-
self: CalibratedClassifierCV_Self,
56+
self,
5957
X: MatrixLike,
6058
y: ArrayLike,
6159
sample_weight: None | ArrayLike = None,
6260
**fit_params,
63-
) -> CalibratedClassifierCV_Self: ...
61+
) -> Self: ...
6462
def predict_proba(self, X: MatrixLike) -> ndarray: ...
6563
def predict(self, X: MatrixLike) -> ndarray: ...
6664

@@ -80,11 +78,11 @@ class _SigmoidCalibration(RegressorMixin, BaseEstimator):
8078
a_: float = ...
8179

8280
def fit(
83-
self: _SigmoidCalibration_Self,
81+
self,
8482
X: ArrayLike,
8583
y: ArrayLike,
8684
sample_weight: None | ArrayLike = None,
87-
) -> _SigmoidCalibration_Self: ...
85+
) -> Self: ...
8886
def predict(self, T: ArrayLike) -> ndarray: ...
8987

9088
def calibration_curve(

stubs/sklearn/cluster/_affinity_propagation.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import warnings
22
from numbers import Integral as Integral, Real as Real
3-
from typing import Any, ClassVar, Literal, TypeVar
3+
from typing import Any, ClassVar, Literal
4+
from typing_extensions import Self
45

56
import numpy as np
67
from numpy import ndarray
@@ -15,8 +16,6 @@ from ..utils import as_float_array as as_float_array, check_random_state as chec
1516
from ..utils._param_validation import Interval as Interval, StrOptions as StrOptions
1617
from ..utils.validation import check_is_fitted as check_is_fitted
1718

18-
AffinityPropagation_Self = TypeVar("AffinityPropagation_Self", bound=AffinityPropagation)
19-
2019
###############################################################################
2120
# Public API
2221

@@ -56,6 +55,6 @@ class AffinityPropagation(ClusterMixin, BaseEstimator):
5655
verbose: bool = False,
5756
random_state: RandomState | None | Int = None,
5857
) -> None: ...
59-
def fit(self: AffinityPropagation_Self, X: MatrixLike, y: Any = None) -> AffinityPropagation_Self: ...
58+
def fit(self, X: MatrixLike, y: Any = None) -> Self: ...
6059
def predict(self, X: MatrixLike | ArrayLike) -> ndarray: ...
6160
def fit_predict(self, X: MatrixLike, y: Any = None) -> ndarray: ...

stubs/sklearn/cluster/_agglomerative.pyi

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import warnings
22
from heapq import heapify as heapify, heappop as heappop, heappush as heappush, heappushpop as heappushpop
33
from numbers import Integral as Integral, Real as Real
4-
from typing import Any, Callable, ClassVar, Literal, TypeVar
4+
from typing import Any, Callable, ClassVar, Literal
5+
from typing_extensions import Self
56

67
import numpy as np
78
from joblib import Memory
@@ -21,9 +22,6 @@ from ..utils._param_validation import HasMethods as HasMethods, Hidden as Hidden
2122
from ..utils.validation import check_memory as check_memory
2223
from ._feature_agglomeration import AgglomerationTransform
2324

24-
FeatureAgglomeration_Self = TypeVar("FeatureAgglomeration_Self", bound=FeatureAgglomeration)
25-
AgglomerativeClustering_Self = TypeVar("AgglomerativeClustering_Self", bound=AgglomerativeClustering)
26-
2725
###############################################################################
2826
# Hierarchical tree building functions
2927

@@ -74,7 +72,7 @@ class AgglomerativeClustering(ClusterMixin, BaseEstimator):
7472
distance_threshold: None | Float = None,
7573
compute_distances: bool = False,
7674
) -> None: ...
77-
def fit(self: AgglomerativeClustering_Self, X: MatrixLike, y: Any = None) -> AgglomerativeClustering_Self: ...
75+
def fit(self, X: MatrixLike, y: Any = None) -> Self: ...
7876
def fit_predict(self, X: MatrixLike, y: Any = None) -> ndarray: ...
7977

8078
class FeatureAgglomeration(ClassNamePrefixFeaturesOutMixin, AgglomerativeClustering, AgglomerationTransform):
@@ -103,5 +101,5 @@ class FeatureAgglomeration(ClassNamePrefixFeaturesOutMixin, AgglomerativeCluster
103101
distance_threshold: None | Float = None,
104102
compute_distances: bool = False,
105103
) -> None: ...
106-
def fit(self: FeatureAgglomeration_Self, X: MatrixLike, y: Any = None) -> FeatureAgglomeration_Self: ...
104+
def fit(self, X: MatrixLike, y: Any = None) -> Self: ...
107105
def fit_predict(self): ...

stubs/sklearn/cluster/_bicluster.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from abc import ABCMeta, abstractmethod
22
from numbers import Integral as Integral
3-
from typing import Any, ClassVar, Literal, TypeVar
3+
from typing import Any, ClassVar, Literal
4+
from typing_extensions import Self
45

56
import numpy as np
67
from numpy import ndarray
@@ -21,8 +22,6 @@ from ..utils.extmath import (
2122
from ..utils.validation import assert_all_finite as assert_all_finite
2223
from . import KMeans as KMeans, MiniBatchKMeans as MiniBatchKMeans
2324

24-
BaseSpectral_Self = TypeVar("BaseSpectral_Self", bound=BaseSpectral)
25-
2625
__all__ = ["SpectralCoclustering", "SpectralBiclustering"]
2726

2827
class BaseSpectral(BiclusterMixin, BaseEstimator, metaclass=ABCMeta):
@@ -39,7 +38,7 @@ class BaseSpectral(BiclusterMixin, BaseEstimator, metaclass=ABCMeta):
3938
n_init: int = 10,
4039
random_state: None | int = None,
4140
) -> None: ...
42-
def fit(self: BaseSpectral_Self, X: MatrixLike, y: Any = None) -> BaseSpectral_Self: ...
41+
def fit(self, X: MatrixLike, y: Any = None) -> Self: ...
4342

4443
class SpectralCoclustering(BaseSpectral):
4544
feature_names_in_: ndarray = ...

stubs/sklearn/cluster/_birch.pyi

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import warnings
22
from math import sqrt as sqrt
33
from numbers import Integral as Integral, Real as Real
4-
from typing import Any, ClassVar, TypeVar
4+
from typing import Any, ClassVar
5+
from typing_extensions import Self
56

67
import numpy as np
78
from numpy import ndarray
@@ -19,8 +20,6 @@ from ..utils.extmath import row_norms as row_norms
1920
from ..utils.validation import check_is_fitted as check_is_fitted
2021
from . import AgglomerativeClustering as AgglomerativeClustering
2122

22-
Birch_Self = TypeVar("Birch_Self", bound=Birch)
23-
2423
# Authors: Manoj Kumar <manojkumarsivaraj334@gmail.com>
2524
# Alexandre Gramfort <alexandre.gramfort@telecom-paristech.fr>
2625
# Joel Nothman <joel.nothman@gmail.com>
@@ -86,7 +85,7 @@ class Birch(ClassNamePrefixFeaturesOutMixin, ClusterMixin, TransformerMixin, Bas
8685
compute_labels: bool = True,
8786
copy: bool = True,
8887
) -> None: ...
89-
def fit(self: Birch_Self, X: MatrixLike | ArrayLike, y: Any = None) -> Birch_Self: ...
90-
def partial_fit(self: Birch_Self, X: None | MatrixLike | ArrayLike = None, y: Any = None) -> Birch_Self: ...
88+
def fit(self, X: MatrixLike | ArrayLike, y: Any = None) -> Self: ...
89+
def partial_fit(self, X: None | MatrixLike | ArrayLike = None, y: Any = None) -> Self: ...
9190
def predict(self, X: MatrixLike | ArrayLike) -> ndarray: ...
9291
def transform(self, X: MatrixLike | ArrayLike) -> ndarray | spmatrix: ...

stubs/sklearn/cluster/_bisect_k_means.pyi

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import warnings
22
from collections.abc import Iterator
3-
from typing import Any, Callable, ClassVar, Literal, TypeVar
3+
from typing import Any, Callable, ClassVar, Literal
4+
from typing_extensions import Self
45

56
import numpy as np
67
import scipy.sparse as sp
@@ -13,8 +14,6 @@ from ..utils.extmath import row_norms as row_norms
1314
from ..utils.validation import check_is_fitted as check_is_fitted, check_random_state as check_random_state
1415
from ._kmeans import _BaseKMeans
1516

16-
BisectingKMeans_Self = TypeVar("BisectingKMeans_Self", bound=BisectingKMeans)
17-
1817
# Author: Michal Krawczyk <mkrwczyk.1@gmail.com>
1918

2019
class _BisectingTree:
@@ -47,9 +46,9 @@ class BisectingKMeans(_BaseKMeans):
4746
bisecting_strategy: Literal["biggest_inertia", "largest_cluster"] = "biggest_inertia",
4847
) -> None: ...
4948
def fit(
50-
self: BisectingKMeans_Self,
49+
self,
5150
X: MatrixLike | ArrayLike,
5251
y: Any = None,
5352
sample_weight: None | ArrayLike = None,
54-
) -> BisectingKMeans_Self: ...
53+
) -> Self: ...
5554
def predict(self, X: MatrixLike | ArrayLike) -> ndarray: ...

stubs/sklearn/cluster/_dbscan.pyi

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import warnings
22
from numbers import Integral as Integral, Real as Real
3-
from typing import Any, Callable, ClassVar, Literal, TypeVar
3+
from typing import Any, Callable, ClassVar, Literal
4+
from typing_extensions import Self
45

56
import numpy as np
67
from numpy import ndarray
@@ -12,8 +13,6 @@ from ..neighbors import NearestNeighbors as NearestNeighbors
1213
from ..utils._param_validation import Interval as Interval, StrOptions as StrOptions
1314
from ._dbscan_inner import dbscan_inner as dbscan_inner
1415

15-
DBSCAN_Self = TypeVar("DBSCAN_Self", bound=DBSCAN)
16-
1716
# Author: Robert Layton <robertlayton@gmail.com>
1817
# Joel Nothman <joel.nothman@gmail.com>
1918
# Lars Buitinck
@@ -56,9 +55,9 @@ class DBSCAN(ClusterMixin, BaseEstimator):
5655
n_jobs: None | Int = None,
5756
) -> None: ...
5857
def fit(
59-
self: DBSCAN_Self,
58+
self,
6059
X: MatrixLike,
6160
y: Any = None,
6261
sample_weight: None | ArrayLike = None,
63-
) -> DBSCAN_Self: ...
62+
) -> Self: ...
6463
def fit_predict(self, X: MatrixLike, y: Any = None, sample_weight: None | ArrayLike = None) -> ndarray: ...

stubs/sklearn/cluster/_kmeans.pyi

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import warnings
22
from abc import ABC, abstractmethod as abstractmethod
33
from numbers import Integral as Integral, Real as Real
4-
from typing import Any, Callable, ClassVar, Literal, TypeVar
4+
from typing import Any, Callable, ClassVar, Literal
5+
from typing_extensions import Self
56

67
import numpy as np
78
import scipy.sparse as sp
@@ -36,9 +37,6 @@ from ._k_means_lloyd import (
3637
lloyd_iter_chunked_sparse as lloyd_iter_chunked_sparse,
3738
)
3839

39-
KMeans_Self = TypeVar("KMeans_Self", bound=KMeans)
40-
MiniBatchKMeans_Self = TypeVar("MiniBatchKMeans_Self", bound=MiniBatchKMeans)
41-
4240
###############################################################################
4341
# Initialization heuristic
4442

@@ -125,11 +123,11 @@ class KMeans(_BaseKMeans):
125123
algorithm: Literal["lloyd", "elkan", "auto", "full"] = "lloyd",
126124
) -> None: ...
127125
def fit(
128-
self: KMeans_Self,
126+
self,
129127
X: MatrixLike | ArrayLike,
130128
y: Any = None,
131129
sample_weight: None | ArrayLike = None,
132-
) -> KMeans_Self: ...
130+
) -> Self: ...
133131

134132
class MiniBatchKMeans(_BaseKMeans):
135133
feature_names_in_: ndarray = ...
@@ -159,14 +157,14 @@ class MiniBatchKMeans(_BaseKMeans):
159157
reassignment_ratio: Float = 0.01,
160158
) -> None: ...
161159
def fit(
162-
self: MiniBatchKMeans_Self,
160+
self,
163161
X: MatrixLike | ArrayLike,
164162
y: Any = None,
165163
sample_weight: None | ArrayLike = None,
166-
) -> MiniBatchKMeans_Self: ...
164+
) -> Self: ...
167165
def partial_fit(
168-
self: MiniBatchKMeans_Self,
166+
self,
169167
X: MatrixLike | ArrayLike,
170168
y: Any = None,
171169
sample_weight: None | ArrayLike = None,
172-
) -> MiniBatchKMeans_Self: ...
170+
) -> Self: ...

0 commit comments

Comments
 (0)