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

False unsubscriptable-object positive with generic subprocess.Popen and python3.9 #4034

Closed
scop opened this issue Jan 18, 2021 · 4 comments · Fixed by pylint-dev/astroid#882 or PennyDreadfulMTG/Penny-Dreadful-Tools#8447
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.9

Comments

@scop
Copy link
Contributor

scop commented Jan 18, 2021

Steps to reproduce

$ cat /tmp/t.py
from subprocess import Popen, PIPE

def foo(popen: Popen[str]) -> None:
    pass

foo(Popen(["ls"], stdout=PIPE))

Current behavior

$ pylint /tmp/t.py | grep subs
/tmp/t.py:3:15: E1136: Value 'Popen' is unsubscriptable (unsubscriptable-object)

Expected behavior

No unsubscriptable-object error.

pylint --version output

pylint 2.6.0
astroid 2.4.2
Python 3.8.0 (default, Dec 13 2020, 12:54:06) 
[GCC 7.5.0]
dbaty added a commit to Polyconseil/astroid that referenced this issue Jan 18, 2021
…thon 3.9

This is necessary for pylint to detect that `subprocess.Popen` is
subscriptable, starting from Python 3.9 (see pylint-dev/pylint#4034).

    $ python3.9
    >>> import subprocess
    >>> subprocess.Popen.__class_getitem__
    <bound method GenericAlias of <class 'subprocess.Popen'>>
@dbaty
Copy link
Contributor

dbaty commented Jan 18, 2021

tl;dr: on Python 3.8 and earlier, everything seems fine, I don't think there is a bug. With Python 3.9, though, this is a bug and a fix has been proposed in Astroid (a dependency of pylint): pylint-dev/astroid/pull/882.

Maintainers: I would mention "under Python 3.9" in the title and add the corresponding issue tag. :)


On Python 3.8 and earlier

Popen is indeed not subscriptable with in Python 3.8 and earlier. For example, running your code with Python 3.8 I get:

$ python3.8 test.py
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    def foo(popen: Popen[str]) -> None:
TypeError: 'type' object is not subscriptable

So I think that pylint is right to report an error here up to Python 3.8.

On Python 3.9

However, under Python 3.9, Popen is subscriptable. Running your snippet with Python 3.9 does not raise any error.

So, with Python 3.9, pylint should not report the unsubscriptable-object warning. Yet it does, which is a bug.

To determine whether a class is subscriptable, pylint looks for a __class_getitem__ method. Here it cannot find one. This is because Astroid "overrides" the definition of subprocess.Popen (with what Astroid calls a "brain") and there the __class_getitem__ method is missing.

I proposed a fix in Astroid: pylint-dev/astroid/pull/882.

dbaty added a commit to Polyconseil/astroid that referenced this issue Jan 18, 2021
…thon 3.9

This is necessary for pylint to detect that `subprocess.Popen` is
subscriptable, starting from Python 3.9 (see pylint-dev/pylint#4034).

    $ python3.9
    >>> import subprocess
    >>> subprocess.Popen.__class_getitem__
    <bound method GenericAlias of <class 'subprocess.Popen'>>
@scop
Copy link
Contributor Author

scop commented Jan 18, 2021

Oh, interesting. The error was provoked by a fix to an issue flagged by running mypy with python 3.8 in python_version 3.8 mode. I think that makes it a mypy (well or typeshed) bug, too. Thanks!

@scop
Copy link
Contributor Author

scop commented Jan 18, 2021

Ref python/typeshed#4948

@hippo91 hippo91 added False Positive 🦟 A message is emitted but nothing is wrong with the code Bug 🪲 python 3.9 labels Jan 19, 2021
@hippo91 hippo91 changed the title False unsubscriptable-object positive with generic subprocess.Popen False unsubscriptable-object positive with generic subprocess.Popen and python3.9 Jan 19, 2021
@hippo91
Copy link
Contributor

hippo91 commented Jan 19, 2021

@scop thanks for the report. @dbaty thanks for your investigation and corresponding PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.9
Projects
None yet
3 participants