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

E1136: False positive when used with typing (mypy) patterns #3979

Closed
ssbarnea opened this issue Dec 14, 2020 · 9 comments · Fixed by #5720
Closed

E1136: False positive when used with typing (mypy) patterns #3979

ssbarnea opened this issue Dec 14, 2020 · 9 comments · Fixed by #5720
Labels
Bug 🪲 C: unsubscriptable-object Issues related to 'unsubscriptable-object' check False Positive 🦟 A message is emitted but nothing is wrong with the code typing
Milestone

Comments

@ssbarnea
Copy link
Contributor

ssbarnea commented Dec 14, 2020

Apparently even the master branch code does not yet play well with a common pattern used to add type hints. The linked example is not unique, the same pattern can be seen in many places.

While adding # pylint: disable=unsubscriptable-object can be used as a temporary workaround, it worth creating a bug as the more people will be adopting type hints, the more such problems we will face.

import os
from typing import TYPE_CHECKING, Any, Union

if TYPE_CHECKING:
    BasePathLike = os.PathLike[Any] # <-- that is where pylint identifies E1136
else:
    BasePathLike = os.PathLike

foo : Union[str, BasePathLike] = "bar"

Current behavior

example.py:5:19: E1136: Value 'os.PathLike' is unsubscriptable (unsubscriptable-object)

Expected behavior

Pass the linting as the code below is correct.

pylint --version output

2.6.0 and master, on python 3.9

@ajeetdsouza
Copy link

This failed for me too:

from typing import Optional
x: Optional[int]

@hippo91
Copy link
Contributor

hippo91 commented Dec 20, 2020

@ssbarnea thanks for your report. I can reproduce it.
@ajeetdsouza your issue is different from original one and has already been fixed thanks to #3890

@hippo91 hippo91 added Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code typing labels Dec 20, 2020
@cdce8p
Copy link
Member

cdce8p commented Feb 3, 2021

This seems to be related to #3951
Which will be fixed with the next astroid update: pylint-dev/astroid#885

@hippo91
Copy link
Contributor

hippo91 commented Feb 6, 2021

@cdce8p thank you! I can confirm the bug is fixed. I can no longer reproduce it with current master branches of astroid and pylint.

@alkatar21
Copy link

I have the same message with exactly this code in Python 3.8 is this intentional or a bug?

pylint 2.12.2
astroid 2.9.3
Python 3.8.10

@Pierre-Sassoulas
Copy link
Member

Pierre-Sassoulas commented Jan 25, 2022

I can reproduce the issue again with python 3.8. It seems like the issue can be reintroduced easily in astroid and we'd need tests there too ideally.

@cdce8p
Copy link
Member

cdce8p commented Jan 30, 2022

The issue was indeed fixed, but only for Python 3.9+.
For 3.8, it's related to #4369, although with a different pattern.

@Avasam
Copy link

Avasam commented Feb 12, 2022

I'm not entirely certain if related, but I'm having the same issue with Column type from` the sqlalchemy typestubs
pylint 2.12.2
Python 3.9.6

from __future__ import annotations
from sqlalchemy import Column, DateTime, Integer, String

class Player(BaseModel):
    __tablename__ = "player"

    user_id = db.Column(db.String(8), primary_key=True)
    name = db.Column(db.String(32), nullable=False)
    # The biggest region code I found so far was "us/co/coloradosprings" at 21
    country_code = db.Column(db.String(24))
    score = db.Column(db.Integer, nullable=False)
    score_details = db.Column(db.String())
    last_update = db.Column(db.DateTime())
    rank: Optional[int] = None

    schedules = db.relationship("Schedule", back_populates="owner")

    @overload
    def __init__(  # type: ignore
        self,
        user_id: str | Column[String],
        name: str | Column[String],
        country_code: Optional[str | Column[String]],
        score: int | float | Column[Integer],
        last_update: Optional[str | Column[DateTime]],
        score_details: Optional[str | Column[String]] = ...,
        rank: Optional[int] = ...
    ): ...
    
[...]

image

@jacobtylerwalls jacobtylerwalls added this to the 2.14.0 milestone Apr 9, 2022
Pierre-Sassoulas added a commit that referenced this issue Apr 9, 2022
…5720)

* Add a regression test for issue #3979

* Don't emit `unsubscriptable-object` for statements in type-checking blocks

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
@cdce8p
Copy link
Member

cdce8p commented Apr 13, 2022

I'm not entirely certain if related, but I'm having the same issue with Column type from` the sqlalchemy typestubs pylint 2.12.2 Python 3.9.6

@Avasam That shouldn't be an issue anymore. Please make sure to test the latest pylint release. Additionally, stringified annotations should be used (either "str | Column[String]" or from __future__ import annotations). Then no unsubscriptable-object warning is emitted.

@jacobtylerwalls jacobtylerwalls modified the milestones: 2.14.0, 2.13.8 May 1, 2022
Pierre-Sassoulas added a commit that referenced this issue May 2, 2022
…5720)

* Add a regression test for issue #3979

* Don't emit `unsubscriptable-object` for statements in type-checking blocks

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
Pierre-Sassoulas added a commit that referenced this issue May 2, 2022
…5720)

* Add a regression test for issue #3979

* Don't emit `unsubscriptable-object` for statements in type-checking blocks

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 C: unsubscriptable-object Issues related to 'unsubscriptable-object' check False Positive 🦟 A message is emitted but nothing is wrong with the code typing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants