Nixtla / mlforecast

Scalable machine 🤖 learning for time series forecasting.
https://nixtlaverse.nixtla.io/mlforecast
Apache License 2.0
860 stars 85 forks source link

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

Open sumamjose opened 1 month ago

sumamjose commented 1 month ago

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]"PylancereportArgumentType

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]"PylancereportArgumentType

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

Link

No response