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

Created on 14 Dec 2020  路  4Comments  路  Source: PyCQA/pylint

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

bug false-positive topic-typing

Most helpful comment

@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

All 4 comments

This failed for me too:

from typing import Optional
x: Optional[int]

@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

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TBoshoven picture TBoshoven  路  3Comments

PCManticore picture PCManticore  路  3Comments

PCManticore picture PCManticore  路  3Comments

elirnm picture elirnm  路  3Comments

z4y4ts picture z4y4ts  路  3Comments