5

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

1
  • It's possible for make_blobs to return three values when return_centers=True, but that shouldn't be the case here (and even if that was the case, y should slurp the remaining values). Maybe VSCode is complaining about return types? Commented Jan 7, 2023 at 23:15

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

5 Comments

But should I still receive this warning message where I just expects anything from make_blobs function? Like I dont expect any specific return type
You expect there to be exactly two values returned by make_blobs.
Yes, but with unknown type and thats why I got this error message from pyright
Something is known about the type you expect: you expect there to be two items, not three. That's what the warning is about. I've added to the answer a way to work around this particular issue.
Ah yes in that case you are correct.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.