pylint icon indicating copy to clipboard operation
pylint copied to clipboard

False positive unsubscriptable-object

Open DarthLegiON opened this issue 2 months ago • 9 comments

Bug description

Hi! I have weird behavior in my code.

Here is a code example:

from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.sql.sqltypes import String, Integer


class RoleReaction(Base):
    __tablename__ = 'user_roles_reactions'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    role_name: Mapped[str] = mapped_column(String)
    reaction_name: Mapped[str] = mapped_column(String)
    group_id: Mapped[int] = mapped_column(ForeignKey('user_roles_groups.id'))
    group: Mapped[RoleGroup] = relationship(RoleGroup)

It causes several unsubscriptable-object errors in pylint (look into the output section).

But when I remove the last line (group definition), all of them disappear. When I move it upper, they don't. There is only one file in the whole project that causes such problem.

Configuration

[MASTER]
disable=
    C0114, # missing-module-docstring
    C0115, # missing-class-docstring
    C0116, # missing-function-docstring
    R0903, # too-few-public-methods
    R0801, # similar lines

extension-pkg-allow-list=
    dependency_injector,
    discord

max-line-length=120

Command used

PYTHONPATH=./src pylint --recursive=y --rcfile=.pylintrc ./src/roles_reactions/db.py

Pylint output

************* Module src.roles_reactions.db
src/roles_reactions/db.py:19:8: E1136: Value 'Mapped' is unsubscriptable (unsubscriptable-object)
src/roles_reactions/db.py:20:11: E1136: Value 'Mapped' is unsubscriptable (unsubscriptable-object)
src/roles_reactions/db.py:21:15: E1136: Value 'Mapped' is unsubscriptable (unsubscriptable-object)
src/roles_reactions/db.py:22:19: E1136: Value 'Mapped' is unsubscriptable (unsubscriptable-object)
src/roles_reactions/db.py:23:14: E1136: Value 'Mapped' is unsubscriptable (unsubscriptable-object)

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

Expected behavior

No errors, 10/10

Pylint version

pylint 3.1.0
astroid 3.1.0
Python 3.10.5 (main, Aug  2 2022, 10:31:34) [GCC 10.2.1 20210110]

OS / Environment

Linux, docker

Additional dependencies

No response

DarthLegiON avatar Apr 13 '24 09:04 DarthLegiON

Hi, without the relevant import statements we won't be able to diagnose the issue. Please make sure to include all relevant code and dependencies :)

DanielNoord avatar Apr 19 '24 19:04 DanielNoord

@DarthLegiON thank you for the report, I’ve seen similar problems in my own project. Unfortunately, we didn’t investigate more but in our case deleting the virtual env and rebuilding it worked. Not a fix, I know, but it may unblock you?

@DanielNoord the code shown above is a SQLAlchemy v2 declarative table mapping (docs).

jenstroeger avatar Apr 23 '24 09:04 jenstroeger

We have faced a similar problem too.

@jenstroeger deleting the virtual env and rebuilding it doesn't fix our issue. We call pylint using a pre-commit hook. So, I ran pre-commit clean to clean up its cach, but still I face the same issue.

This is the error we get:

src/macaron/slsa_analyzer/checks/infer_artifact_pipeline_check.py:36:8: E1136: Value 'Mapped' is unsubscriptable (unsubscriptable-object)
src/macaron/slsa_analyzer/checks/infer_artifact_pipeline_check.py:39:16: E1136: Value 'Mapped' is unsubscriptable (unsubscriptable-object)
src/macaron/slsa_analyzer/checks/infer_artifact_pipeline_check.py:42:17: E1136: Value 'Mapped' is unsubscriptable (unsubscriptable-object)
src/macaron/slsa_analyzer/checks/infer_artifact_pipeline_check.py:45:13: E1136: Value 'Mapped' is unsubscriptable (unsubscriptable-object)

You can find the code here: https://github.com/oracle/macaron/blob/f9be5387ae5ed4e04ceabb9b86579929d2ddcb8f/src/macaron/slsa_analyzer/checks/infer_artifact_pipeline_check.py#L36-L45

behnazh-w avatar Apr 23 '24 09:04 behnazh-w

Could you provide the output of pylint --version @behnazh-w, please ? @DarthLegiON I upgraded your example but we still miss the content of Base and RoleGroup, would you mind providing us the detail or create a minimal reproducer, please ?

Pierre-Sassoulas avatar Apr 23 '24 10:04 Pierre-Sassoulas

Could you provide the output of pylint --version @behnazh-w, please ?

pylint 3.1.0
astroid 3.1.0
Python 3.11.3 (main, Apr  4 2023, 22:36:41) [GCC 11.3.0]

sqlalchemy version: "SQLAlchemy >=2.0.0,<3.0.0", so 2.0.29 atm.

behnazh-w avatar Apr 23 '24 11:04 behnazh-w

I upgraded your example but we still miss the content of Base and RoleGroup, would you mind providing us the detail or create a minimal reproducer, please ?

Base is probably defined as described here and RoleGroup seems to be another table mapping just like the RoleReaction in the example. (It may even be enough to declare an __abstract__ table mapping, for the sake of a reproducible example?)

jenstroeger avatar Apr 23 '24 11:04 jenstroeger

Sorry for the delay and thanks for your response!

Base definition:

Base = declarative_base()

Rolegroup definition:

class RoleGroup(Base):
    __tablename__ = 'user_roles_groups'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    give_role_channel_id: Mapped[int] = mapped_column(BigInteger)
    give_role_message_id: Mapped[int] = mapped_column(BigInteger)

@jenstroeger about deleting a virtual env. Maybe it will surprise you but I don't use one even in development mode. I run the project in Docker. And it fails both in local environment and in CI pipeline, but docker and pip was rebuilt every time. Problem disappears only when I delete the last line with group definition.

DarthLegiON avatar May 16 '24 15:05 DarthLegiON