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

Bad unsubscriptable-object error #2016

Closed
nickdrozd opened this issue Apr 14, 2018 · 2 comments
Closed

Bad unsubscriptable-object error #2016

nickdrozd opened this issue Apr 14, 2018 · 2 comments
Labels
Control flow Requires control flow understanding Minor 💅 Polishing pylint is always nice

Comments

@nickdrozd
Copy link
Collaborator

Steps to reproduce

def subscriptable(flag):
    state = None
    for _ in [0, 1]:
        if state is None:
            if flag:
                state = [None]
        elif state[0] is None:
            print('subscripted')

Current behavior

E: 7,13: Value 'state' is unsubscriptable (unsubscriptable-object)

Expected behavior

state is subscriptable by the time it gets subscripted, so that
error is wrong. After all, the code runs:

>>> subscriptable(False)
>>> subscriptable(True)
subscripted

That said, it does look like it could be an error, or maybe it would
be an error in a more complicated case, so maybe a warning could be
emitted -- possibly-unsubscriptable-object or something like that.

Related to, but more specific than, #701

pylint --version output

pylint 2.0.0
astroid 2.0.0
Python 3.6.5 (default, Mar 30 2018, 06:42:10) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
@PCManticore
Copy link
Contributor

From the point of view of pylint, state can also be None as it doesn't consider control flow when inferring the potential values.

@PCManticore PCManticore added Control flow Requires control flow understanding Minor 💅 Polishing pylint is always nice labels Apr 16, 2018
@nickdrozd
Copy link
Collaborator Author

I've come around to the belief that control flow should be as statically analyzable as possible. The example code here is terrible in that regard, even if it does happen to work out at runtime. So the warning is appropriate.

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 Minor 💅 Polishing pylint is always nice
Projects
None yet
Development

No branches or pull requests

2 participants