Pylint: typing.Pattern not treated as a type

Created on 28 Apr 2020  路  4Comments  路  Source: PyCQA/pylint

The following code:

from re import compile as reCompile
from typing import Pattern

pattern = reCompile(r'')
print(isinstance(pattern, Pattern))

produces the following output:

$ python3 Test.py
True
$ pylint3 Test.py
************* Module Test
Test.py:5: [W1116(isinstance-second-argument-not-valid-type), ] Second argument of isinstance is not a type

$ python3 --version
Python 3.6.9
$ pylint3 --version
pylint 2.5.0
astroid 2.4.0
Python 3.6.9 (default, Apr 18 2020, 01:56:04) 

Clearly, there's some problem with checking Pattern, as, for example, isinstance({}, Mapping) produces no warning.

bug

Most helpful comment

Same with Sequence. Of course, technically these are not in fact type instances, but can still be used with isinstance.

All 4 comments

Thank you for the issue, I can reproduce this with the latest pylint.

Same with Sequence. Of course, technically these are not in fact type instances, but can still be used with isinstance.

Totally agreed. IMHO most (any?) types/protocols from typing and collections (especially collections.abc) shall be treated as OK for isinstance().

Btw. this seems to be duplicate of https://github.com/PyCQA/pylint/issues/3507 and probably https://github.com/PyCQA/pylint/issues/2287 as well as https://github.com/PyCQA/pylint/issues/1162 (see discussions in them as there is also the idea to "copy" the implementation mypy uses - i.e. assert isinstance()).

Same with Callable

Was this page helpful?
0 / 5 - 0 ratings