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 E1130 invalid-unary-operand-type for Numpy array #2436

Closed
cdeil opened this issue Aug 22, 2018 · 4 comments · Fixed by pylint-dev/astroid#643, pythoninja/adarklib#29, thermondo/stanley#208 or ChrisRBe/PP-P2P-Parser#85
Assignees
Labels
Astroid Related to astroid Bug 🪲

Comments

@cdeil
Copy link

cdeil commented Aug 22, 2018

Pylint emits a false positive E1130 invalid-unary-operand-type error for Numpy arrays:

import numpy as np
a = np.array([1, 2])
b = -a

Actually, in this case, it emits two:

$ pylint test.py  -E
************* Module test
test.py:3:4: E1130: bad operand type for unary -: recarray (invalid-unary-operand-type)
test.py:3:4: E1130: bad operand type for unary -: list (invalid-unary-operand-type)

This is not just for np.array, I get a similar false positive for np.atleast_1d, which also returns Numpy arrays. But np.array is super common in scientific Python projects.

$ pylint --version
pylint 2.2.0
astroid 2.0.4
Python 3.6.0 | packaged by conda-forge | (default, Feb 10 2017, 07:08:35) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)]

This issue was already mentioned in #1472 and #1497 , but @PCManticore you suggested to split out a new issue with a simple test case that doesn't involve flow control in #1472 (comment) , so there you have it.

:-)

Thanks for all your work on pylint, it's a huge help!

@PCManticore
Copy link
Contributor

Thanks for creating an issue, can confirm the bug!

@PCManticore PCManticore added Bug 🪲 Astroid Related to astroid labels Aug 23, 2018
@NeilGirdhar
Copy link

This also triggers on scalar types like numpy.float32.

@hippo91 hippo91 self-assigned this Jan 27, 2019
@Max16hr
Copy link

Max16hr commented Jan 30, 2019

Bug is still there.
In my case, np.linspace returns a np.ndarray object. But when I try to negate that I will get a pylint error:

bad operand type for unary -: tuple

Obviously, this is a bug. There is no tuple at all.

(Maybe this is the wrong ticket for my issue? I’m not sure.)

@hippo91
Copy link
Contributor

hippo91 commented Feb 3, 2019

@cdeil in fact there are two problems:

  • the first one, and easiest to solve, is that astroid (which is responsible for inference in pylint) doesn't recognize the unary operators - or ~ as valid operators for numpy types;
  • the second one, and a little bit more complex, is that astroid infers the result of the function array (or linspace in @Max16hr's case) as a recarray, which is correct, but also a list or a tuple depending on the type you passed as argument in the function.

Currently I have found a solution for both problems. I'm trying to make it the more general as possible.

clrpackages pushed a commit to clearlinux-pkgs/astroid that referenced this issue Feb 28, 2019
…ed single and double quotes.

Azeem Bande-Ali (1):
      Docs: Fixing transformation example to be functional (#640)

Benjamin Elven (1):
      Added 2 more missing _ssl constants (#648)

Bianca Power (1):
      Fix typo in description for brain_attrs (#638)

Claudiu Popa (48):
      Add reversed() to Enum classes
      Use a string for the name of the enum instead of grabbing it as a variable
      Added special support for `enum.IntFlag`
      Pin typed_ast so a new release won't break the pylint's pragmas
      Fix a bug where an Attribute used as a base class was triggering a crash
      Add missing _ssl constant. Close pylint-dev/pylint#2629
      ``typing`` types have the `__args__` property. Close pylint-dev/pylint#2419
      Make sure the test does not crash if the directory already exists
      Revert path copying in InferenceContext()
      Prevent crashing when processing ``enums`` with mixed single and double quotes.
      Move infer_name_module() in pylint and remove it from here
      Remove test that does not work actually work consistently on all Python versions
      Disable the pylint job until we fix the clone inference bugs
      Revert "Disable the pylint job until we fix the clone inference bugs"
      Add brain tips for `http.client`. Close pylint-dev/pylint#2687
      Revert "Remove the restore_path() method"
      Add a registry for builtin exception models. Close pylint-dev/pylint#1432
      Disable test on Python 3.4
      Change the filter to use equal
      Use a different prefix for the object model methods
      Added more builtin exceptions attributes. Close #580
      Remove StopIteration handling which should not leak at all from inference
      Replace a couple of returns with explicit InferenceError exceptions
      Make sure that Attribute.infer has a protection against StopIterations
      Add a method to the manager to retrieve the builtins module
      Remove the restriction that "old style classes" cannot have a MRO.
      Remove inadvertent test which still does not have a fix
      Adjust the pull request template a little bit
      Make the astroid bootstrapping take care of builtins such as Generator
      Prepare a dev release
      Fix a recursion error caused by inferring the ``slice`` builtin.
      Bump typed_ast to 1.2.0
      ``brain_numpy`` returns an undefined type for ``numpy`` methods to avoid ``assignment-from-no-retur
      Remove inadvertent returns from class bodies
      Fix a crash in the ``enum`` brain tip caused by non-assign members in class definitions.
      Support non-const nodes as values of Enum attributes.
      Add a proper ``strerror`` inference to the ``OSError`` exceptions.
      Fix a crash with ``typing.NamedTuple`` and empty fields. Close pylint-dev/pylint#2745
      Update to typed-ast 1.3 and drop the Python 3.7 restriction
      Install typed ast 1.3 for Python 3.7 and earlier versions for Python 3.6
      Do a new dev release
      Use the same versions in tox as in __pkginfo__
      Use the right pytest methods for setup and teardown
      Don't clear the manager caching any longer
      Remove merge marker
      Prepare 2.2.0
      Disable test that does not make sense on versions older than Python 3.6
      When picking the parse function, make sure to parse with the right minor version

Nick Drozd (1):
      Cut generator flags

Peter Talley (1):
      Add new Popen kwarg 'text' in Python 3.7

Tomas Gavenciak (2):
      Remove else after return/raise
      Extend detection of classes defined with attr

hippo91 (11):
      Just breakpoint to debug and mark the origin of the problem
      Revert "Just breakpoint to debug and mark the origin of the problem"
      Refactoring chained comparison (#636)
      Avoid statement deletion in the _filter_stmts method of the LookupMixin class for PartialFunction
      Adding support for unary operators for numpy ndarray and numbers. Adding hook to filter results of infer_call_result from list and tuples for function array and linspace
      Refactoring to code DRY
      Add a unittest checking that the call to numpy.array function is not inferred as a list or a tuple
      Refactors and generalizes unittests dealing with tuple or list inference of numpy function calls
      Add of ChangeLog entry
      Format according to Black
      Add support of unary operators to numpy types.

What's New in astroid 2.2.0?
============================
Release Date: 2019-02-27

* Fix a bug concerning inference of calls to numpy function that should not return Tuple or List instances.

 Close pylint-dev/pylint#2436

* ``typed_ast`` gets installed for Python 3.7, meaning type comments can now work on 3.7.

* Fix a bug concerning inference of unary operators on numpy types.

  Close pylint-dev/pylint#2436 (first part)

(NEWS truncated at 15 lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment