pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.33k stars 1.14k forks source link

False-positive E1137 (unsupported-assignment-operation) with np.empty_like #2767

Closed bersbersbers closed 4 years ago

bersbersbers commented 5 years ago

Steps to reproduce

pylint this file:

"""X"""
import numpy as np

X = np.empty_like([1])
X[:] = 2

Current behavior

************* Module bug
bug.py:5:0: E1137: 'X' does not support item assignment (unsupported-assignment-operation)

Expected behavior

No error, code runs fine in python bug.py

pylint --version output

pylint 2.2.2
astroid 2.1.0
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)]`
PCManticore commented 5 years ago

Hi @bersbersbers thanks for reporting the issue.

martinholmer commented 5 years ago

Same problem as reported by @bersbersbers in #2767 occurs when using np.zeros_like(...).

Also, use of np.logical_or(..., ...) generates an assignment-from-no-return pylint error.

Neither of these errors were generated by earlier versions of pylint.

cc @PCManticore

hippo91 commented 5 years ago

@martinholmer @bersbersbers thank for the report. In fact it's very close to #2746 #2694 #2784.

In early times of pylint the brain used to deal with numpy was quite simple. We, then, had a lot of no-member false positives. They were due to the fact that some numpy modules are written in c language, especially those at the core of numpy and thus astroid (the pylint inference engine) can not infer any object defined in those modules.

To correct this, i wrote python ghosts of those numpy c modules in order astroid/pylint to deal with them correctly (PyCQA/astroid#567 PyCQA/astroid#486). It implied studying numpy deeply and wrote a python ghost for every function/class that numpy defines in c language. At this point almost all classes or functions are defined and thus almost all no-member false positives should have gone away. However the call to such functions/methods cannot yet be inferred. That's why you are facing such problems.

I'am currently working on an improvment of astroids numpy brain. It's quite a hard work and have not so much time to spent on it but i hope it will be finished quite soon.

Sorry for that long message but i wanted to explain why you and other numpy + pylint users are facing such problems.

martinholmer commented 5 years ago

@hippo91 said in issue #2767:

Sorry for that long message but I wanted to explain why you and other numpy + pylint users are facing such problems.

Thanks for the helpful explanation, and thanks especially for all your ongoing work on this problem.

jordyjwilliams commented 5 years ago

not sure if this helps: but I found that the following throwed a unsupported-assignment-operation

my_array = np.zeros_like(another_array)
condition_array = _np.where(_np.less(different_array, my_array))
my_array[condition_array] = values

that could be solved by calling np.zeros(another_array.shape) instead of zeros_like

martinholmer commented 5 years ago

@jordyjwilliams said in issue #2767:

I found that the following throwed a unsupported-assignment-operation pylint warning:

my_array = np.zeros_like(another_array)
condition_array = _np.where(_np.less(different_array, my_array))
my_array[condition_array] = values

that could be solved by calling np.zeros(another_array.shape) instead of np.zeros_like(another_array).

Thanks for the useful tip!

hippo91 commented 4 years ago

@martinholmer , @jordyjwilliams The problem you were facing should have disappeared with, at least, the 2.3 version of astroid. That's why i'm closing this issue. Feel free to reopen it if necessary.

EmilyBourne commented 10 months ago

I am also experiencing this problem. The example code:

"""X"""
import numpy as np

X = np.empty_like([1])
X[:] = 2

works as expected, but the almost identical code:

"""X"""
from numpy import empty_like

X = empty_like([1])
X[:] = 2

raises the error:

************* Module bug
bug.py:5:0: E1137: 'X' does not support item assignment (unsupported-assignment-operation)

pylint --version output

pylint 3.0.3
astroid 3.0.2
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
Pierre-Sassoulas commented 10 months ago

@EmilyBourne you should open a new issue, this is going to get lost otherwise