-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 with E1136 unsubscriptable-object #1498
Comments
This has hit us too. We are using a variable that is initialized to None, but then gets assigned to dict in a loop. I think something like this will trigger it too:
not sure how you would solve this, seems like the inferencer would have to go into the if/else clauses to see what might get assigned to the variable to figure out that it's Union(None, dict). |
Even checking |
This check is flaky and sometimes reports false-positive errors. pylint-dev/pylint#1498 Disable the check since we cannot rely on it to find report actual bugs. Signed-off-by: Robin Jarry <robin@jarry.cc>
Yes @hughes that's a problem that's still crippling pylint even today (but hopefully with the new number of committers we have, we might tackle it at some point). I'm referring here to the fact that pylint does not quite grasp control flow, that is, if you use |
I had this problem today opening a config file. |
+1 |
I have this false positive too. I reference a variable instanced as none in the main object class, and then change it in function from an inherited class. class Response():
def __init__(self):
self.raw_response = None
def filter_raw_response(self):
if not self.raw_response:
return None
num_datapoints = len(self.raw_response['Datapoints']) # Error shows here
if num_datapoints == 0:
return None
class SpecificResponse(Response):
def craft_response(self)
# Variable instanced here:
self.raw_response = self.client.get_metric_statistics() |
+1 I got this too when I tried to iterate over candidates, a matrix like variable. It reports item as NoneType and raise E1136 error. But, actually, it is a list.
|
False positive even if the custom class implements the |
Will this ever be fixed then? |
@melikyuksel Unless someone proposes a patch, it's unlikely. |
I had the same issue when using |
Would this actually be a false positive? Seems to me it is following the intended behavior.
|
@sgornic |
The warning is because args in initialized as None See pylint-dev/pylint#1498 False positive with unsubscriptable-object...
There is also issue #2063 that seems to describe the same error. |
I also get this with the built-in
|
Just make your list Python3 compatible: |
I'm also seeing this with __getitem__ implemented |
I'm getting the same with a Pandas DataFrame:
Output of |
a similar thing also occurs with bidict |
@kbakk @ricardoquesada thanks for your report.
with
and with
I'm not closing this issue, because original issue is not the same as the one we are dealing with here. |
This issue also appears where the variable in question is a class attribute, as in the following minimal example: class MyClass:
my_cashed_value = None
def get_message(self):
result = MyClass.my_cashed_value
if result is not None:
return result["message"]
else:
message = "my cached message"
MyClass.my_cashed_value = {"message": message}
return message (using pylint 2.6.0 and python 3.8.3) |
@BenLatham thank for your report. |
Pylint does not see __members__ as existing or subscribable. Maybe see also: pylint-dev/pylint#1498
Pylint does not see __members__ as existing or subscribable. Maybe see also: pylint-dev/pylint#1498 (cherry picked from commit 13f061d)
Similar issue here using python3.8. In this example
I get
on the second call to |
Yes, @andrea-cassioli-maersk astroid do not have real control flow (no restriction on the inference after |
I'm still not understanding why pylint can infer this case correctly: def func(i) -> Optional[Tuple]:
return (i, i) if i else None
print(func(1)[0]) # OK
print(func(0)[0]) # unsubscriptable-object but not this: foo: Optional[Tuple] = None
print(foo and foo[0]) # unsubscriptable-object (??) |
I think we can infer that in the first call of the first example For the second example we only infer that |
I see. It would be a small quality of life improvement, but even without using the type annotation, pylint could certainly infer that |
Perhaps I don't understand your examples but in both examples the |
the expression but to clarify the example: foo: Optional[Tuple] = None
# ... code that assigns a tuple to foo
print(foo and foo[0]) # unsubscriptable-object (??) it's actually a very common case, since variables declared type |
Ah my bad, that's indeed quite obvious and a pattern we use ourselves. Should have examined the example better. However, it is not quite that easy to add such control flow recognition to |
Another short-term idea: without fully supporting type hints, if pylint could at least see that the var is declared as a union including at least one indexible type, it should not emit |
I mean seriously, how old is this bug? pylint-dev/pylint#1498
This comment was marked as spam.
This comment was marked as spam.
+1 |
following code also triggers it maybe disable it by default as very likely to produce false positives?
|
I am getting this false positive as well. Below is the code triggering this: from sqlalchemy.orm import Mapped, mapped_column
class UserDB(Base):
"""
User database model.
"""
__tablename__ = "users"
user_id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True, autoincrement=True, nullable=False) The documentation from sqlalchemy describes how to use Mapped in the following link: But this is causing false positives with pylint: My pylint version:
|
Steps to reproduce
z.py
pylint z.py
Current behavior
Expected behavior
no errors, no warnings
pylint --version output
The text was updated successfully, but these errors were encountered: