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

Notice is not None and similar checks and prevent BadUnaryOperationMessage #791

Closed
gpshead opened this issue May 20, 2020 · 1 comment · Fixed by #1189
Closed

Notice is not None and similar checks and prevent BadUnaryOperationMessage #791

gpshead opened this issue May 20, 2020 · 1 comment · Fixed by #1189
Labels
topic-control-flow Control flow understanding

Comments

@gpshead
Copy link
Contributor

gpshead commented May 20, 2020

Steps to reproduce

Run pylint on this file:

"""Demonstrate invalid-unary-operand-type deficiency."""

class Klass:
    """Demonstration."""

    def __init__(self, param=None):
        self._param = param

    def method(self):
        """Returns -param or None."""
        if self._param is not None:
            return -self._param
        return None

Current behavior

example.py:12:19: E1130: bad operand type for unary -: NoneType (invalid-unary-operand-type)

Expected behavior

The invalid-unary-operand-type message should've been supressed because the code block it is coming from is guarded by an is not None check on the attribute in question. astroid has enough information to infer that within this file.

(confirming that... change the default value from the constructor parameter from None to any number and the error disappears)

I suppose you can argue that anything could have been changed to another type at any time by threads or some intervening call that somehow twiddles with this instances state, but the natural intent of code like this is that it hasn't which seems like the ideal thing for a best effort inference engine like astroid to infer.

Versions

pylint 2.5.2
astroid 2.4.1

@yilei
Copy link

yilei commented Sep 14, 2022

This appears to be a dup of pylint-dev/pylint#1472

@Pierre-Sassoulas Pierre-Sassoulas added the topic-control-flow Control flow understanding label Sep 14, 2022
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.13.0 milestone Nov 30, 2022
@Pierre-Sassoulas Pierre-Sassoulas removed this from the 2.13.0 milestone Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-control-flow Control flow understanding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants