-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
False positive unsubscriptable-object #9549
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
Comments
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 :) |
@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). |
We have faced a similar problem too. @jenstroeger deleting the virtual env and rebuilding it doesn't fix our issue. We call 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 |
Could you provide the output of |
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: |
|
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 |
I have the same error for class NetworkAwareModel(ApostroBaseModel):
__abstract__ = True
network_id: Mapped[int] = mapped_column(ForeignKey("network.id"))
@declared_attr
def network(self) -> Mapped[Network]:
"""Ensures that a new instance is created for each subclass"""
return relationship("Network")
|
If you're still intersted, here is my requirements.txt:
|
I had the same issue, by looking at the source code from the You can change the hinted types to match the return type of sqlalchemy functions ( from sqlalchemy import ForeignKey
from sqlalchemy.orm import MappedColumn, Relationship, mapped_column, relationship
from sqlalchemy.sql.sqltypes import String, Integer
class RoleReaction(Base):
__tablename__ = 'user_roles_reactions'
id: MappedColumn[int] = mapped_column(Integer, primary_key=True)
role_name: MappedColumn[str] = mapped_column(String)
reaction_name: MappedColumn[str] = mapped_column(String)
group_id: MappedColumn[int] = mapped_column(ForeignKey('user_roles_groups.id'))
group: Relationship[RoleGroup] = relationship(RoleGroup) It made my pylint error messages disappear. |
We have experienced this problem again under new circumstances that may be of interest to the After some refactoring of our codebase, In the original refactor, two files were moved and modified slightly: As part of the investigation this refactor was re-done in a piecemeal fashion to help clarify what was happening. The two files above were created in the new location, in their final form, instead of simply moved. Then everything using the old files was modified to use the new ones. Finally, the old files were deleted, and it is only after this deletion that the unsubscriptable error appears:
This investigation is available for inspection on this branch: pylint-bug-unsubscriptable-3 Also worth noting:
|
I have observed this issue as well in one of our projects. At first I tried using Since it was clearly not a code change triggering this I suspected code size might be an issue. That’s why I tried deleting the My theory is now: The result depends on the order in which pylint evaluates the files in the project. Deleting and recreating files changes that order. |
I was able to create a very minimal example that is reproducible for me on Debian 12 (with Python 3.11.2): mkdir pylint-test; cd pylint-test
python -m venv .venv
. .venv/bin/activate
pip install sqlalchemy pylint
pylint models.py # see below Output:
"""DB models."""
# Model classes are useful even without methods.
# pylint: disable=too-few-public-methods
import typing as t
import sqlalchemy as sa
from sqlalchemy import orm
# Create database connection object
db = sa.declarative_base()
class Organization(db.Model):
"""Organization model."""
id: orm.Mapped[int] = orm.mapped_column(primary_key=True)
class Component(db.Model):
"""Model for a component."""
id: orm.Mapped[int] = orm.mapped_column(primary_key=True)
parent_id: orm.Mapped[t.Optional[int]]
parent: orm.Mapped["Component"] = orm.relationship(
remote_side=[id],
back_populates="components",
) Update: I was also able to reproduce this on Gentoo with Python 3.13.1. |
Uh oh!
There was an error while loading. Please reload this page.
Bug description
Hi! I have weird behavior in my code.
Here is a code example:
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
Command used
Pylint output
Expected behavior
No errors, 10/10
Pylint version
OS / Environment
Linux, docker
Additional dependencies
No response
The text was updated successfully, but these errors were encountered: