-
-
Notifications
You must be signed in to change notification settings - Fork 26k
Closed
Labels
Description
set_{method}_request
methods are added to all estimators inheriting from BaseEstimator
, around this part of the code:
scikit-learn/sklearn/utils/_metadata_requests.py
Lines 1004 to 1013 in e9f5676
for method in METHODS: | |
mmr = getattr(requests, method) | |
# set ``set_{method}_request``` methods | |
if not len(mmr.requests): | |
continue | |
setattr( | |
cls, | |
f"set_{method}_request", | |
RequestMethod(method, sorted(mmr.requests.keys())), | |
) |
Since mypy
passes the code to AST and that's how they check for types and available methods, any code using these methods such as est.set_fit_request(sample_weight=True)
would make mypy
to fail with and error similar to:
error: "LinearRegression" has no attribute "set_fit_request"
There are at least two ways to fix this issue:
- Write a plugin for
mypy
to somehow fix the issues:
mypy
's reference: https://mypy.readthedocs.io/en/stable/extending_mypy.htmlnumpy
's example: https://numpy.org/devdocs/reference/typing.html
- Declare all possible methods for all objects for type checkers even if they don't exist. This would silence the type checker errors, but it's a bit hackish. For this, we'd need to change the declaration of the
_MetadataRequester
and add:
from typing import TYPE_CHECKING
class _MetadataRequester:
"""Mixin class for adding metadata request functionality.
.. versionadded:: 1.2
"""
if TYPE_CHECKING:
set_fit_request: Callable[..., Any]
set_partial_fit_request: Callable[..., Any]
set_predict_request: Callable[..., Any]
set_predict_proba_request: Callable[..., Any]
...
def __init_subclass__(cls, **kwargs):
...
This wouldn't pollute the runtime env since in runtime TYPE_CHECKING
is always False
.
I'm in favor of adding the second option as a temporary solution until somebody implemented the plugin, if they do.