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

Incorrect 'unsubscriptable-object' reported #3637

Open
qsmy41 opened this issue May 19, 2020 · 12 comments
Open

Incorrect 'unsubscriptable-object' reported #3637

qsmy41 opened this issue May 19, 2020 · 12 comments
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code

Comments

@qsmy41
Copy link

qsmy41 commented May 19, 2020

Steps to reproduce

Running Pylint on the following code will produce the error on the last line.

import statsmodels.api as sm

data = sm.datasets.longley.load_pandas().data

properties = ['GNP', 'POP']
specificData = data[properties]

Current behavior

Running Pylint gives

example.py|6 col 16 error| E1136: Value 'data' is unsubscriptable (unsubscriptable-object)
example.py|6 col 16 error| unsubscriptable-object: Value 'data' is unsubscriptable

Expected behavior

No errors/warnings should be produced.

pylint --version output

pylint 2.5.2
astroid 2.4.1
Python 3.7.7 (default, Apr 14 2020, 11:51:19)
[Clang 11.0.0 (clang-1100.0.33.12)]

@beauxq
Copy link

beauxq commented Sep 4, 2020

Same problem with this:

import numpy as np

ARRAY = np.array([[1, 2, 3]]).T

print(ARRAY[0])

Value 'ARRAY' is unsubscriptable (unsubscriptable-object)
There should be no error.

@Cielquan
Copy link

Cielquan commented Nov 1, 2020

Have a similar problem with type hints.

This LoC

EnvVarTypes = Union[str, int, float, bool, None]

gives E1136: Value 'Union' is unsubscriptable (unsubscriptable-object).

This LoC

def validate(self, line: str, _: GitCommit) -> List[Optional[RuleViolation]]:

gives E1136: Value 'Optional' is unsubscriptable (unsubscriptable-object).

BUT those errors only occur in with

$ pylint --version
pylint 2.6.0
astroid 2.4.2
Python 3.9.0 (default, Oct  6 2020, 02:47:17) 
[GCC 7.5.0]

and not with

$ pylint --version
pylint 2.6.0
astroid 2.4.2
Python 3.8.6 (default, Oct  6 2020, 03:22:36) 
[GCC 7.5.0]

@leycec
Copy link

leycec commented Nov 21, 2020

This also breaks PEP 560 and PEP 585. See this comment at the related issue #3882.

@hippo91
Copy link
Contributor

hippo91 commented Nov 26, 2020

@beauxq thanks for your comment. It seems your issue is not linked to the original one. Morevoer it should have been fixed through pylint-dev/astroid#831.

@hippo91
Copy link
Contributor

hippo91 commented Nov 26, 2020

@Cielquan thanks for your report. However your issue is not linked to the original one but it is due to the fact pylint is not yet supporting python3.9. We are working on it.

@hippo91
Copy link
Contributor

hippo91 commented Nov 26, 2020

To any people thinking adding a comment here, please do only if your problem seems to be linked to panda's support in pylint. Otherwise please open another issue. Thanks.

@PCManticore PCManticore added Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code labels Dec 29, 2020
@MiltiadisKoutsokeras
Copy link

Another case that breaks our code checking CI/CD:

"""Pylint unsubscriptable-object example"""
import pandas as pd

result = pd.DataFrame()
cols = ['a', 'b', 'c', 'd']
CSVDATA = 'data.csv'
df = pd.read_csv(CSVDATA)
df['d'] = [CSVDATA.split('.', maxsplit=1)[0]] * df.shape[0]
result = pd.concat([result, df[cols]], axis=0)
print(result)

Pylint reports:

************* Module pylint_wrong
pylint_wrong.py:9:28: E1136: Value 'df' is unsubscriptable (unsubscriptable-object)

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

@bhargavgohil25
Copy link

@MiltiadisKoutsokeras got any solution to remove this error ? I am facing the same but on initializing i.e on pd.DataFrame()

@juliangilbey
Copy link

For those trying to fix this issue, the case I reported in #6942 may be useful:

import pandas as pd  # type: ignore

df = pd.read_csv("data.csv")

df_a = df.drop(columns=["a"])
df_b = df["b"]

gives an unsubscriptable-object error message, but swapping the lines doesn't:

import pandas as pd  # type: ignore

df = pd.read_csv("data.csv")

df_b = df["b"]
df_a = df.drop(columns=["a"])

Hope this helps!

@wclark
Copy link

wclark commented Mar 23, 2023

I ran into this issue myself, and after looking over some examples here and in related/duplicated tickets, I have a hunch that the cause is that methods like drop (or in my case, set_index) don't always return a DataFrame -- sometimes they return None, per the Pandas API. If you force the methods to return None (by passing an inplace=True parameter) and restructure your code accordingly, this error seems to go away.

I'd argue this is still something to fix in Pylint, because these Pandas methods ONLY seem to return None when inplace=True is passed -- which Pylint could (and arguably should) easily check.

@nathanjmcdougall
Copy link

Note that now the original example (and similar cases) now give invalid-name in addition to E1136:

import statsmodels.api as sm

data = sm.datasets.longley.load_pandas().data

properties = ['GNP', 'POP']
specificData = data[properties]
************* Module yetanotherbug
yetanotherbug.py:4:0: C0103: Constant name "data" doesn't conform to UPPER_CASE naming style (invalid-name)
yetanotherbug.py:7:15: E1136: Value 'data' is unsubscriptable (unsubscriptable-object)

Also, juliangilbey's example is now fixed with the latest versions of pylint and pandas.

@scorphus
Copy link

scorphus commented Jul 31, 2024

I cannot reproduce this issue with the above snippets.

However, the following snippet reproduces the issue:

"""Pylint unsubscriptable-object example"""

import random

import pandas as pd


data = []
for n in range(random.randint(0, 10)):
    data.append({"number": n**2})

df = pd.DataFrame(data) if len(data) > 0 else None

if df is None:
    raise ValueError("DataFrame is empty")

print(df["number"].sum())

df = pd.DataFrame(df)  # This line clears the error (and is my workaround for now)
print(df["number"].sum())
$ pylint -d C dataframe_unsubscriptable.py
************* Module dataframe_unsubscriptable
dataframe_unsubscriptable.py:17:6: E1136: Value 'df' is unsubscriptable (unsubscriptable-object)

------------------------------------------------------------------
Your code has been rated at 5.45/10 (previous run: 4.55/10, +0.91)
$ pylint --version
pylint 3.2.6
astroid 3.2.4
Python 3.11.9 (main, May 23 2024, 10:16:49) [Clang 15.0.0 (clang-1500.3.9.4)]

Thanks for taking the time to look into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

No branches or pull requests