Pylint: False-positive E1137 (unsupported-assignment-operation) with np.empty_like

Created on 27 Feb 2019  路  7Comments  路  Source: PyCQA/pylint

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)]`
astroid bug regression

Most helpful comment

@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.

All 7 comments

Hi @bersbersbers thanks for reporting the issue.

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

@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.

@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.

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

@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!

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PCManticore picture PCManticore  路  3Comments

lancelote picture lancelote  路  3Comments

PCManticore picture PCManticore  路  3Comments

DevynCJohnson picture DevynCJohnson  路  3Comments

Hubro picture Hubro  路  3Comments