Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bersbersbers opened this issue Feb 27, 2019 · 9 comments
Closed
Assignees
Labels

Comments

@bersbersbers
Copy link

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)]`
@bersbersbers bersbersbers changed the title False-positive E1137 with np.empty_like False-positive E1137 (unsupported-assignment-operation) with np.empty_like Feb 27, 2019
@PCManticore PCManticore added Bug 🪲 Astroid Related to astroid labels Feb 27, 2019
@PCManticore
Copy link
Contributor

Hi @bersbersbers thanks for reporting the issue.

@martinholmer
Copy link

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
Copy link
Contributor

hippo91 commented Mar 30, 2019

@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 (pylint-dev/astroid#567 pylint-dev/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
Copy link

@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
Copy link

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
Copy link

@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
Copy link
Contributor

hippo91 commented Dec 23, 2019

@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
Copy link

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants