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

Skip to content

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

Closed
@adrinjalali

Description

@adrinjalali

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions