-
-
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 for use in or
after is None
guard
#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 |
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:
|
i see this has been open for awhile. is there trouble getting a dev to fix this issue? what is blocking this for so long |
Like @optimaltayfun and several others, I'm getting this while upgrading to SQLAlchemy 2.0. Lots of other people will hit this doing the same thing, I think. |
because noone implemented fix for this you can submit pull request fixing this, hire someone to do this or hope that someone else will do either as someone who is developer on open source projects: this kind of comment is neither helpful nor encouraging, and if someone is doing it as unpaid hobby (or poorly paid one) it will sooner or later result in them dropping such project altogether note that ability to skip unhelpful/broken rules is present in pylint (and I use it to suppress many rules I consider unhelpful), you can use it here (and this issue has both Help wanted "🙏Outside help would be appreciated, good for new contributors" and Needs PR "This issue is accepted, sufficiently specified and now needs an implementation" labels) |
sorry, I was genuinely trying to understand the bottleneck to the solution |
Control flow is hard, it requires generic and intimidating changes in astroid (the underlying code representation of pylint) and it's inference engine (the infer function on each node of the astroid's abstract syntax tree) that is going to affect a lot of code, thus a variety of very strange and unexpected code too. If the code is too clever or too dynamic it's harder (I don't know if that's the case here). The volunteers maintainers of this project are already busy trying to follow the upstream rhythm of changes from python core to support the latest interpreter, and keeping up with the reviews of all proposed MR takes time too. (Tidelift pays me to fix security issues and prevent supply chain attack, which I do, but everything else is us giving away free time). Offering a bounty somewhere (issue hunt, github sponsor, etc.) or implementing the change yourself would probably speed things up. |
Would this fall into your $1000 one-time medium bug category? For everyone else: I'd be happy to help sponsor getting this fixed but I can't really afford to do it myself. Maybe we can band together 10-20 people who can all donate a bit to help get us there (if you'd be willing to donate $50-100 maybe react with a 🚀 to this comment). |
This issue would requires a big new feature in astroid (make astroid understand control flow better) and would fix numerous nearly duplicate issues, so it's probably more akin to a large project. To be honest at this point I have too much on my plate to handle this even for a large bounty, but maybe @jacobtylerwalls or @DanielNoord could be interested (or someone on IssueHunt, from a country with low cost of living for whom the monetary compensation would be worth months of work). |
#1498 (comment) is a separate issue from the original post. I'll open a new issue for it. |
or
after is None
guard
Actually, that SQLAlchemy issue looks like #9549, which has a workaround at #9549 (comment). |
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: