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 for unsubscriptable-object #6825

Closed
JohnVillalovos opened this issue Jun 4, 2022 · 3 comments
Closed

False positive for unsubscriptable-object #6825

JohnVillalovos opened this issue Jun 4, 2022 · 3 comments
Labels
C: unsubscriptable-object Issues related to 'unsubscriptable-object' check Control flow Requires control flow understanding Duplicate 🐫 Duplicate of an already existing issue Enhancement ✨ Improvement to a component

Comments

@JohnVillalovos
Copy link

Bug description

With this file:

#!/usr/bin/python3 -ttu

from typing import Optional

class Reproducer:
    def __init__(self, name):
        self.http_username: Optional[str] = None
        self.set_values(name)

    def _get_values_from_helper(self) -> None:
        value = getattr(self, "http_username")
        if not isinstance(value, str):
            return

        helper = value[2:].strip()
        print(helper)


    def set_values(self, value: str) -> None:
        self.http_username = value

Configuration

No response

Command used

pylint reproducer.py

Pylint output

************* Module reproducer
reproducer.py:15:17: E1136: Value 'value' is unsubscriptable (unsubscriptable-object)

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

Expected behavior

Would expect it to realize value is a string.

Pylint version

pylint 2.14.0
astroid 2.11.5
Python 3.10.4 (main, Mar 25 2022, 00:00:00) [GCC 11.2.1 20220127 (Red Hat 11.2.1-9)]

OS / Environment

No response

Additional dependencies

No response

@JohnVillalovos JohnVillalovos added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jun 4, 2022
@jacobtylerwalls
Copy link
Member

Thanks for the report. In one sense this is a duplicate of #1162, so I'll be closing it, but in another sense this is outside the scope of pylint:

pylint uses astroid as an inference engine to statically analyze your code to find what values variables actually take, not what they promise to take from type annotations. In this sample, only None is assigned to http_username. Of course, you have a setter, but despite the type annotation, it could take anything at runtime. If you want type-checking that performs sophisticated type-narrowing and incorporates your type annotations, mypy is your tool.

That does mean, unfortunately, you will run into issues where you have to disable no-member, unsubscriptable-object in pylint from time to time.

We are open to improvements where we quiet pylint warnings when we detect ambiguity, and there are some interesting proposals in astroid, see pylint-dev/astroid#1189, but I don't see astroid implementing full-fledged type-narrowing and type annotation analysis like mypy--which is what would be needed to find "ambiguity" here in code that only ever sets http_username to None. I hope that makes sense.

@jacobtylerwalls jacobtylerwalls closed this as not planned Won't fix, can't repro, duplicate, stale Jun 4, 2022
@jacobtylerwalls jacobtylerwalls added Control flow Requires control flow understanding Duplicate 🐫 Duplicate of an already existing issue and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jun 4, 2022
@jacobtylerwalls
Copy link
Member

Ah ha! Wait a darn minute! I feel like this is something that could be discussed in pylint-dev/astroid#1189, which introduces Constraint. If something previously inferable becomes Uninferable only in virtue of a Constraint, we should be able to pass back another sentinel like UninferableOnlyBecauseOfConstraintSoShutUpAboutNoMember.

@jacobtylerwalls jacobtylerwalls added Enhancement ✨ Improvement to a component C: unsubscriptable-object Issues related to 'unsubscriptable-object' check and removed Duplicate 🐫 Duplicate of an already existing issue labels Jun 4, 2022
@jacobtylerwalls
Copy link
Member

I'll go echo that comment on #1162 and pylint-dev/astroid#1189.

Despite my excitement I guess this is still a duplicate of either #1162 or #1498 or #4920.

@jacobtylerwalls jacobtylerwalls closed this as not planned Won't fix, can't repro, duplicate, stale Jun 4, 2022
@jacobtylerwalls jacobtylerwalls added the Duplicate 🐫 Duplicate of an already existing issue label Jun 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: unsubscriptable-object Issues related to 'unsubscriptable-object' check Control flow Requires control flow understanding Duplicate 🐫 Duplicate of an already existing issue Enhancement ✨ Improvement to a component
Projects
None yet
Development

No branches or pull requests

2 participants