Weird scikit-learn Python intellisense error message

Question:

Lately I was doing some ML stuff with Python using scikit-learn package.
I wanted to use make_blobs() function so I began writing code for example:

X, y = make_blobs(n_samples=m, centers=2, n_features=2,  center_box=(80, 100))

and of course this is fine.

However while coding next lines my Intellisense within Visual Studio Code (I have only Microsoft addons for Python installed just to be clear) started to showing weird error on that line I mentioned before.

Here’s full error message:

Expression with type "tuple[Unknown | list[Unknown] | NDArray[float64], Unknown | list[Unknown] | NDArray[Any], ndarray[Any, dtype[float64]] | Any] | tuple[Unknown | list[Unknown] | NDArray[float64], Unknown | list[Unknown] | NDArray[Any]]" cannot be assigned to target tuple
  Type "tuple[Unknown | list[Unknown] | NDArray[float64], Unknown | list[Unknown] | NDArray[Any], ndarray[Any, dtype[float64]] | Any]" is incompatible with target tuple
     Element size mismatch; expected 2 but received 3

Please notice the last sentence. Element size mismatch where make_blobs() function returned 3 elements. What???
I’ve checked scikit-learn documentation for make_blobs() function and I’ve read that on default make_blobs() returns only 2 elements not 3.
3 elements can be returned when return_centers is set to True, where I have not set that to true as you can see in my example.

Ok, maybe I’ll try to expect those 3 elements, so I modified that line

X, y, _ = make_blobs(n_samples=m, centers=2, n_features=2,  center_box=(80, 100))

and well… this is the error message…

Expression with type "tuple[Unknown | list[Unknown] | NDArray[float64], Unknown | list[Unknown] | NDArray[Any], ndarray[Any, dtype[float64]] | Any] | tuple[Unknown | list[Unknown] | NDArray[float64], Unknown | list[Unknown] | NDArray[Any]]" cannot be assigned to target tuple
  Type "tuple[Unknown | list[Unknown] | NDArray[float64], Unknown | list[Unknown] | NDArray[Any]]" is incompatible with target tuple
    Element size mismatch; expected 3 but received 2

Now it returns 2 elements?!

What I have tried next is:

  • reinstall scikit-learn package. Same effect
  • purging Python with all it files. Same effetc
  • reinstalling Microsoft python extension for vscode. Same effect

Clearly it is some kind of intellisense issue, because running the code works fine, but what cause this behaviour?

Python I used was 3.10.9 and 3.11.1.

Running on Windows 10 22H2 19045.2364.

VSCode up-to-date.

For completeness scikit-learn version is 1.2.0

Asked By: gunhasiz

||

Answers:

This is a known behaviour of pyright (which is a Python type checker used in Intellisense). It raises a return type mismatch warning if there is at least one return statement within the function that’s incompatible with what you’re expecting. See a similar issue in their repo for more details and an explanation from one of the maintainers.

You can suppress type checking for this particular line with a comment though:

X, y = make_blobs(n_samples=m, centers=2, n_features=2,  center_box=(80, 100)) # pyright: ignore
Answered By: Alex Bochkarev

I think I found a solution to my problem.

Checking my settings.json I found setting python.analysis.typeCheckingMode which was set to basic.
I’ve changed the value of that setting to strict and then back to basic and it kinda worked? Because I no longer have that error message I mention.

However @Alex Bochkarev answer is also correct.

Answered By: gunhasiz