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

invalid-unary-operand-type error false positive #1472

Open
pbaughman opened this issue May 8, 2017 · 9 comments
Open

invalid-unary-operand-type error false positive #1472

pbaughman opened this issue May 8, 2017 · 9 comments
Labels
Control flow Requires control flow understanding Hacktoberfest Help wanted 🙏 Outside help would be appreciated, good for new contributors High effort 🏋 Difficult solution or problem to solve High priority Issue with more than 10 reactions

Comments

@pbaughman
Copy link

pbaughman commented May 8, 2017

Steps to reproduce

  1. create a file called test.py containing the following:
def frob(value=None):
    if value is not None:
        return -value
    return value
  1. pylint test.py

Current behavior

pylint output contains E: 3,15: bad operand type for unary -: NoneType (invalid-unary-operand-type)

Expected behavior

There should be no invalid-unary-operator-type error for this line because it is not possible for value to be None here

pylint --version output

pete@dev:~$ pylint --version
No config file found, using default configuration
pylint 1.7.1, 
astroid 1.5.2
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4]
@rogalski rogalski added the Control flow Requires control flow understanding label May 9, 2017
julia326 added a commit to openvax/isovar that referenced this issue May 23, 2017
@nschloe
Copy link

nschloe commented Aug 18, 2017

Just bumped into this with numpy.arrays:

import numpy
def foobar(value=None):
    a = numpy.array([1, 2, 3])
    return +a, -a

@JVillella
Copy link

Same issue, e.x.

import numpy as np

def position_camera(dq=(0, 0, 0, 0), dT=(0, 0, 0)):
    q = -np.array((0.0, 1.0, 0.15, 0.35)) + dq

    # ...

@ScriptingSquirrel
Copy link

When using @pbaughman's snipped from the issue description for testing, it seems the false positive appeared with Pylint release 1.7.0. In Pylint 1.6.5 (=the last release before 1.7.0) this false positive does not get triggered.

Using the older release 1.6.5 until this is fixed.

jburos pushed a commit to hammerlab/stanity that referenced this issue Dec 21, 2017
@olysonek
Copy link

olysonek commented Jan 4, 2018

I have a similar issue with the following code:

try:
    open('file')
except OSError as ex:
    print(-ex.errno)

Pylint output:
E: 4, 7: bad operand type for unary -: errno (invalid-unary-operand-type)

@PCManticore
Copy link
Contributor

Folks, not all these examples are the same problem. :) I would suggest for some of them to be in their separate issues, since we don't need flow control for correctly checking numpy arrays or OSError instances. Creating different issues for these would mean that we could focus on smaller ones first rather than being concentrated on a major blob of work which is control flow inference

@dovahcrow
Copy link

After two years this looks like still an issue. Any ideas on how to solve this?

arza-zara added a commit to pinusc/qutebrowser that referenced this issue Nov 7, 2019
rakanalh pushed a commit to rakanalh/qutebrowser that referenced this issue Jan 30, 2020
@hippo91
Copy link
Contributor

hippo91 commented Mar 5, 2021

@nschloe @JVillella the bug you were facing with numpy does not occur anymore with latest pylint and master as soon as you import numpy with an alias (import numpy as np for example). For direct numpy import the situation will be correct once pylint-dev/astroid#918 will be merged.

I let this issue open because initial report was dealing with control-flow.

Please do not add any reports here unless it is directly related to control-flow.

@flaviovs
Copy link

Not sure if this qualifies as control-flow (apologies if it does not), but I can easily replicate the issue with:

# func.py
def func(moo = None):
    return -moo if moo else 0
$ pylint func
************* Module func
func.py:2: [E1130(invalid-unary-operand-type), func] bad operand type for unary -: NoneType

----------------------------------------------------------------------
Your code has been rated at -15.00/10 (previous run: -15.00/10, +0.00)

$ pylint --version
pylint 2.7.2
astroid 2.5.1
Python 3.7.3 (default, Jul 25 2020, 13:03:44) 
[GCC 8.3.0]

@xareelee
Copy link

xareelee commented May 17, 2021

Using disable & enable again did not work for me.

if value_sign == "-":
    # pylint: disable=invalid-unary-operand-type
    raw_value = (-raw_value)
    # pylint: enable=invalid-unary-operand-type
    raw_value = (-raw_value)   # this line will be also suppressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Control flow Requires control flow understanding Hacktoberfest Help wanted 🙏 Outside help would be appreciated, good for new contributors High effort 🏋 Difficult solution or problem to solve High priority Issue with more than 10 reactions
Projects
None yet
Development

No branches or pull requests