Skip to content
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

pylance warning - Argument of type "list[LGBMRegressor]" cannot be assigned to parameter "models" of type "Models" in function "init" #411

Closed
sumamjose opened this issue Sep 4, 2024 · 1 comment

Comments

@sumamjose
Copy link

sumamjose commented Sep 4, 2024

Description

I'm encountering a type mismatch issue when passing a list of models (regressors) to the MLForecast class. The documentation states that the models parameter can be either a single regressor or a list of regressors as in https://github.com/Nixtla/mlforecast/blob/main/mlforecast/forecast.py#L126. However, when passing a list of models or the model alone, I receive a type error indicating that Argument of type "list[LGBMRegressor]" cannot be assigned to parameter "models" of type "Models" in function "init"

Python 3.10.12
mlforecast 0.13.4

According to the documentation:

Parameters

models : regressor or list of regressors
Models that will be trained and used to compute the forecasts.

Sample code:


# Import necessary libraries
from mlforecast import MLForecast
import lightgbm as lgb
from sklearn.linear_model import LinearRegression
import pandas as pd

# Create a simple DataFrame to simulate data
train_df = pd.DataFrame({
    'ds': pd.date_range(start='2020-01-01', periods=100, freq='D'),
    'y': range(100),
})

# Define a list of models
models = [
    lgb.LGBMRegressor(random_state=0, verbosity=-1),
    LinearRegression(),
]

# Initialize MLForecast with the list of models
try:
    fcst = MLForecast(models=models, freq='D')
    print("Model initialized successfully.")
except TypeError as e:
    print(f"Encountered an error: {e}")

the above code works fine without warning, but when using

models = [lgb.LGBMRegressor(random_state=0, verbosity=-1)]

or

models = lgb.LGBMRegressor(random_state=0, verbosity=-1) 

it gives respective pylance warnings as below in vscode:

Argument of type "list[LGBMRegressor]" cannot be assigned to parameter "models" of type "Models" in function "__init__"
  Type "list[LGBMRegressor]" is incompatible with type "Models"
    "list[LGBMRegressor]" is incompatible with "BaseEstimator"
    "list[LGBMRegressor]" is incompatible with "List[BaseEstimator]"
      Type parameter "_T@list" is invariant, but "LGBMRegressor" is not the same as "BaseEstimator"
      Consider switching from "list" to "Sequence" which is covariant
    "list[LGBMRegressor]" is incompatible with "Dict[str, BaseEstimator]"Pylance[reportArgumentType](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportArgumentType)

or 

Argument of type "LGBMRegressor" cannot be assigned to parameter "models" of type "Models" in function "__init__"
  Type "LGBMRegressor" is incompatible with type "Models"
    "LGBMRegressor" is incompatible with "BaseEstimator"
    "LGBMRegressor" is incompatible with "List[BaseEstimator]"
    "LGBMRegressor" is incompatible with "Dict[str, BaseEstimator]"Pylance[reportArgumentType](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportArgumentType)

Could you please look into why that is happening despite the documentation?

Link

No response

@jmoralez
Copy link
Member

Hey @sumamjose, thanks for using mlforecast. This seems to be related to LightGBM, it seems that the linter picks up the dummy classes defined in the compat module instead of the scikit learn ones. Here's a minimal example (without mlforecast) where we can see the issue:
Screenshot from 2024-10-29 20-03-06

Feel free to open an issue there, it seems that what XGBoost does (defining those dummy base classes as object instead) works.

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

2 participants