Thanks to visit codestin.com
Credit goes to github.com

Skip to content

SLEP006: mypy can't find set_{method}_request methods #23933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tracked by #22893
adrinjalali opened this issue Jul 17, 2022 · 1 comment
Closed
Tracked by #22893

SLEP006: mypy can't find set_{method}_request methods #23933

adrinjalali opened this issue Jul 17, 2022 · 1 comment

Comments

@adrinjalali
Copy link
Member

set_{method}_request methods are added to all estimators inheriting from BaseEstimator, around this part of the code:

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:

  1. Write a plugin for mypy to somehow fix the issues:
  1. 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.

@github-actions github-actions bot added the Needs Triage Issue requires triage label Jul 17, 2022
@thomasjpfan
Copy link
Member

We likely need to go through the second option always. The mypy plugin system does not work with other typing checking tools such as Pylance.

@thomasjpfan thomasjpfan added New Feature and removed Needs Triage Issue requires triage labels Jul 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants